That makes a little more sense. ;)
Give this a try...
ECLARE @tmpTbl as table
(
ordNum varchar(10),
ordType varchar(4),
dlqAmt numeric(5,2),
pdAmt numeric(5,2),
rcvdDt datetime
)
INSERT @tmpTbl
VALUES
('5','ORG',0.00,101.32,'12/1/2013'),
('8','ADD',0.00,41.12,'12/18/2013'),
('11','ADD',32.17,17.19,'1/13/2014'),
('21','ADD',0.00,31.01,'1/28/2014'),
('92','ADD',0.00,0.92,'2/13/2014'),
('108','ADD',0.19,92.42,'2/21/2014'),
('317','ADD',0.00,13.11,'3/3/2014'),
('1010','ADD',0.00,4.08,'3/17/2014'),
('1713','ADD',0.00,11.06,'4/1/2014'),
('3010','ADD',22.97,2.19,'4/16/2014')
;WITH Delq AS (
SELECT
tt.*,
SUM(tt.dlqAmt) OVER (ORDER BY tt.rcvdDt ROWS UNBOUNDED PRECEDING) RunningTotal
FROM
@tmpTbl tt
WHERE
tt.dlqAmt > 0
)
SELECT
d.ordNum,
d.ordType,
d.RunningTotal AS dlqAmt,
pa.pdAmt,
d.rcvdDt
FROM
Delq d
CROSS APPLY (
SELECT
SUM(tt.pdAmt) AS pdAmt
FROM
@tmpTbl tt
WHERE
tt.rcvdDt <= d.rcvdDt
) pa
HTH,
Jason
Jason Long
No comments:
Post a Comment