Issue Calculating from Rows and Columns(Summing two columns with the third of a different row)

Posted by vstsdev on Stack Overflow See other posts from Stack Overflow or by vstsdev
Published on 2012-12-16T09:07:10Z Indexed on 2012/12/16 17:04 UTC
Read the original article Hit count: 373

With reference to my previous question Adding columns resulting from GROUP BY clause

SELECT AcctId,Date,
   Sum(CASE
         WHEN DC = 'C' THEN TrnAmt
         ELSE 0
       END) AS C,
   Sum(CASE
         WHEN DC = 'D' THEN TrnAmt
         ELSE 0
       END) AS D
FROM   Table1 where AcctId = '51'
GROUP  BY AcctId,Date
ORDER  BY AcctId,Date

I executed the above query and got my desired result..

  AcctId       Date         C        D
    51       2012-12-04   15000      0
    51       2012-12-05   150000  160596
    51       2012-12-06    600        0

now I have a another operation to do on the same query i.e.

I need the result to be like this

  AcctId   Date                                Result

    51       2012-12-04    (15000-0)->       15000  
    51       2012-12-05   (150000-160596) + (15000->The first value)  4404
    51       2012-12-06     600-0         +(4404 ->The calculated 2nd value) 5004

Is it possible with the same query??.

© Stack Overflow or respective owner

Related posts about sql

Related posts about sql-server