使用交叉连接时按语法分组

时间:2015-11-19 09:28:58

标签: sql group-by cross-join

我希望在下面的SQL中应用Group By条件,以便O / P将显示带有GP的POL#。

select t.POl#, (DEB.CUSTD - CRED.CUSTC) AS GP 
from (
        (
            select POL.SP_NUM POL#
            ,      sum(D.AMT) AS CUSTD 
            from   S_INVOICE D
            ,      S_ASSET POL 
            where  POL.ROW_ID           =   D.FN_ACCNT_ID 
            and    POL.SP_NUM           in  ('000','111','222') 
            and    D.DEBIT_TYPE         =   'Customer' 
            group by POL.SP_NUM
        ) DEB
        CROSS JOIN
        (
            select pol.SP_NUM POL#
            ,      sum(C.AMT) AS CUSTC 
            from   S_SRC_PAYMENT C
            ,      S_ASSET POL
            where  POL.ROW_ID           =   C.ASSET_ID 
            and    POL.SP_NUM           in  ('000','111','222') 
            and    C.CG_DEDN_TYPE_CD    =   'Customer' 
            group by POL.SP_NUM
        ) CRED
    ) t
    group by t.POL#

当我执行相同的操作时,我收到“ORA-00933:SQL命令未正确结束”错误,其中光标指向't'

请帮忙。

Expected O/P:
POL#    GP
000     800
111     120
222     50

附上样本数据并附上解释,以便更好地理解要求:

表1:

S_ASSET
ROW_ID    POL#
1         000
2         111
3         222
4         333
5         444

表2:

S_INVOICE (Debit Table)
FN_ACCNT_ID    POL#    DEBIT_TYPE          AMT
1              000     Customer            10
1              000     Customer            10
1              000     Insurer             5
2              111     Customer            10
3              222     Customer            10
3              222     Insurer             5
5              444     Insurer             10

表3:

S_SRC_PAYMENT (Credit Table)
ASSET_ID    POL#    CG_DEDN_TYPE_CD     AMT
1           000     Insurer             10
1           000     Insurer             10
1           000     Customer            5
2           111     Insurer             10
3           222     Insurer             5
3           222     Insurer             5
3           222     Customer            5
5           444     Customer            10

根据此查询,我将考虑每个POL#的“客户”记录和SUM AMT。 (客户的每笔借记都将按照POL&#反之亦然信用保险公司

每个POL的预期O / P(借方总和 - 贷方总和)

POL #       AMT (GP)
000         15
111         10
222         5
333         0
444        -10

2 个答案:

答案 0 :(得分:2)

你显然只想获得每个s_asset的deb和cred然后聚合以获得总和。您可以在没有连接的情况下执行此操作,并直接将参数子查询:

select 
  sp_num as pol#,
  sum(<get deb sum for the pol.row_id here>) - sum(<get cred sum for the pol.row_id here>)
from s_asset pol 
where sp_num in ('000','111','222')
group by sp_num;

完整的查询:

select 
  sp_num as pol#,
  coalesce(sum(
    (
      select sum(deb.amt)
      from s_invoice deb
      where deb.fn_accnt_id = pol.row_id
      and deb.debit_type = 'Customer' 
    )
  ), 0) -
  coalesce(sum(
    (
      select sum(cred.amt) 
      from s_src_payment cred
      where cred.asset_id = pol.row_id
      and cred.cg_dedn_type_cd = 'Customer' 
    ), 0)
  ) as gp
from s_asset pol 
where sp_num in ('000','111','222')
group by sp_num;

与联接相同:

select 
  pol.sp_num as pol#,
  coalesce(sum(deb.total), 0) - coalesce(sum(cred.total), 0) as gp
from s_asset pol
left join
(
  select fn_accnt_id as pol_row_id, sum(deb.amt) as total
  from s_invoice
  where  debit_type = 'Customer' 
  group by fn_accnt_id 
) deb on deb.pol_row_id = pol.row_id
left join
(
  select asset_id as pol_row_id, sum(amt) as total
  from s_src_payment
  where  cg_dedn_type_cd = 'Customer' 
  group by asset_id
) cred on cred.pol_row_id = pol.row_id
group by pol.sp_num;

答案 1 :(得分:0)

此语法

from ( (select ...) CROSS JOIN (select ...) )

无效。 JOIN属于FROM。所以这些都是正确的:

from ( SELECT * FROM (select ...) CROSS JOIN (select ...) )

from (select ...) CROSS JOIN (select ...)

但是,你确定要一个CROSS JOIN吗?两个子查询都为你提供POL数字和数据,而不是在POL#上加入它们,你交叉加入结果,所以你得到所有的POL#组合。