联合所有来自 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 不存在。我只是不想为内部块创建 table 。请帮助我。

您必须为 union all 的 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

你需要一个通用Table表达式:

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

但这比我在

的回答中描述的方式效率低得多