I've been having one of those weeks...
I am creating a simple POC for the Director, who is looking for weekly stats of order values shipping.
The scenario they asked is "At the end of each week (Sunday), I want to see the orders total ($), for the next 52 Weeks, based on the shipping date".
Standard SCD2 schemas, expanded below. Simple Date Dimension.
This is simple to answer in its basic form, DIMDATE.DATEKEY_SK = ORDERS.ORDERDATE_SK WHERE ORDERS.CURRENTRECORD ='Y' GROUP BY DIMDATE.WEEKEND. Which gives me all required DIMDATE attributes e.g. WeekStart, WeekEnd, MonthStart, MonthEnd etc etc.
However, the Director has now said he would like to see the previous 12 weeks as well, (-12/+52) in order to compare week on week trends.
Now i could insert all data from the "simple" query into a snapshot table each week which would solve the problem BUT i feel this is the cheats way out and there must be a way to do the same through T-SQL. This is where i am stuck because i have [EffectiveDate] and [EndDate] in my ORDERS table, along with the [OrderShipDate_SK].
Example Schemas Below:
DIMDATE
DATEKEY_SK | WeekStart | WeekEnd |
---|---|---|
20230101 | 2023/01/01 | 2023/01/07 |
20250131 | 2023/01/29 | 2023/02/04 |
ORDERS
ORDERKEY_SK | EffectiveDate | EndDate | OrderId | OrderAmount | OrderShipDate_SK | CurrentRecord |
---|---|---|---|---|---|---|
1234 | 2023/01/01 | 2023/01/03 | IF76876 | 35000 | 20230107 | N |
1234 | 2023/01/04 | 2023/01/08 | IF76876 | 37000 | 20230107 | N |
3345 | 2023/01/09 | 2999/12/31 | IF76876 | 40000 | 20230114 | Y |
1432 | 2023/01/01 | 2023/01/12 | IF89996 | 10000 | 20230107 | N |
5456 | 2023/01/13 | 2999/12/31 | IF89996 | 12000 | 20230114 | Y |
1932 | 2023/01/01 | 2023/01/22 | IF62336 | 10000 | 20230131 | N |
7456 | 2023/01/23 | 2999/12/31 | IF62336 | 12000 | 20230218 | Y |
What I'm after is like the below:
WeekStart | WeekEnd | OrderTotal | ShippingWeekEnd (OrderShipDate) |
---|---|---|---|
2023/01/01 | 2023/01/07 | 47000 | 2023/01/07 |
2023/01/01 | 2023/01/07 | 0 | 2023/01/14 |
2023/01/01 | 2023/01/07 | 0 | 2023/01/21 |
2023/01/01 | 2023/01/07 | 0 | 2023/01/28 |
2023/01/01 | 2023/01/07 | 10000 | 2023/02/04 |
2023/01/08 | 2023/01/14 | 52000 | 2023/01/14 |
2023/01/08 | 2023/01/14 | 0 | 2023/01/21 |
2023/01/08 | 2023/01/14 | 0 | 2023/01/28 |
2023/01/08 | 2023/01/14 | 10000 | 2023/02/04 |
2023/01/15 | 2023/01/21 | 0 | 2023/01/21 |
2023/01/15 | 2023/01/21 | 0 | 2023/01/28 |
2023/01/15 | 2023/01/21 | 10000 | 2023/02/04 |
2023/01/15 | 2023/01/21 | 0 | 2023/02/11 |
2023/01/22 | 2023/01/28 | 12000 | 2023/01/28 |
2023/01/22 | 2023/01/28 | 0 | 2023/02/04 |
2023/01/22 | 2023/01/28 | 0 | 2023/02/11 |
2023/01/22 | 2023/01/28 | 12000 | 2023/02/18 |
I want to show (64) rows for each week start/end with the sum of orders, grouped by the shipping week end (always Saturdays) E.g. If the order changes mid week only the latest record during that week is factored in at the end of the week.
I just cant figure out how i need to construct the query to do this. Existing Query Below
SELECT SUM(OrderTotal),DIMDATE.WeekEnd
FROM ORDERS
LEFT JOIN DIMDATE ON DIMDATE.DATEKEY_SK = ORDERS.OrderShipDate_SK
WHERE ORDERS.CurrentRecord = 'Y'
GROUP BY DIMDATE.WeekEnd DESC