Hi,
This is a bit long, sorry.
I've been stuck for days trying to get a specific result set and I hope somebody can help.
The results I'm after look simple:
Category, OrderValue, PrevMonth, Date
I need the results to show ALL categories for ALL days and populate a value in the OrderValue and/or PrevMonth fields if there was a sale on that day in that category, otherwise it should be NULL.
The PrevMonth value is the OrderValue one month prior to the Date shown in the results. To get the PrevMonth value to work correctly, I've created a Calendar table with every date from 2005 to 2010 (to cover the range of data). My first query returns every date in the Calendar with an OrderValue for each Category that was sold on that day. If there wasn't a sale on a day then it wont show the Category.
Sample of results from Query1:
Date, OrderValue, Category
2009-09-04, 49.9900, CAT1
2009-09-04, 42.9700, CAT2
2009-09-04, .0000,CAT3
2009-09-04, 446.4500, CAT4
2009-09-04, 35.9600, CAT5
2009-09-04, 183.9700, CAT6
2009-09-04, 299.9900, CAT7
2009-09-04, .0000,NULL
2009-09-04, 7679.6400, CAT8
2009-09-04, 290.4500, CAT9
2009-09-05, NULL,NULL
2009-09-06, NULL,NULL
2009-09-07, 20.9800, CAT6
2009-09-07, 199.9900, CAT10
2009-09-07, 54.9800, CAT2
2009-09-07, 49.9900, CAT1
2009-09-07, 24.9900, CAT5
2009-09-07, 475.4000, CAT9
2009-09-07, 12929.4000, CAT8
2009-09-07, 682.2900, CAT4
2009-09-07, .0000, NULL
2009-09-07, 6.9500, CAT3
2009-09-07, 209.9800, CAT7
My second query uses the results of the first query and performs a self join on the Date field in order to return the PrevMonth value, but it then has to join on the Category as well in order for the correct value to display by the correct Date and Category. This is where it falls over because there might not always be a Category for the prior month if there were no sales on that day.
Sample of results from Query2 (missing days 2009-09-05 & 2009-09-06):
2009-09-04, 49.9900, CAT1
2009-09-04, 42.9700, CAT2
2009-09-04, .0000, CAT3
2009-09-04, 446.4500, CAT4
2009-09-04, 35.9600, CAT5
2009-09-04, 183.9700, CAT6
2009-09-04, 299.9900, CAT7
2009-09-04, 7679.6400, CAT8
2009-09-04, 290.4500, CAT9
2009-09-07, 49.9900, CAT1
2009-09-07, 54.9800, CAT2
2009-09-07, 6.9500, CAT3
2009-09-07, 682.2900, CAT4
2009-09-07, 24.9900, CAT5
2009-09-07, 20.9800, CAT6
2009-09-07, 209.9800, CAT7
2009-09-07, 12929.4000, CAT8
2009-09-07, 475.4000, CAT9
2009-09-07, 199.9900, CAT10
To get the results I want I think I need to get all the Categories to show next to every date in Query1 whether there was a sale or not.
Something like this:
Date, OrderValue, Category
2009-09-04, 49.9900, CAT1
2009-09-04, 42.9700, CAT2
2009-09-04, .0000,CAT3
2009-09-04, 446.4500, CAT4
2009-09-04, 35.9600, CAT5
2009-09-04, 183.9700, CAT6
2009-09-04, 299.9900, CAT7
2009-09-04, 7679.6400, CAT8
2009-09-04, 290.4500, CAT9
2009-09-04, 90.1300, CAT10
2009-09-05, NULL, CAT1
2009-09-05, NULL, CAT2
2009-09-05, NULL, CAT3
2009-09-05, NULL, CAT4
2009-09-05, NULL, CAT5
2009-09-05, NULL, CAT6
2009-09-05, NULL, CAT7
2009-09-05, NULL, CAT8
2009-09-05, NULL, CAT9
2009-09-05, NULL, CAT10
2009-09-06, NULL, CAT1
2009-09-06, NULL, CAT2
2009-09-06, NULL, CAT3
2009-09-06, NULL, CAT4
2009-09-06, NULL, CAT5
2009-09-06, NULL, CAT6
2009-09-06, NULL, CAT7
2009-09-06, NULL, CAT8
2009-09-06, NULL, CAT9
2009-09-06, NULL, CAT10
2009-09-07, 49.9900, CAT1
2009-09-07, 54.9800, CAT2
2009-09-07, 6.9500, CAT3
2009-09-07, 682.2900, CAT4
2009-09-07, 24.9900, CAT5
2009-09-07, 20.9800, CAT6
2009-09-07, 209.9800, CAT7
2009-09-07, 12929.4000, CAT8
2009-09-07, 475.4000, CAT9
2009-09-07, 199.9900, CAT10
Query2 should then work shouldn't it?.
How can I achieve these results above? I've tried all sorts of joins to no avail.
The code of my queries:
Query 1 (dbo.usr_vw_DespatchedOrdersByCategory2):
SELECT TOP 100 PERCENT DATEADD(dd, 0,DATEDIFF(dd, 0, Cal.CalDate)) as CalDate, SUM(oih.GrossValue) AS OrderValue,
Cat.Category
FROM dbo.CategoryCodes Cat RIGHT OUTER JOIN
dbo.Stock ON Cat.CategoryCode = dbo.Stock.CategoryCode RIGHT OUTER JOIN
dbrderItemHeader oih ON dbo.Stock.Sku = oih.Sku RIGHT OUTER JOIN
(SELECT CalDate
FROM Dashboard.dbo.usr_Calendar
WHERE (CalDate < GETDATE())) Cal ON DATEADD(dd, 0, DATEDIFF(dd, 0, oih.ItemDate)) = Cal.CalDate
GROUP BY DATEADD(dd, 0, DATEDIFF(dd,0, Cal.CalDate)), Cat.Category
ORDER BY DATEADD(dd, 0, DATEDIFF(dd, 0, Cal.CalDate))
Query2:
SELECT TOP 100 PERCENT Cat.Category, SUM(Curr.OrderValue) AS OrderValue, SUM(Prev.OrderValue) AS PrevMonth,
Curr.CalDate
FROM CategoryCodes Cat LEFT JOIN dbo.usr_vw_DespatchedOrdersByCategory2 Curr ON Cat.Category = Curr.Category LEFT OUTER JOIN
dbo.usr_vw_DespatchedOrdersByCategory2 Prev ON DateAdd(m,-1,Curr.[Date]) = Prev.[Date] AND
Prev.Category = Cat.Category
WHERE Cat.Type = 3
GROUP BY ALL Curr.CalDate, Cat.Category
ORDER BY Curr.CalDate, Cat.Category
I hope somebody can understand all that. Any guidance appreciated.
Thanks
This is a bit long, sorry.
I've been stuck for days trying to get a specific result set and I hope somebody can help.
The results I'm after look simple:
Category, OrderValue, PrevMonth, Date
I need the results to show ALL categories for ALL days and populate a value in the OrderValue and/or PrevMonth fields if there was a sale on that day in that category, otherwise it should be NULL.
The PrevMonth value is the OrderValue one month prior to the Date shown in the results. To get the PrevMonth value to work correctly, I've created a Calendar table with every date from 2005 to 2010 (to cover the range of data). My first query returns every date in the Calendar with an OrderValue for each Category that was sold on that day. If there wasn't a sale on a day then it wont show the Category.
Sample of results from Query1:
Date, OrderValue, Category
2009-09-04, 49.9900, CAT1
2009-09-04, 42.9700, CAT2
2009-09-04, .0000,CAT3
2009-09-04, 446.4500, CAT4
2009-09-04, 35.9600, CAT5
2009-09-04, 183.9700, CAT6
2009-09-04, 299.9900, CAT7
2009-09-04, .0000,NULL
2009-09-04, 7679.6400, CAT8
2009-09-04, 290.4500, CAT9
2009-09-05, NULL,NULL
2009-09-06, NULL,NULL
2009-09-07, 20.9800, CAT6
2009-09-07, 199.9900, CAT10
2009-09-07, 54.9800, CAT2
2009-09-07, 49.9900, CAT1
2009-09-07, 24.9900, CAT5
2009-09-07, 475.4000, CAT9
2009-09-07, 12929.4000, CAT8
2009-09-07, 682.2900, CAT4
2009-09-07, .0000, NULL
2009-09-07, 6.9500, CAT3
2009-09-07, 209.9800, CAT7
My second query uses the results of the first query and performs a self join on the Date field in order to return the PrevMonth value, but it then has to join on the Category as well in order for the correct value to display by the correct Date and Category. This is where it falls over because there might not always be a Category for the prior month if there were no sales on that day.
Sample of results from Query2 (missing days 2009-09-05 & 2009-09-06):
2009-09-04, 49.9900, CAT1
2009-09-04, 42.9700, CAT2
2009-09-04, .0000, CAT3
2009-09-04, 446.4500, CAT4
2009-09-04, 35.9600, CAT5
2009-09-04, 183.9700, CAT6
2009-09-04, 299.9900, CAT7
2009-09-04, 7679.6400, CAT8
2009-09-04, 290.4500, CAT9
2009-09-07, 49.9900, CAT1
2009-09-07, 54.9800, CAT2
2009-09-07, 6.9500, CAT3
2009-09-07, 682.2900, CAT4
2009-09-07, 24.9900, CAT5
2009-09-07, 20.9800, CAT6
2009-09-07, 209.9800, CAT7
2009-09-07, 12929.4000, CAT8
2009-09-07, 475.4000, CAT9
2009-09-07, 199.9900, CAT10
To get the results I want I think I need to get all the Categories to show next to every date in Query1 whether there was a sale or not.
Something like this:
Date, OrderValue, Category
2009-09-04, 49.9900, CAT1
2009-09-04, 42.9700, CAT2
2009-09-04, .0000,CAT3
2009-09-04, 446.4500, CAT4
2009-09-04, 35.9600, CAT5
2009-09-04, 183.9700, CAT6
2009-09-04, 299.9900, CAT7
2009-09-04, 7679.6400, CAT8
2009-09-04, 290.4500, CAT9
2009-09-04, 90.1300, CAT10
2009-09-05, NULL, CAT1
2009-09-05, NULL, CAT2
2009-09-05, NULL, CAT3
2009-09-05, NULL, CAT4
2009-09-05, NULL, CAT5
2009-09-05, NULL, CAT6
2009-09-05, NULL, CAT7
2009-09-05, NULL, CAT8
2009-09-05, NULL, CAT9
2009-09-05, NULL, CAT10
2009-09-06, NULL, CAT1
2009-09-06, NULL, CAT2
2009-09-06, NULL, CAT3
2009-09-06, NULL, CAT4
2009-09-06, NULL, CAT5
2009-09-06, NULL, CAT6
2009-09-06, NULL, CAT7
2009-09-06, NULL, CAT8
2009-09-06, NULL, CAT9
2009-09-06, NULL, CAT10
2009-09-07, 49.9900, CAT1
2009-09-07, 54.9800, CAT2
2009-09-07, 6.9500, CAT3
2009-09-07, 682.2900, CAT4
2009-09-07, 24.9900, CAT5
2009-09-07, 20.9800, CAT6
2009-09-07, 209.9800, CAT7
2009-09-07, 12929.4000, CAT8
2009-09-07, 475.4000, CAT9
2009-09-07, 199.9900, CAT10
Query2 should then work shouldn't it?.
How can I achieve these results above? I've tried all sorts of joins to no avail.
The code of my queries:
Query 1 (dbo.usr_vw_DespatchedOrdersByCategory2):
SELECT TOP 100 PERCENT DATEADD(dd, 0,DATEDIFF(dd, 0, Cal.CalDate)) as CalDate, SUM(oih.GrossValue) AS OrderValue,
Cat.Category
FROM dbo.CategoryCodes Cat RIGHT OUTER JOIN
dbo.Stock ON Cat.CategoryCode = dbo.Stock.CategoryCode RIGHT OUTER JOIN
dbrderItemHeader oih ON dbo.Stock.Sku = oih.Sku RIGHT OUTER JOIN
(SELECT CalDate
FROM Dashboard.dbo.usr_Calendar
WHERE (CalDate < GETDATE())) Cal ON DATEADD(dd, 0, DATEDIFF(dd, 0, oih.ItemDate)) = Cal.CalDate
GROUP BY DATEADD(dd, 0, DATEDIFF(dd,0, Cal.CalDate)), Cat.Category
ORDER BY DATEADD(dd, 0, DATEDIFF(dd, 0, Cal.CalDate))
Query2:
SELECT TOP 100 PERCENT Cat.Category, SUM(Curr.OrderValue) AS OrderValue, SUM(Prev.OrderValue) AS PrevMonth,
Curr.CalDate
FROM CategoryCodes Cat LEFT JOIN dbo.usr_vw_DespatchedOrdersByCategory2 Curr ON Cat.Category = Curr.Category LEFT OUTER JOIN
dbo.usr_vw_DespatchedOrdersByCategory2 Prev ON DateAdd(m,-1,Curr.[Date]) = Prev.[Date] AND
Prev.Category = Cat.Category
WHERE Cat.Type = 3
GROUP BY ALL Curr.CalDate, Cat.Category
ORDER BY Curr.CalDate, Cat.Category
I hope somebody can understand all that. Any guidance appreciated.
Thanks