簡體   English   中英

從Teradata中的子查詢塊合並全部

[英]Union all from a subquery block in teradata

我有一個內部子查詢塊,可為我提供10個字段。 我想在上面使用union all語句,看起來像

SEL upd_tbl.cdi_batch_id, upd_tbl. equ_gender1_chg_cnt AS name 
UNION ALL
SEL upd_tbl.cdi_batch_id, upd_tbl. exp_ex_bmyr1_chg_cnt AS name 

FROM 
(
SEL 
MAX(act.load_id) AS cdi_batch_id,
SUM(CASE WHEN COALESCE(act.equ_gender1,'') = COALESCE(inact.equ_gender1,'') THEN 0 ELSE 1 END ) AS equ_gender1_chg_cnt,
SUM(CASE WHEN COALESCE(act.exp_ex_bmyr1,'') = COALESCE(inact.exp_ex_bmyr1,'') THEN 0 ELSE 1 END ) AS exp_ex_bmyr1_chg_cnt,
SUM(CASE WHEN COALESCE(act.equ_age1,'') = COALESCE(inact.equ_age1,'') THEN 0 ELSE 1 END ) AS equ_age1_chg_cnt,
SUM(CASE WHEN COALESCE(act.maritalstatus1,'') = COALESCE(inact.maritalstatus1,'') THEN 0 ELSE 1 END ) AS maritalstatus1_chg_cnt,
SUM(CASE WHEN COALESCE(act.person_type1,'') = COALESCE(inact.person_type1,'') THEN 0 ELSE 1 END ) AS person_type1_chg_cnt,
SUM(CASE WHEN COALESCE(act.homeowner,'') = COALESCE(inact.homeowner,'') THEN 0 ELSE 1 END ) AS homeowner_chg_cnt,
SUM(CASE WHEN COALESCE(act.dwelling_size,'') = COALESCE(inact.dwelling_size,'') THEN 0 ELSE 1 END ) AS dwelling_size_chg_cnt,
SUM(CASE WHEN COALESCE(act.lengthofresidence,'') = COALESCE(inact.lengthofresidence,'') THEN 0 ELSE 1 END ) AS lengthofresidence_chg_cnt,
SUM(CASE WHEN COALESCE(act.childrenage0_18,'') = COALESCE(inact.childrenage0_18,'') THEN 0 ELSE 1 END ) AS childrenage0_18_chg_cnt,
SUM(CASE WHEN COALESCE(act.numberofchildrenhh,'') = COALESCE(inact.numberofchildrenhh,'') THEN 0 ELSE 1 END ) AS numberofchildrenhh,
SUM(CASE WHEN COALESCE(act.numberadultsinhh,'') = COALESCE(inact.numberadultsinhh,'') THEN 0 ELSE 1 END ) AS numberadultsinhh
FROM
(SEL * FROM arc_mdm_Tbls.cnst_chrctrstc_bb WHERE load_id=10609 AND cnst_chrctrstc_end_dt='9999-12-31' (DATE)
)act
LEFT JOIN
(SEL * FROM arc_mdm_Tbls.cnst_chrctrstc_bb WHERE load_id=10609 AND cnst_chrctrstc_end_dt<'9999-12-31' (DATE) 
QUALIFY ROW_NUMBER() OVER (PARTITION BY cnst_mstr_id ORDER BY cnst_chrctrstc_strt_ts DESC)=1
)inact
ON act.cnst_mstr_id = inact.cnst_mstr_id 
) upd_tbl

但是它說對象upd_tbl不存在。 我只是不想為內部塊創建表。 請幫我。

您需要一個通用表表達式

