Hello gurus, I have a question about doing a balance sheet in CTE style.CREATE TABLE TEST1(ID INT, CDTYPE CHAR(1), INITAMT INT, AMT INT)INSERT INTO TEST1select 1,'C',100,10UNION ALLselect 2,'D',100,20UNION ALLselect 3,'D',100,30UNION ALLselect 4,'C',100,15UNION ALLselect 5,'D',100,20ID CDTYPE INITAMT AMT1 C 100 102 D 100 203 D 100 304 C 100 155 D 100 20
I want to run a CTE sql to recursively calculate the cumulative amount as current amount, if CDTYPE is C then add the amount to the cumulative amount; if D then subtract from the cumulative amount:ID CDTYPE INITAMT CURRENTAMT1 C 100 1102 D 100 903 D 100 604 C 100 755 D 100 55
Thanks in advance!