对于列Q03Total和Q02TOTAL,我在执行时获得了无效的列名。我不确定问题出在哪里。嵌套查询正在按预期运行。
INSERT INTO LARGE_EXPOSURE_MAIN
select counterparty, CType, CParties, CStatus , CDemands, CSavings, CTime , CFinIns , CLiab,
CCp , (CDemands + CSavings + CTime + CFinIns+ CLiab + CCp ) Q03_Total , CTrn, CLoans, COnBal, COffBal,
CNonPerf, CFunCoun, (CLoans + COnBal + COffBal + CNonPerf + CFunCoun) Q02_TOTAL
from
(
select counterparty , dbo.Q03_get_type_of_counterparty(customerid, 'Y') CType,
dbo.Q03_get_list_of_counterparties(customerid, 'Y') CParties ,
dbo.Q03_get_status_of_cp(customerid, 'Y') CStatus,
dbo.Q03_get_demand_deposits(@as_at_date, customerid, 'YES') CDemands,
dbo.Q03_get_savings_deposits(@as_at_date, customerid, 'YES') CSavings,
dbo.Q03_get_time_deposits_qs(@as_at_date, customerid, 'YES') CTime,
dbo.Q03_get_due_to_dep_taking_inst(@as_at_date, customerid, 'YES', trn) CFinIns,
dbo.Q03_get_other_liabilities(@as_at_date, customerid, 'YES') CLiab,
dbo.Q03_get_funding_to_cp(@as_at_date, customerid, 'YES') CCp,
trn Ctrn,
dbo.Q03_get_new_loans_advances(@as_at_date, customerid, 'Y',trn) CLoans,
dbo.Q03_get_accts_receivables_qs(customerid, 'Y',trn) COnBal,
dbo.Q02_Off_Bal_Ex(@as_at_date, customerid, 'Y',trn) COffBal,
dbo.Q02_extract_np_loans(@as_at_date, customerid, 'Y',trn) CNonPerf,
dbo.Q02_extract_counter_funding(@as_at_date, customerid, 'Y',trn) CFunCoun
from
(
select CMG.cust_name counterparty, cust_id customerid, PAN_GIR_NUM trn from [DEV_EIMDW_Archive].[ARCHOWN].[FINCL_CMG] cmg
where del_flg = 'N' and entity_cre_flg = 'Y'
and exists (select 1 from [DEV_EIMDW_Archive].[ARCHOWN].[FINCL_GAM] gam where gam.cust_id = cmg.cust_id and acct_cls_flg = 'N')
and cust_minor_flg = 'N'
and CUST_GRP = 'OTH' and PAN_GIR_NUM is not null) as a) as b
WHERE ((EXISTS (SELECT DISTINCT(TRN) FROM PRIME_CUST_MAIN) OR EXISTS (SELECT DISTINCT(CUST_NAME) FROM PRIME_CUST_MAIN))
OR (NOT EXISTS(SELECT DISTINCT(TRN) FROM PRIME_CUST_MAIN ) OR NOT EXISTS(SELECT DISTINCT(CUST_NAME) FROM PRIME_CUST_MAIN) ))
AND (Q03Total > 0 OR Q02TOTAL > 0)
答案 0 :(得分:1)
您对Q02Total和Q03Total的命名在一个位置上带有下划线,而在另一个位置则没有。此外,WHERE子句是定义这些字段的同一SELECT的一部分,因此它们对WHERE子句不可用。您可以在WHERE子句中用它们的值(添加在一起的字段的列表)替换它们,或将SELECT语句放在子查询中,然后在其上执行WHERE。
编辑: 将其放在子查询中的示例:
INSERT INTO LARGE_EXPOSURE_MAIN
select *
from (
select counterparty, CType, CParties, CStatus , CDemands, CSavings, CTime , CFinIns , CLiab,
CCp , (CDemands + CSavings + CTime + CFinIns+ CLiab + CCp ) Q03_Total , CTrn, CLoans, COnBal, COffBal,
CNonPerf, CFunCoun, (CLoans + COnBal + COffBal + CNonPerf + CFunCoun) Q02_TOTAL
from
(
select counterparty , dbo.Q03_get_type_of_counterparty(customerid, 'Y') CType,
dbo.Q03_get_list_of_counterparties(customerid, 'Y') CParties ,
dbo.Q03_get_status_of_cp(customerid, 'Y') CStatus,
dbo.Q03_get_demand_deposits(@as_at_date, customerid, 'YES') CDemands,
dbo.Q03_get_savings_deposits(@as_at_date, customerid, 'YES') CSavings,
dbo.Q03_get_time_deposits_qs(@as_at_date, customerid, 'YES') CTime,
dbo.Q03_get_due_to_dep_taking_inst(@as_at_date, customerid, 'YES', trn) CFinIns,
dbo.Q03_get_other_liabilities(@as_at_date, customerid, 'YES') CLiab,
dbo.Q03_get_funding_to_cp(@as_at_date, customerid, 'YES') CCp,
trn Ctrn,
dbo.Q03_get_new_loans_advances(@as_at_date, customerid, 'Y',trn) CLoans,
dbo.Q03_get_accts_receivables_qs(customerid, 'Y',trn) COnBal,
dbo.Q02_Off_Bal_Ex(@as_at_date, customerid, 'Y',trn) COffBal,
dbo.Q02_extract_np_loans(@as_at_date, customerid, 'Y',trn) CNonPerf,
dbo.Q02_extract_counter_funding(@as_at_date, customerid, 'Y',trn) CFunCoun
from
(
select CMG.cust_name counterparty, cust_id customerid, PAN_GIR_NUM trn from [DEV_EIMDW_Archive].[ARCHOWN].[FINCL_CMG] cmg
where del_flg = 'N' and entity_cre_flg = 'Y'
and exists (select 1 from [DEV_EIMDW_Archive].[ARCHOWN].[FINCL_GAM] gam where gam.cust_id = cmg.cust_id and acct_cls_flg = 'N')
and cust_minor_flg = 'N'
and CUST_GRP = 'OTH' and PAN_GIR_NUM is not null) as a) as b) as b
WHERE ((EXISTS (SELECT DISTINCT(TRN) FROM PRIME_CUST_MAIN) OR EXISTS (SELECT DISTINCT(CUST_NAME) FROM PRIME_CUST_MAIN))
OR (NOT EXISTS(SELECT DISTINCT(TRN) FROM PRIME_CUST_MAIN ) OR NOT EXISTS(SELECT DISTINCT(CUST_NAME) FROM PRIME_CUST_MAIN) ))
AND (Q03Total > 0 OR Q02TOTAL > 0)