Month by Month Sales breakdown for top 10 Product for specific year Using Adventure Works Database in SQL Server

569 views Asked by At

This is what I used to find the top 10 products for 2013 by total sum of sales. What is the easiest way to get a monthly breakdown of sales for a specific product ID for the year?

SELECT TOP 10 
    sod.ProductID, prd.Name, SUM(LineTotal) AS SumOfSales
FROM 
    Sales.SalesOrderDetail AS SOD
JOIN 
    Sales.SalesOrderHeader AS SOH ON SOD.SalesOrderID = SOH.SalesOrderID
JOIN
    Production.Product prd ON prd.ProductID = sod.ProductID
WHERE 
    SOH.OrderDate >= '01/01/2013' 
    AND SOH.OrderDate <= '12/31/2013'
GROUP BY 
    sod.ProductID, prd.Name
-- HAVING SUM(LineTotal) >= 2000000
ORDER BY 
    SUM(LineTotal) DESC

Image of SQL output

This is what I found after research but it says date_format is invalid

select date_format(sdate,'%M-%Y') as sdate,
sum(LineTotal) as 'netsales',
from Sales.SalesOrderDetail
where ProductID=782 and ModifiedDate >= '01/01/2013' and ModifiedDate <= '12/31/2013'
group by MONTH(sdate)
order by MONTH(sdate);
1

There are 1 answers

2
Mehmet Arlı On

Full list:

select *
from Sales.SalesOrderDetail
where ProductID=782 and DATEPART(YEAR,ModifiedDate) = 2013

You can use "*" or column names

Mothly Group List:

select datepart(month,ModifiedDate) as sdate,
sum(LineTotal) as 'netsales',
from Sales.SalesOrderDetail
group by datepart(month,ModifiedDate)

but you will see month number in sdate column.