如果我运行以下查询:
select load_cyc_num
, crnt_dnlq_age_cde
, sum(cc_min_pymt_amt) as min_pymt
, sum(ec_tot_bal) as budget
, case when ec_tot_bal > 0 then 'Y' else 'N' end as budget
, case when ac_stat_cde in ('A0P','A1P','ARP','A3P') then 'Y' else 'N' end as arngmnt
, sum(sn_close_bal) as st_bal
from statements
where (sn_close_bal > 0 or ec_tot_bal > 0)
and load_cyc_num in (200911)
group by load_cyc_num
, crnt_dnlq_age_cde
, case when ec_tot_bal > 0 then 'Y' else 'N' end
, case when ac_stat_cde in ('A0P','A1P','ARP','A3P') then 'Y' else 'N' end
然后我得到了正确的“BUDGET”分组,但没有正确的“ARRANGEMENT”分组,只有两行有“Y”。
如果我改变GROUP BY中case语句的顺序,那么我得到正确的分组(两列的完整Y-N细分)。
我错过了一些明显的东西吗?
答案 0 :(得分:0)
尝试移动
,总和(cc_min_pymt_amt)为min_pymt,sum(ec_tot_bal)为预算
到select语句的末尾,即
select load_cyc_num,
crnt_dnlq_age_cde,
case when ec_tot_bal > 0 then 'Y' else 'N' end as budget,
case when ac_stat_cde in ('A0P','A1P','ARP','A3P') then 'Y' else 'N' end as arngmnt,
sum(sn_close_bal) as st_bal,
sum(cc_min_pymt_amt) as min_pymt,
sum(ec_tot_bal) as budget
from statements
where (sn_close_bal > 0 or ec_tot_bal > 0)and load_cyc_num in (200911)
group by load_cyc_num,
crnt_dnlq_age_cde,
case when ec_tot_bal > 0 then 'Y' else 'N' end ,
case when ac_stat_cde in ('A0P','A1P','ARP','A3P') then 'Y' else 'N' end