Graph of top four products each month for last 6 months?
(Yeah I know I typ'od the title)
I need create a chart which displays the four highest selling products for each month in the last 6 months to be displayed in a report.
Ive managed to get all the products for each month for the last six months but im unsure how to filter so it displays only the top four for each month.
Ive done some programming but I thiinnk im going about this wrongly.
This is my code so far
This produces ChartLabel,Prod uctId,QunatityM ade,ReleaseDate columns.
but its everything for each month, I just want the top 4 for each month to be sent to the graph.
(Yeah I know I typ'od the title)
I need create a chart which displays the four highest selling products for each month in the last 6 months to be displayed in a report.
Ive managed to get all the products for each month for the last six months but im unsure how to filter so it displays only the top four for each month.
Ive done some programming but I thiinnk im going about this wrongly.
This is my code so far
Code:
DECLARE @StartDate SMALLDATETIME -- Variables for the dashboard
DECLARE @EndDate SMALLDATETIME
SET @StartDate = DATEADD(s,0,DATEADD(mm, DATEDIFF(m,0,GETDATE())-6,0)) -- get todays month minus 6
SET @EndDate = GETDATE() -- get todays date
----
DECLARE @WhereDateStart SMALLDATETIME
DECLARE @WhereDateEnd SMALLDATETIME
SET @WhereDateStart = CONVERT(SMALLDATETIME,'01/' + LTRIM(STR(MONTH(@StartDate))) + '/' + LTRIM(STR(YEAR(@StartDate))))
SET @WhereDateEnd = DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,@EndDate)+1,0))-1
-- we want each date/month minus six months for filter
SELECT -- Set up columns and graph output.
LEFT(Products.ProductId,14) AS ChartLabel,
Products.ProductId,
CONVERT(INT, SUM(ManufacturingOrders.QuantityMade)) AS QuantityMade
,
CONVERT(CHAR(10),
LTRIM(STR(YEAR(ManufacturingOrders.ReleaseDate))) + '/' +
LTRIM(STR(MONTH(ManufacturingOrders.ReleaseDate)))) AS ReleaseDate -- trim dates
FROM
ManufacturingOrders
INNER JOIN Products ON ManufacturingOrders.Product = Products.Product -- Get the product type
INNER JOIN Classifications ON Products.Classification = Classifications.Classification -- Get the Product number
WHERE
Products.ProductId IN (
SELECT Products.ProductId
-- sales code
FROM Products INNER JOIN Classifications ON Products.Classification = Classifications.Classification
WHERE Classifications.ClassificationId = 'BOOK'
)
AND ManufacturingOrders.ReleaseDate BETWEEN @WhereDateStart AND @WhereDateEnd
AND ManufacturingOrders.SystemType = 'B'
AND QuantityMade > 0
GROUP BY
Products.ProductId
,
YEAR(ManufacturingOrders.ReleaseDate)
,
MONTH(ManufacturingOrders.ReleaseDate)
ORDER BY
ReleaseDate
,
ProductId
but its everything for each month, I just want the top 4 for each month to be sent to the graph.
Comment