2009-11-19 85 views
0

如果我運行下面的查詢:奇GROUP BY輸出DB2 - 結果並不如預期


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 

然後我得到了正確的「預算」的分組,而不是正確的「安排」的分組,只有兩排有一個「Y」。

如果我更改GROUP BY中的case語句的順序,那麼我會得到正確的分組(兩列的完整Y-N分解)。

我錯過了一些明顯的東西嗎?

回答

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 
+0

不,改變的順序選擇不起作用。 – CallCthulhu 2009-11-20 06:57:23