Fiscal Week Quantity Current_quantity
2024011 1000 0
2024012 2000 1000
2024013 0 2000
2024014 3000 0
2024015 0 3000
Offset = 1
I need help on How to achieve the above scenario
example if offset = 1 then the Current_quantity column should move by 1 based upon the Quantity column . For Fiscalweek 2024011 Quantity 1000 Current_quantity 0 , FiscalWeek 2024012 Quantity 2000 Current_quantity 1000. Since 1000 was the Quantity for fiscalweek 2024011 this became the current_quantity for fiscalweek 2024012.
Offset value can be anything between 0 – 8 .
If offset is 2 then Current_Quantity for 2024013 will be 1000 and so on.
Offset is a column from another table . I am joining the table with offset table for getting offset value.
I tried the below query
SELECT
FiscalWeek,
Quantity,
LAG(Quantity, Offset) OVER (ORDER BY FiscalWeek) AS Current_quantity
FROM
YourTableName
ORDER BY
FiscalWeek;
but getting error function LAG needs to be constant, found ‘OFFSET’
Mithun Alinkil is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1