我有一个内部子查询块,它提供了10个字段。我想使用union所有语句,看起来像
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不存在。我只是不想为内部块创建一个表。请帮帮我。
答案 0 :(得分:1)
您需要公用表表达式:
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
但这比我对your last question
的回答中描述的方式效率低答案 1 :(得分:0)
你必须为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