简体   繁体   中英

Calculate running balance

I found below query to calculate running balance.

SELECT intId, varName, decAmt, charCrDr, 
       SUM(CASE WHEN charCrDr = 'c' THEN
               decAmt
           ELSE
              decAmt * -1
           END )
           OVER (PARTITION BY varName ORDER BY intId) As decTotal
FROM #Temp;

I would like to know what it means by else condition

这是执行SUM或DIFFERENCE操作的粗略方法:在这里,您要尝试找到decAmt的不同之处,如下所示:

decAmt(where charCrDr = 'c') - decAmt 

decAmt * -1decAmt * -1的值乘以-1,这样,如果它为正数,它将变为负数;如果为负数,它将变为正数。

Actually it's about the Credit(Cr) and Debit(Dr). The running balance is:

sum of Credits - (sum of Debits)

So the decAmt for debit records (in the ELSE part, that is WHEN charCrDr != 'c' ) are multiplied by -1 to maintain the above formula.

Good Luck.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM