This is my requirement.
PDay Query_width Sal Sum of last '3' rows inclusive
9/13/2013 3 10 10
9/14/2013 3 20 30
9/15/2013 3 30 60
9/16/2013 3 40 90
9/17/2013 3 50 120
9/18/2013 3 60 150
9/19/2013 3 70 180
The issue here is the value query_width can change each time. Hence could I use the col name instead of hardcode value 3?
Please share your thoughts.
I doubt you can use the column name instead of the width.
However, the use of MSUM is discouraged in Teradata, and you should prefer to use SUM window function...
SUM(SAL) OVER(ORDER BY PDAY ROWS 2 PRECEDING)