'DB2 sum over dynamic batch of rows
I'm working on a project that involves building an automated tool for our pricing team to look at the effects of their pricing changes on demand. I'm writing in Python and using SQL to query our DB2 data sources.
The idea is to allow the pricing team to tell the tool the line they want to check and the week number that they made a price change (week_id in the form 202219 in case it is needed), the program will then calculate the number of completed weeks between runtime and the price change to determine how many weeks before and after the price change to return for comparison (this variable is called deltaWeek).
My thought right now is to use a CTE to calculate the total demand by week, then I want to reference that CTE to gather week_id batches the size of deltaWeek and SUM() the total quantity for each batch.
I have the CTE query working, and the output of the query is good, and assuming the week of the price change was week 12, deltaWeek = 6, and the quantity is all the same (which it isn't in reality, but it makes it easy) a condensed output looks like this (it excludes the week of the price change on purpose)
| ROW_NUM | WEEK_ID | QUANTITY |
|---|---|---|
| 1 | 201906 | 10 |
| 2 | 201907 | 10 |
| 3 | 201908 | 10 |
| 4 | 201909 | 10 |
| 5 | 201910 | 10 |
| 6 | 201911 | 10 |
| 7 | 201913 | 10 |
| 8 | 201914 | 10 |
| 9 | 201915 | 10 |
| 10 | 201916 | 10 |
| 11 | 201917 | 10 |
| 12 | 201918 | 10 |
Is there a way in DB2 to reference this CTE and return something that would look like this
| BATCH | QUANTITY |
|---|---|
| 1 | 60 |
| 2 | 60 |
where BATCH 1 represents SUM(QUANTITY) for ROW_NUM 1-6 FROM WEEKLY_TOTALS_CTE and BATCH 2 is similar for ROW_NUM 7-12
More generally, because deltaWeek, and thus the number of weeks in any given batch will depend on when the tool is ran, I need to total from ROW 1 - deltaWeek, then deltaWeek+1 - deltaWeek*2, etc.. I have working python functions to make SQL templates using parameters so I can pass deltaWeek into the query if I can figure out the logic to make this query work.
If this is a terrible idea to try to make work, I guess I can just run the query using pd.read_sql and then use iloc[] to do the batch aggregation, but I feel like it should be able to be done all in the query, maybe?
Thank you for any help/reference.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
