I want to get report data in below scenarios using the sample table provided below(data is huge in my db)
- List item(same week, prior year) sales and (same day, same week, prior year) sales.
- Rolling 6 month avg weekly selling
id | date_week | date_value | sales |
---|---|---|---|
Item1 | 2020/01-04 | 20200120 | 230 |
Item2 | 2020/06-03 | 20200608 | 23.0 |
Item3 | 2019/11-03 | 20191111 | null |
Item4 | 2020/07-04 | 20200720 | 123 |
Item5 | 2019/08-01 | 20190729 | 456 |
Item6 | 2019/09-03 | 20190909 | 1234 |
Item7 | 2020/06-02 | 20200601 | 4556 |
Item8 | 2020/09-01 | 20200824 | 23 |
Item9 | 2021/09-02 | 20210906 | 1223 |
in above table date_week is year/month_week ( so here i get the week number)
Am trying the below query to achieve
SELECT
DATEPART(week, date_value) AS Week,
id ,
sum(sales) AS sales
FROM table
WHERE date_value <= date_value
AND date_value < DATEADD(year, 1, date_value)
GROUP BY DATEPART(week, date_value), id
ORDER BY DATEPART(week, date_value);
Please suggest me how to achieve the scenarios am looking for.