我有2个部分的联合查询。第1部分计算上个月的付款金额,第2部分计算上个月的付款金额。我使用以下查询
WITH DATA AS(
(SELECT DA.ID, DDO.CODE, NULL AS PAYMENTS, ABS(SUM(FAT.AMOUNT_IN_DEFAULT_CURRENCY)) AS STATEMENTS --, FPP.AMOUNT_IN_DEFAULT_CURRENCY AS PAYMENTS
FROM DIM_ACCOUNT DA
JOIN DIM_DATA_OWNER DDO ON DA.DATA_OWNER_ID = DDO.ID
JOIN FACT_AS_TRANSACTION FAT ON FAT.ACCOUNT_ID = DA.ID
JOIN DIM_DATE DD ON FAT.VALUE_DATE_ID = DD.ID
WHERE DD.CAL_DATE >= TO_DATE('2015-09-20', 'YYYY-MM-DD')
AND FAT.DEBIT_CREDIT_MARK = 'Debit'
GROUP BY DA.ID, DDO.CODE
HAVING SUM(FAT.AMOUNT_IN_DEFAULT_CURRENCY) != 0)
UNION
(SELECT DA.ID, DDO.CODE, ABS(COALESCE(SUM(FPP.AMOUNT_IN_DEFAULT_CURRENCY),0)) AS PAYMENTS, NULL
FROM DIM_ACCOUNT DA
JOIN DIM_DATA_OWNER DDO ON DA.DATA_OWNER_ID = DDO.ID
JOIN FACT_PAY_PAYMENT FPP ON FPP.ORDERING_ACCOUNT_ID = DA.ID
JOIN DIM_PAY_PAYMENT_METHOD DPPM ON DPPM.ID = FPP.PAYMENT_METHOD_ID
JOIN DIM_DATE DD ON FPP.REQUESTED_EXECUTION_DATE_ID = DD.ID
WHERE DD.CAL_DATE >= TO_DATE('2015-09-20', 'YYYY-MM-DD')
GROUP BY DA.ID, DDO.CODE)
)
SELECT * FROM DATA
答案 0 :(得分:1)
如果你真的无法在一个select语句中将它们全部加在一起,那么这个小适应应该有效:
WITH DATA AS(
(SELECT DA.ID, DDO.CODE, 0 AS PAYMENTS, ABS(SUM(FAT.AMOUNT_IN_DEFAULT_CURRENCY)) AS STATEMENTS --, FPP.AMOUNT_IN_DEFAULT_CURRENCY AS PAYMENTS
FROM DIM_ACCOUNT DA
JOIN DIM_DATA_OWNER DDO ON DA.DATA_OWNER_ID = DDO.ID
JOIN FACT_AS_TRANSACTION FAT ON FAT.ACCOUNT_ID = DA.ID
JOIN DIM_DATE DD ON FAT.VALUE_DATE_ID = DD.ID
WHERE DD.CAL_DATE >= TO_DATE('2015-09-20', 'YYYY-MM-DD')
AND FAT.DEBIT_CREDIT_MARK = 'Debit'
GROUP BY DA.ID, DDO.CODE
HAVING SUM(FAT.AMOUNT_IN_DEFAULT_CURRENCY) != 0)
UNION
(SELECT DA.ID, DDO.CODE, ABS(COALESCE(SUM(FPP.AMOUNT_IN_DEFAULT_CURRENCY),0)) AS PAYMENTS, 0 AS STATEMENTS
FROM DIM_ACCOUNT DA
JOIN DIM_DATA_OWNER DDO ON DA.DATA_OWNER_ID = DDO.ID
JOIN FACT_PAY_PAYMENT FPP ON FPP.ORDERING_ACCOUNT_ID = DA.ID
JOIN DIM_PAY_PAYMENT_METHOD DPPM ON DPPM.ID = FPP.PAYMENT_METHOD_ID
JOIN DIM_DATE DD ON FPP.REQUESTED_EXECUTION_DATE_ID = DD.ID
WHERE DD.CAL_DATE >= TO_DATE('2015-09-20', 'YYYY-MM-DD')
GROUP BY DA.ID, DDO.CODE)
)
SELECT ID, CODE, SUM(PAYMENTS), SUM(STATEMENTS) FROM DATA
GROUP BY ID, CODE
答案 1 :(得分:0)
尝试这样的事情,我在一个查询中加入了它。如果您只能在一个表中包含数据,则使用LEFT OUTER JOIN
。
SELECT DA.ID, DDO.CODE, ABS(COALESCE(SUM(FPP.AMOUNT_IN_DEFAULT_CURRENCY),0)) AS PAYMENTS, ABS(SUM(FAT.AMOUNT_IN_DEFAULT_CURRENCY)) AS STATEMENTS
FROM DIM_ACCOUNT DA
JOIN DIM_DATA_OWNER DDO ON DA.DATA_OWNER_ID = DDO.ID
LEFT OUTER JOIN FACT_AS_TRANSACTION FAT ON FAT.ACCOUNT_ID = DA.ID
LEFT OUTER JOIN FACT_PAY_PAYMENT FPP ON FPP.ORDERING_ACCOUNT_ID = DA.ID
LEFT OUTER JOIN DIM_DATE DD1 ON FAT.VALUE_DATE_ID = DD1.ID
LEFT OUTER JOIN DIM_DATE DD2 ON FPP.REQUESTED_EXECUTION_DATE_ID = DD.DD2
LEFT OUTER JOIN DIM_PAY_PAYMENT_METHOD DPPM ON DPPM.ID = FPP.PAYMENT_METHOD_ID
WHERE DD1.CAL_DATE >= TO_DATE('2015-09-20', 'YYYY-MM-DD')
AND DD2.CAL_DATE >= TO_DATE('2015-09-20', 'YYYY-MM-DD')
AND FAT.DEBIT_CREDIT_MARK = 'Debit'
GROUP BY DA.ID, DDO.CODE
HAVING SUM(FAT.AMOUNT_IN_DEFAULT_CURRENCY) != 0 OR SUM(FPP.AMOUNT_IN_DEFAULT_CURRENCY) != 0;