2016-04-25 63 views

回答

0

,這是我能想到的唯一的事情:

... 
GROUP BY 
    CASE WHEN A >= 1 THEN 1 
    ELSE 0 
    END 
1

如果你在談論使用>運算符和GROUP BY子句,則:

select 
case when age between 0 and 20 then '0 - 20' 
    when age between 21 and 40 then '21 - 40' 
else '> 40' end age, 
sum(case when survived = 1 then else 0 end) survivors 
from rms_titanic 
group by 
case when age between 0 and 20 then '0 - 20' 
    when age between 21 and 40 then '21 - 40' 
else '> 40' 
order by 
case when age between 0 and 20 then '0 - 20' 
    when age between 21 and 40 then '21 - 40' 
else '> 40'; 

這可以寫成:

select 
case when age between 0 and 20 then '0 - 20' 
    when age between 21 and 40 then '21 - 40' 
else '> 40' end age, 
sum(case when survived = 1 then else 0 end) survivors 
from rms_titanic 
group by 1 
order by 1; 

但是,如果你想使用>對結果集的GROUP BY的運營商,T你將不得不包括HAVING子句:

select 
case when age between 0 and 20 then '0 - 20' 
    when age between 21 and 40 then '21 - 40' 
else '> 40' end age, 
passenger_class, 
sum(case when survived = 1 then else 0 end) survivors 
from rms_titanic 
group by 1, passenger_class having count(*) > 20 
order by 1; 
+1

爲什麼-1?我無法相信這一點。 – MontyPython

+2

不知道,對於一個模棱兩可的問題,你是一個完全合理的迴應。平息你。 –

+0

@RichBenner - 有時人們不知道他們在問什麼。這很好。我們的工作是幫助。也許問這個問題的人並不像我們其他人那麼清楚。因此。 :) – MontyPython

相關問題