简体   繁体   English

从Teradata中的子查询块合并全部

[英]Union all from a subquery block in teradata

I have a inner sub query block which provides me 10 fields . 我有一个内部子查询块,可为我提供10个字段。 I want to use union all statements on top of that that would look something like 我想在上面使用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

but it says object upd_tbl does not exist. 但是它说对象upd_tbl不存在。 I just do not want to create a table for the inner block. 我只是不想为内部块创建表。 Please help me. 请帮我。

You need a Common Table Expression : 您需要一个通用表表达式

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

But this will be way less efficient than the way described in my answer to your last question 但这将比我对上一个问题的回答中描述的方式效率低下

You have to write inner query for both the select query of union all as below. 您必须为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