MSUM - use column name for query width

Database
Enthusiast

MSUM - use column name for query width

Hi All,

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

MSUM(sal,3,PDay)

The issue here is the value query_width can change each time. Hence could I use the col name instead of hardcode value 3?

MSUM(sal,query_width,PDay)

Please share your thoughts.

Regards,

Sam

1 REPLY
Enthusiast

Re: MSUM - use column name for query width

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)