Oracle case when then简单用法

rowid      ID 主键     COUNTRY 国家    SEX 性别    POPULIATION 人口数量

1     0     中国     1     200 

2     1     中国     2     300 

3     2     美国     1     20 

4     3     美国     2     10 

5     4     加拿大     1     40 

6     5     加拿大     2     80 

7     6     英国     1     55 

8     7     英国     2     110 

9     8     小日本     3     111 

select t.country,
sum(case 
       when t.sex = '1' then t.populiation else '0' end) as 男,
sum(case when t.sex = '2' then t.populiation else '0' end) as 女
from test t group by t.country

rowid   country  男   女

1 中国 200 300
2 加拿大 40 80
3 美国 20 10
4 小日本 0 0
5 英国 55 110

select sum(t.populiation) ,
 case t.country when '中国' then '亚洲' when '美国' then '美洲'
       when '加拿大' then '美洲' when '英国' then '欧洲' 
      else '其他' end 
from test t  GROUP BY CASE t.country
     WHEN '中国'     THEN '亚洲'  WHEN '美国'     THEN '美洲' 
     WHEN '加拿大' THEN '美洲'  WHEN '英国' THEN '欧洲' ELSE '其他' END;

rowid   sum       area

1 165 欧洲
2 111 其他
3 500 亚洲
4 150 美洲

猜你喜欢

转载自my.oschina.net/u/2335525/blog/494857