I would like to write a query for SQL Server to select “Sales” by “month”
(Sales from one table[sales_fact] and month from another table[time_by_day])
where sales is grouped by “ product_name"
and it is again form another table[product] i.e. sales/product by month. Here is my query:
SELECT
time_by_day.the_month ,
( SELECT sum(store_sales)
FROM sales_fact
INNER JOIN product
ON sales_fact.product_id = product.product_id
GROUP BY product_name
)
AS total_sales
FROM sales_fact
INNER JOIN time_by_day
ON sales_fact.time_id = time_by_day.time_id
GROUP BY
time_by_day.the_month
ORDER BY
DATEPART(mm,CAST(the_month+ ' 1900' AS DATETIME)) asc;
But it is not working. Getting error for the subquery. Can I get some advice pls?
Best Answer
Your subquery:
returns a list of aggregate sum by product.
But your main query expects only one value due you're grouping by month.
SQL Server returns an error indicating that your subquery returns more than one value.
If you want aggregate sales by product, instead of a subquery, join your products table on your main query on this way:
However if you need total sales by month, remove
prduct.id
andproduct.name
from both, SELECT and GROUP BY statements.If your time_by_day table has a datetime field, you can use it on your ORDER BY clause instead of build a datetime value. In fact if you're filtering more than one year, actual ORDER BY clause do not work properly.
If there is no datetime value, you can order by month without cast.