SQL 需要使用 Union All 解决方案

SQL using Union All solution required

I have 5 tables which are set up at SQLFiddle

我需要帮助来完善以下查询。目前它给了我 "ORA-01427: single-row subquery returns more than one row"

数据库 - 甲骨文 11.x

sum(CRED) - sum(DEB) of 'Insurer' records only; group by pol.SP_NUM

select sp_num as pol_#, 
(coalesce (sum(
(
select     sum(Q.AMTQ) AS INSD 
from     S_INVOICE debit
,         S_ASSET pol
,         S_QUOTE_ITEM Q 
where     debit.FN_ACCNT_ID        =    pol.row_id
and     pol.x_quote_id            =    Q.row_id 
and     debit.DEBIT_TYPE        =    'Insurer'
and     debit.CO_ID is null
UNION ALL
select     sum(QXM.AMTQXM) AS INSD 
from     S_INVOICE debit
,         S_ASSET pol
,         S_QUOTE_ITEM_XM QXM 
where     debit.FN_ACCNT_ID        =    pol.row_id 
and     debit.DEBIT_TYPE        =    'Insurer'
and     debit.CO_ID                =    QXM.ROW_ID
)
  ),0) - 
  coalesce (sum(
  (
    select     sum(Q.AMTQ) AS INSC 
    from     S_SRC_PAYMENT credit
    ,         S_ASSET pol
    ,        S_QUOTE_ITEM Q 
    where     credit.ASSET_ID            =    pol.row_id
    and     pol.x_quote_id            =    Q.row_id 
    and     credit.CG_DEDN_TYPE_CD    =    'Insurer'
    and     credit.CO_ID is null
    UNION ALL
    select     sum(QXM.AMTQXM) AS INSC 
    from     S_SRC_PAYMENT credit
    ,         S_ASSET pol
    ,         S_QUOTE_ITEM_XM QXM 
    where     credit.asset_id            =    pol.row_id
    and     credit.CG_DEDN_TYPE_CD    =    'Insurer'
    and     credit.CO_ID            =    QXM.ROW_ID
    )
  ),0)
) as NP
from s_asset
group by sp_num;

预计O/P:

sum(cred) - sum(deb) = (412) - (63.1) = 348.9    

我修改后的查询是:

WITH CREDITS AS (SELECT SP_NUM, SUM(INSC) AS TOTAL_CREDITS
                   FROM (select pol.SP_NUM, Q.AMTQ AS INSC 
                           from S_SRC_PAYMENT credit,
                                S_ASSET pol,
                                S_QUOTE_ITEM Q 
                           where credit.ASSET_ID = pol.row_id and
                                 pol.x_quote_id = Q.row_id and
                                 credit.CG_DEDN_TYPE_CD = 'Insurer' and
                                 credit.CO_ID is null
                         UNION ALL
                         select pol.SP_NUM, QXM.AMTQXM AS INSC
                           from S_SRC_PAYMENT credit,
                                S_ASSET pol,
                                S_QUOTE_ITEM_XM QXM 
                           where credit.asset_id = pol.row_id and
                                 credit.CG_DEDN_TYPE_CD = 'Insurer' and
                                 credit.CO_ID = QXM.ROW_ID)
                   GROUP BY SP_NUM),
     DEBITS AS (SELECT SP_NUM, SUM(INSD) AS TOTAL_DEBITS
                  FROM (select pol.SP_NUM, Q.AMTQ AS INSD 
                          from S_INVOICE debit,
                               S_ASSET pol,
                               S_QUOTE_ITEM Q 
                          where debit.FN_ACCNT_ID = pol.row_id and
                                pol.x_quote_id = Q.row_id and
                                debit.DEBIT_TYPE = 'Insurer' and
                                debit.CO_ID is null
                          UNION ALL
                          select pol.SP_NUM, QXM.AMTQXM AS INSD 
                            from S_INVOICE debit,
                                 S_ASSET pol,
                                 S_QUOTE_ITEM_XM QXM 
                            where debit.FN_ACCNT_ID = pol.row_id and
                                  debit.DEBIT_TYPE = 'Insurer' and
                                  debit.CO_ID = QXM.ROW_ID)
                  GROUP BY SP_NUM)
SELECT COALESCE(c.SP_NUM, d.SP_NUM) AS POL_#,
       COALESCE(TOTAL_CREDITS, 0) - COALESCE(TOTAL_DEBITS, 0) AS NP
  FROM CREDITS c
  FULL OUTER JOIN DEBITS d
    ON d.SP_NUM = c.SP_NUM;

SQLFiddle here

基本问题是内部查询(此处作为 CTE 提取)正在对 INSC 和 INSD 值求和,但没有保留 SP_NUM(政策编号)作为求和的一部分(即没有 GROUP BY)。此外,因为我不知道您的数据,因此无法确定每个保单号码至少有贷方或借方,所以我将其设为 FULL OUTER JOIN。如果您可以确定保单号码总是有贷方或借方,您可以将 CTE 放在最后 SELECT 的第一位,然后将 LEFT OUTER JOIN 放在另一个 table 中。无论如何,这行得通。

祝你好运。