Use a calendar table, and simply join the date to the calendar table and group by the year/month.
https://weblogs.sqlteam.com/dang/2010/07/19/calendar-table-and-datetime-functions/
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I have a table like a sales table .
date store staff product qty
2022-01-01 01 01 001 1
2022-01-01 01 02 001 2
2022-01-02 01 01 001 1
2022-01-02 01 02 003 2
I use sum(QTY) over partition by datemon,store,staff,product order by date asc rows between 31 preceding and current row) . .. and give a table like
date store staff product qty MTDQTY
2022-01-01 01 01 001 1 1
2022-01-01 01 02 001 2 2
2022-01-02 01 01 001 1 2
2022-01-02 01 02 003 2 2
if I want to get qty and mtdqty of the whole store , and save to the same table , is it possible ?
Use a calendar table, and simply join the date to the calendar table and group by the year/month.
https://weblogs.sqlteam.com/dang/2010/07/19/calendar-table-and-datetime-functions/
Your question is not entirely clear, and you did not provide CREATE TABLE + INSERT, making it difficult to test. But this may be what you are looking for:
; WITH CTE AS
SELECT MTDQTY,
newvalue sum(QTY) over partition by datemon,store,staff,product
order by date asc rows between 31 preceding and current row)
FROM tbl
)
UPDATE CTE
SET MTDQTY = newvalue