加入查询后,sum aggregate的值会发生变化

时间:2017-07-18 16:22:19

标签: sql oracle oracle-sqldeveloper

当我单独为SUM值运行三个单独的查询时,我得到了正确的数量,但是当我加入查询时,我的值会发生变化。如何仅汇总DISTINCT VALUES?我需要CUST_ID这个组。

SELECT DISTINCT
    CUS.CUST_ID,
    VWC.DISPLAY_NM,
    VWC.PREFIX_NM,
    VWC.FIRST_NM,
    VWC.MIDDLE_NM,
    VWC.LAST_NM,
    VWC.SUFFIX_NM,
    VWC.STREET1,
    VWC.STREET2,
    VWC.CITY_NM,
    VWC.STATE_CD,
    VWC.POSTAL_CD,
    VWC.COUNTRY_CD,
    VWC.EMAIL,
    SUM(ACC.PAYMENT_FAMT) AS TOTALWRITEOFF,
    SUM(CUS.PAID) AS TOTALPAID,
    SUM(REC.BALANCE) AS TOTALBALANCE

    FROM
    AHR_VW_CUSTOMER VWC
    JOIN
    AR_VW_PAY_ACCT ACC 
    ON ACC.CUST_ID = VWC.CUST_ID
    JOIN
    AR_VW_CUS_ACCT CUS
    ON CUS.CUST_ID = ACC.CUST_ID
    JOIN
    AR_VW_CUS_ACCT_RECEIPT REC
    ON REC.CUST_ID=CUS.CUST_ID

    WHERE 
    ---CUS.CUST_ID = '00123044'
    ---AND
    (ACC.PAYMENT_TY='FDN - Writeoff Individul')

    GROUP BY
    CUS.CUST_ID,
    VWC.DISPLAY_NM,
    VWC.PREFIX_NM,
    VWC.FIRST_NM,
    VWC.MIDDLE_NM,
    VWC.LAST_NM,
    VWC.SUFFIX_NM,
    VWC.STREET1,
    VWC.STREET2,
    VWC.CITY_NM,
    VWC.STATE_CD,
    VWC.POSTAL_CD,
    VWC.COUNTRY_CD,
    VWC.EMAIL
    ORDER BY
    VWC.LAST_NM

1 个答案:

答案 0 :(得分:0)

使用分析功能;或者在连接之前在子查询中生成总和。

我也有你的连接问题,因为你在左连接的右边的表上有where子句限制来否定你的左连接。

CUS.CUST_ID = '00123044'
AND
(ACC.PAYMENT_TY='FDN - Writeoff Individul')

以上将取消VWC没有匹配记录的记录;那左边加入的原因是什么?用内心?或者将这些限制移到连接处。

SELECT DISTINCT
CUS.CUST_ID,
VWC.DISPLAY_NM,
VWC.PREFIX_NM,
VWC.FIRST_NM,
VWC.MIDDLE_NM,
VWC.LAST_NM,
VWC.SUFFIX_NM,
VWC.STREET1,
VWC.STREET2,
VWC.CITY_NM,
VWC.STATE_CD,
VWC.POSTAL_CD,
VWC.COUNTRY_CD,
VWC.EMAIL,
SUM(ACC.PAYMENT_FAMT) over (partition by ACC.CUST_ID) AS TOTALWRITEOFF,
SUM(CUS.PAID) over (partition by CUS.CUST_ID)AS TOTALPAID,
SUM(REC.BALANCE) over (partition by REC.CUST_ID)AS TOTALBALANCE

FROM AHR_VW_CUSTOMER VWC
JOIN AR_VW_PAY_ACCT ACC 
  ON ACC.CUST_ID = VWC.CUST_ID
 AND ACC.PAYMENT_TY='FDN - Writeoff Individul'
LEFT JOIN AR_VW_CUS_ACCT CUS
  ON CUS.CUST_ID = ACC.CUST_ID
 and CUS.CUST_ID = '00123044'
LEFT JOIN AR_VW_CUS_ACCT_RECEIPT REC
  ON REC.CUST_ID=CUS.CUST_ID

GROUP BY
CUS.CUST_ID,
VWC.DISPLAY_NM,
VWC.PREFIX_NM,
VWC.FIRST_NM,
VWC.MIDDLE_NM,
VWC.LAST_NM,
VWC.SUFFIX_NM,
VWC.STREET1,
VWC.STREET2,
VWC.CITY_NM,
VWC.STATE_CD,
VWC.POSTAL_CD,
VWC.COUNTRY_CD,
VWC.EMAIL
ORDER BY
VWC.LAST_NM

或者在连接之前计算总计。

SELECT DISTINCT
CUS.CUST_ID,
VWC.DISPLAY_NM,
VWC.PREFIX_NM,
VWC.FIRST_NM,
VWC.MIDDLE_NM,
VWC.LAST_NM,
VWC.SUFFIX_NM,
VWC.STREET1,
VWC.STREET2,
VWC.CITY_NM,
VWC.STATE_CD,
VWC.POSTAL_CD,
VWC.COUNTRY_CD,
VWC.EMAIL,
Sum_PAYMENT_FAMT AS TOTALWRITEOFF,
SUM_PAID AS TOTALPAID,
SUM_Balance  AS TOTALBALANCE

FROM AHR_VW_CUSTOMER VWC
JOIN (SELECT SUM(PAYMENT_FAMT) SUM_Payment_FAMT, CUST_ID 
      FROM AR_VW_PAY_ACCT
      GROUP BY CUST_ID) ACC 
  ON ACC.CUST_ID = VWC.CUST_ID
 AND  ACC.PAYMENT_TY='FDN - Writeoff Individul'
LEFT JOIN (SELECT SUM(PAID) sum_Paid, CUST_ID 
           FROM AR_VW_CUS_ACCT
           GROUP BY CUST_ID) CUS
  ON CUS.CUST_ID = ACC.CUST_ID
 AND CUS.CUST_ID = '00123044'
LEFT JOIN (SELECT SUM(BALANCE) Sum_Balance, CUST_ID
           FROM AR_VW_CUS_ACCT_RECEIPT 
           GROUP BY CUST_ID) REC
  ON REC.CUST_ID=CUS.CUST_ID
--  WHERE CUS.CUST_ID = '00123044'
--    AND  (ACC.PAYMENT_TY='FDN - Writeoff Individul')

GROUP BY
CUS.CUST_ID,
VWC.DISPLAY_NM,
VWC.PREFIX_NM,
VWC.FIRST_NM,
VWC.MIDDLE_NM,
VWC.LAST_NM,
VWC.SUFFIX_NM,
VWC.STREET1,
VWC.STREET2,
VWC.CITY_NM,
VWC.STATE_CD,
VWC.POSTAL_CD,
VWC.COUNTRY_CD,
VWC.EMAIL
ORDER BY
VWC.LAST_NM

或者你可以计算选择中的总数,但我发现性能很差,因为必须为每个cust_ID运行一次

SELECT ...

(SELECT SUM(PAYMENT_FAMT) 
 FROM AR_VW_PAY_ACCT ACC 
 WHERE ACC.CUST_ID = VWC.CUST_ID) AS TOTALWRITEOFF, --uses correlated query