WITH upd_tbl AS 
(
SEL 
MAX(act.load_id) AS cdi_batch_id,
SUM(CASE WHEN COALESCE(act.equ_gender1,'') = COALESCE(inact.equ_gender1,'') THEN 0 ELSE 1 END ) AS equ_gender1_chg_cnt,
SUM(CASE WHEN COALESCE(act.exp_ex_bmyr1,'') = COALESCE(inact.exp_ex_bmyr1,'') THEN 0 ELSE 1 END ) AS exp_ex_bmyr1_chg_cnt,
SUM(CASE WHEN COALESCE(act.equ_age1,'') = COALESCE(inact.equ_age1,'') THEN 0 ELSE 1 END ) AS equ_age1_chg_cnt,
SUM(CASE WHEN COALESCE(act.maritalstatus1,'') = COALESCE(inact.maritalstatus1,'') THEN 0 ELSE 1 END ) AS maritalstatus1_chg_cnt,
SUM(CASE WHEN COALESCE(act.person_type1,'') = COALESCE(inact.person_type1,'') THEN 0 ELSE 1 END ) AS person_type1_chg_cnt,
SUM(CASE WHEN COALESCE(act.homeowner,'') = COALESCE(inact.homeowner,'') THEN 0 ELSE 1 END ) AS homeowner_chg_cnt,
SUM(CASE WHEN COALESCE(act.dwelling_size,'') = COALESCE(inact.dwelling_size,'') THEN 0 ELSE 1 END ) AS dwelling_size_chg_cnt,
SUM(CASE WHEN COALESCE(act.lengthofresidence,'') = COALESCE(inact.lengthofresidence,'') THEN 0 ELSE 1 END ) AS lengthofresidence_chg_cnt,
SUM(CASE WHEN COALESCE(act.childrenage0_18,'') = COALESCE(inact.childrenage0_18,'') THEN 0 ELSE 1 END ) AS childrenage0_18_chg_cnt,
SUM(CASE WHEN COALESCE(act.numberofchildrenhh,'') = COALESCE(inact.numberofchildrenhh,'') THEN 0 ELSE 1 END ) AS numberofchildrenhh,
SUM(CASE WHEN COALESCE(act.numberadultsinhh,'') = COALESCE(inact.numberadultsinhh,'') THEN 0 ELSE 1 END ) AS numberadultsinhh
FROM
(SEL * FROM arc_mdm_Tbls.cnst_chrctrstc_bb WHERE load_id=10609 AND cnst_chrctrstc_end_dt='9999-12-31' (DATE)
)act
LEFT JOIN
(SEL * FROM arc_mdm_Tbls.cnst_chrctrstc_bb WHERE load_id=10609 AND cnst_chrctrstc_end_dt<'9999-12-31' (DATE) 
QUALIFY ROW_NUMBER() OVER (PARTITION BY cnst_mstr_id ORDER BY cnst_chrctrstc_strt_ts DESC)=1
)inact
ON act.cnst_mstr_id = inact.cnst_mstr_id 
) 
SEL cdi_batch_id, equ_gender1_chg_cnt AS name FROM upd_tbl 
UNION ALL
SEL cdi_batch_id, exp_ex_bmyr1_chg_cnt AS name FROM upd_tbl

但這將比我對上一個問題的回答中描述的方式效率低下

您必須為union的全部select查詢編寫內部查詢,如下所示。

SEL upd_tbl1.cdi_batch_id, upd_tbl1.equ_gender1_chg_cnt AS name 
FROM 
(
SEL 
MAX(act.load_id) AS cdi_batch_id,
SUM(CASE WHEN COALESCE(act.equ_gender1,'') = COALESCE(inact.equ_gender1,'')      THEN 0 ELSE 1 END ) AS equ_gender1_chg_cnt,
SUM(CASE WHEN COALESCE(act.exp_ex_bmyr1,'') =  COALESCE(inact.exp_ex_bmyr1,'') THEN 0 ELSE 1 END ) AS exp_ex_bmyr1_chg_cnt,
SUM(CASE WHEN COALESCE(act.equ_age1,'') = COALESCE(inact.equ_age1,'') THEN 0 ELSE 1 END ) AS equ_age1_chg_cnt,
SUM(CASE WHEN COALESCE(act.maritalstatus1,'') = COALESCE(inact.maritalstatus1,'') THEN 0 ELSE 1 END ) AS maritalstatus1_chg_cnt,
SUM(CASE WHEN COALESCE(act.person_type1,'') = COALESCE(inact.person_type1,'') THEN 0 ELSE 1 END ) AS person_type1_chg_cnt,
SUM(CASE WHEN COALESCE(act.homeowner,'') = COALESCE(inact.homeowner,'') THEN 0 ELSE 1 END ) AS homeowner_chg_cnt,
 SUM(CASE WHEN COALESCE(act.dwelling_size,'') = COALESCE(inact.dwelling_size,'') THEN 0 ELSE 1 END ) AS dwelling_size_chg_cnt,
SUM(CASE WHEN COALESCE(act.lengthofresidence,'') = COALESCE(inact.lengthofresidence,'') THEN 0 ELSE 1 END ) AS lengthofresidence_chg_cnt,
 SUM(CASE WHEN COALESCE(act.childrenage0_18,'') = COALESCE(inact.childrenage0_18,'') THEN 0 ELSE 1 END ) AS childrenage0_18_chg_cnt,
 SUM(CASE WHEN COALESCE(act.numberofchildrenhh,'') = COALESCE(inact.numberofchildrenhh,'') THEN 0 ELSE 1 END ) AS numberofchildrenhh,
 SUM(CASE WHEN COALESCE(act.numberadultsinhh,'') =  COALESCE(inact.numberadultsinhh,'') THEN 0 ELSE 1 END ) AS numberadultsinhh
 FROM
 (SEL * FROM arc_mdm_Tbls.cnst_chrctrstc_bb WHERE load_id=10609 AND    cnst_chrctrstc_end_dt='9999-12-31' (DATE)
 )act
 LEFT JOIN
 (SEL * FROM arc_mdm_Tbls.cnst_chrctrstc_bb WHERE load_id=10609 AND   cnst_chrctrstc_end_dt<'9999-12-31' (DATE) 
 QUALIFY ROW_NUMBER() OVER (PARTITION BY cnst_mstr_id ORDER BY   cnst_chrctrstc_strt_ts DESC)=1
)inact
ON act.cnst_mstr_id = inact.cnst_mstr_id 
 ) upd_tbl1

