我在一家银行工作,我试图为我们的一些交易编写查询,每笔交易都作为交易金额(TXN_AMOUNT_BASE)输入,如果是贷方或借方(CREDIT_DEBIT_CODE),则稍后在另一列中进行标识。
长期目标是获得一个查询,该查询告诉您每个客户的贷方比率。
立即目标是将金额简单地分为“贷方”列或“借方”列。这是我到目前为止的内容:
SELECT
PRIMARY_CUST_SRCE_REF_ID,
TXN_AMOUNT_BASE,
FROM
IDP_INTERFACE.V_L3_HPT_TRANSACTIONS
WHERE
CREDIT_DEBIT_CODE = 'C' AS Credit
AND CREDIT_DEBIT_CODE = 'D' AS Debit
GROUP BY
(PRIMARY_CUST_SRCE_REF_ID, TXN_AMOUNT_BASE, CREDIT_DEBIT_CODE)
我知道我在WHERE C_D_CODE AS段上错了,但是我不知道从哪里开始进行更正。
谢谢
答案 0 :(得分:1)
我认为您需要条件聚合:
SELECT PRIMARY_CUST_SRCE_REF_ID,
SUM(CASE WHEN CREDIT_DEBIT_CODE = 'C' THEN TXN_AMOUNT_BASE ELSE 0 END) AS Credit,
SUM(CASE WHEN CREDIT_DEBIT_CODE = 'D' THEN TXN_AMOUNT_BASE ELSE 0 END) AS Debit
FROM IDP_INTERFACE.V_L3_HPT_TRANSACTIONS
WHERE CREDIT_DEBIT_CODE IN ('C', 'D')
GROUP BY PRIMARY_CUST_SRCE_REF_ID;
这将为您提供每个客户参考的贷项和借项之和。如果需要比率,则将其除。