Вложенный подзапрос - PullRequest
0 голосов
/ 17 марта 2019

Для столбцов Q03Total и Q02TOTAL Im получает недопустимое имя столбца при выполнении. Я не уверен, где проблема. Вложенный запрос выполняется должным образом.

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)

1 Ответ

1 голос
/ 17 марта 2019

Ваши имена 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)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...