UNION ALL
SEL upd_tbl2.cdi_batch_id, upd_tbl2. exp_ex_bmyr1_chg_cnt AS name 

FROM 
(
SEL 
MAX(act.load_id) AS cdi_batch_id,
SUM(CASE WHEN COALESCE(act.equ_gender1,'') = COALESCE(inact.equ_gender1,'') THEN 0 ELSE 1 END ) AS equ_gender1_chg_cnt,
SUM(CASE WHEN COALESCE(act.exp_ex_bmyr1,'') = COALESCE(inact.exp_ex_bmyr1,'') THEN 0 ELSE 1 END ) AS exp_ex_bmyr1_chg_cnt,
SUM(CASE WHEN COALESCE(act.equ_age1,'') = COALESCE(inact.equ_age1,'') THEN 0 ELSE 1 END ) AS equ_age1_chg_cnt,
SUM(CASE WHEN COALESCE(act.maritalstatus1,'') = COALESCE(inact.maritalstatus1,'') THEN 0 ELSE 1 END ) AS maritalstatus1_chg_cnt,
SUM(CASE WHEN COALESCE(act.person_type1,'') = COALESCE(inact.person_type1,'') THEN 0 ELSE 1 END ) AS person_type1_chg_cnt,
SUM(CASE WHEN COALESCE(act.homeowner,'') = COALESCE(inact.homeowner,'') THEN 0 ELSE 1 END ) AS homeowner_chg_cnt,
SUM(CASE WHEN COALESCE(act.dwelling_size,'') = COALESCE(inact.dwelling_size,'') THEN 0 ELSE 1 END ) AS dwelling_size_chg_cnt,
SUM(CASE WHEN COALESCE(act.lengthofresidence,'') = COALESCE(inact.lengthofresidence,'') THEN 0 ELSE 1 END ) AS lengthofresidence_chg_cnt,
SUM(CASE WHEN COALESCE(act.childrenage0_18,'') = COALESCE(inact.childrenage0_18,'') THEN 0 ELSE 1 END ) AS childrenage0_18_chg_cnt,
SUM(CASE WHEN COALESCE(act.numberofchildrenhh,'') = COALESCE(inact.numberofchildrenhh,'') THEN 0 ELSE 1 END ) AS numberofchildrenhh,
SUM(CASE WHEN COALESCE(act.numberadultsinhh,'') = COALESCE(inact.numberadultsinhh,'') THEN 0 ELSE 1 END ) AS numberadultsinhh
FROM
(SEL * FROM arc_mdm_Tbls.cnst_chrctrstc_bb WHERE load_id=10609 AND cnst_chrctrstc_end_dt='9999-12-31' (DATE)
)act
LEFT JOIN
(SEL * FROM arc_mdm_Tbls.cnst_chrctrstc_bb WHERE load_id=10609 AND cnst_chrctrstc_end_dt<'9999-12-31' (DATE) 
QUALIFY ROW_NUMBER() OVER (PARTITION BY cnst_mstr_id ORDER BY  cnst_chrctrstc_strt_ts DESC)=1
)inact
ON act.cnst_mstr_id = inact.cnst_mstr_id 
) upd_tbl2

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM