I have a inner sub query block which provides me 10 fields . I want to use union all statements on top of that that would look something like
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. 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.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.