使用 Cross Join 时按语法分组
Group By syntax while using Cross Join
我希望对下面的 SQL 应用分组条件,以便 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 command not properly ended" 错误,光标指向 't'
请提供帮助。
Expected O/P:
POL# GP
000 800
111 120
222 50
为了更好地理解需求,附加示例数据和解释:
Table 1:
S_ASSET
ROW_ID POL#
1 000
2 111
3 222
4 333
5 444
Table 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
Table 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# 的 "Customer" 记录并对 AMT 求和。 (客户的每笔借记都将根据 POL# 信用保险公司,反之亦然)
每个 POL 客户的预期 O/P(借方总和 - 贷方总和)#
POL # AMT (GP)
000 15
111 10
222 5
333 0
444 -10
这种语法
from ( (select ...) CROSS JOIN (select ...) )
无效。 JOIN 属于 FROM。所以这些中的任何一个都是正确的:
from ( SELECT * FROM (select ...) CROSS JOIN (select ...) )
或
from (select ...) CROSS JOIN (select ...)
但是,您确定要 CROSS JOIN 吗?两个子查询都为您提供 POL 编号和数据,而不是在 POL# 上连接它们,您交叉连接结果,因此您得到所有 POL# 组合。
您显然只想获得每个 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;
我希望对下面的 SQL 应用分组条件,以便 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 command not properly ended" 错误,光标指向 't'
请提供帮助。
Expected O/P:
POL# GP
000 800
111 120
222 50
为了更好地理解需求,附加示例数据和解释:
Table 1:
S_ASSET
ROW_ID POL#
1 000
2 111
3 222
4 333
5 444
Table 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
Table 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# 的 "Customer" 记录并对 AMT 求和。 (客户的每笔借记都将根据 POL# 信用保险公司,反之亦然)
每个 POL 客户的预期 O/P(借方总和 - 贷方总和)#
POL # AMT (GP)
000 15
111 10
222 5
333 0
444 -10
这种语法
from ( (select ...) CROSS JOIN (select ...) )
无效。 JOIN 属于 FROM。所以这些中的任何一个都是正确的:
from ( SELECT * FROM (select ...) CROSS JOIN (select ...) )
或
from (select ...) CROSS JOIN (select ...)
但是,您确定要 CROSS JOIN 吗?两个子查询都为您提供 POL 编号和数据,而不是在 POL# 上连接它们,您交叉连接结果,因此您得到所有 POL# 组合。
您显然只想获得每个 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;