Wednesday, April 1, 2015

Summing by row number

Wow... It's amazing what you see when you give your own code a 2nd look...


This will do the same thing in a much more concise manner and fewer passes at the source table.



SELECT
tt.ordNum AS ordNum,
tt.ordType,
ttx.RunningTotalDelq AS dlqAmt,
ttx.RunningTotalPaid AS pdAmt,
tt.rcvdDt
FROM
@tmpTbl tt
CROSS APPLY (
SELECT
SUM(tt2.dlqAmt) AS RunningTotalDelq,
SUM(tt2.pdAmt) AS RunningTotalPaid
FROM @tmpTbl tt2
WHERE tt.rcvdDt >= tt2.rcvdDt
) ttx
WHERE
tt.dlqAmt > 0



HTH,


Jason




Jason Long


No comments:

Post a Comment