简体   繁体   English

DB2 SQL 查询对列求和并减去列

[英]DB2 SQL query to SUM the column and Subtract column

I have TRAN_AM (Transaction amount) and DB_CRD_ID (Debit Credit ind).我有TRAN_AM (交易金额)和DB_CRD_ID (借记贷记 ind)。 I have to SUM the TRAN_AM based on the credit debit indicator in a single query.我必须在单个查询中根据贷记借记指示对 TRAN_AM 求和。

Input:输入:

Acct     TRAN_AM     DB_CRD_ID
AAAAAA    10             D 
AAAAAA    10             C
AAAAAA    10             D   

My output should be 10, D. Also have to manage negative if Credit is higher.我的 output 应该是 10,D。如果信用较高,也必须管理负数。

This query will be used as subquery, As I have to compare this amount with the another table.此查询将用作子查询,因为我必须将此数量与另一个表进行比较。

Thanks in advance.提前致谢。

select SUM(TRAN_AM * 
        case DB_CRD_ID 
            when 'D' THEN 1
            when 'C' THEN -1
        END
       ) 
 from TABLE;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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