我写了一个查询来显示客户ID,登录ID和客户信用债务金额之和。如果要从中扣除客户到期金额,则必须创建虚拟列,并从客户其他客户处显示为“到期”。请帮我解决一下这个问题。
我将表格结构和值与我的查询一起附加。
- 客户表
CREATE TABLE isbs_customer_mst
(
cust_id VARCHAR2(30) NOT NULL,
login_id VARCHAR2(30) NOT NULL,
cust_nm VARCHAR2(30),
cust_addr VARCHAR2(300),
CONSTRAINT isbs_customer_mst_pk PRIMARY KEY (cust_id)
);
- 值
INSERT INTO ISBS_CUSTOMER_MST (CUST_ID, LOGIN_ID, CUST_NM, CUST_ADDR)
VALUES ('CUST0000000001', 'USER1', 'User Login ID 1', '143/1 Uthamar Gandhi Salai, Nungambakkam, Chennai - 34');
INSERT INTO ISBS_CUSTOMER_MST (CUST_ID, LOGIN_ID, CUST_NM, CUST_ADDR)
VALUES ('CUST0000000002', 'USER2', 'User Login ID 2', '143/2 Uthamar Gandhi Salai, Nungambakkam, Chennai - 34');
INSERT INTO ISBS_CUSTOMER_MST (CUST_ID, LOGIN_ID, CUST_NM, CUST_ADDR)
VALUES ('CUST0000000003', 'USER3', 'User Login ID 3', '143/3 Uthamar Gandhi Salai, Nungambakkam, Chennai - 34');
INSERT INTO ISBS_CUSTOMER_MST (CUST_ID, LOGIN_ID, CUST_NM, CUST_ADDR)
VALUES ('CUST0000000004', 'USER4', 'User Login ID 4', '143/4 Uthamar Gandhi Salai, Nungambakkam, Chennai - 34');
- 信用借记表
CREATE TABLE isbs_acct_ledger_det
(
acct_ledger_id VARCHAR2(30),
cust_id VARCHAR2(30),
credit_debit_amt VARCHAR2(30) NOT NULL,
credit_debit_dttm TIMESTAMP NOT NULL,
CONSTRAINT isbs_acct_ledger_det_pk PRIMARY KEY (acct_ledger_id),
CONSTRAINT isbs_acct_ledger_det_fk FOREIGN KEY (cust_id) REFERENCES
isbs_customer_mst (cust_id)
);
- 值
INSERT INTO ISBS_ACCT_LEDGER_DET (ACCT_LEDGER_ID, CUST_ID, CREDIT_DEBIT_AMT, CREDIT_DEBIT_DTTM)
VALUES ('ACC0000000001', 'CUST0000000001', -1000.25, TO_DATE('01-10-2008 11:00:00', 'DD-MM-YYYY HH24:MI:SS'));
INSERT INTO ISBS_ACCT_LEDGER_DET (ACCT_LEDGER_ID, CUST_ID, CREDIT_DEBIT_AMT, CREDIT_DEBIT_DTTM)
VALUES ('ACC0000000002', 'CUST0000000002', -256.75, TO_DATE('01-10-2008 11:00:00', 'DD-MM-YYYY HH24:MI:SS'));
INSERT INTO ISBS_ACCT_LEDGER_DET (ACCT_LEDGER_ID, CUST_ID, CREDIT_DEBIT_AMT, CREDIT_DEBIT_DTTM)
VALUES ('ACC0000000003', 'CUST0000000002', 100.25, TO_DATE('05-10-2008 11:00:00', 'DD-MM-YYYY HH24:MI:SS'));
- 查询
SELECT c.CUST_NM
, c.LOGIN_ID
, SUM(a.CREDIT_DEBIT_AMT) "Outstanding Amt"
, CASE WHEN a.CREDIT_DEBIT_AMT <= -9999.99
THEN 'Due to Cust'
ELSE 'Due from Cust' END "Due"
FROM ISBS_CUSTOMER_MST c
JOIN ISBS_ACCT_LEDGER_DET a
ON c.CUST_ID = a.CUST_ID
GROUP BY c.CUST_NM, c.LOGIN_ID, a.CREDIT_DEBIT_AMT;
提前致谢
答案 0 :(得分:2)
您可能需要从a.CREDIT_DEBIT_AMT
移除group by
并在CASE
上使用SUM()
表达。
SELECT c.cust_nm,
c.login_id,
SUM(a.credit_debit_amt) "Outstanding Amt",
CASE
WHEN SUM(a.credit_debit_amt) <= -9999.99 THEN 'Due to Cust'
ELSE 'Due from Cust'
END "Due"
FROM isbs_customer_mst c
JOIN isbs_acct_ledger_det a
ON c.cust_id = a.cust_id
GROUP BY c.cust_nm,
c.login_id ;
答案 1 :(得分:0)
您需要在group by子句中添加case语句:
SELECT c.CUST_NM , c.LOGIN_ID , SUM(a.CREDIT_DEBIT_AMT) "Outstanding Amt" , CASE WHEN a.CREDIT_DEBIT_AMT <= -9999.99 THEN 'Due to Cust' ELSE 'Due from Cust' END "Due" FROM ISBS_CUSTOMER_MST c JOIN ISBS_ACCT_LEDGER_DET a ON c.CUST_ID = a.CUST_ID GROUP BY c.CUST_NM, c.LOGIN_ID, CASE WHEN a.CREDIT_DEBIT_AMT <= -9999.99 THEN 'Due to Cust' ELSE 'Due from Cust' END "Due";
因为聚合函数的规则是在group by子句中包含所有其他所选属性
答案 2 :(得分:0)
为什么在处理总和时会使用sin函数。 Sin给出参数的sin值 而abs则返回正数,同样
Select abs(-5) from dual ;
将输出5作为输出。
我认为你的要求不需要这两种功能。