Oracle case when then简单用法

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

1     0     中国     1     200 code

2     1     中国     2     300 io

3     2     美国     1     20 class

4     3     美国     2     10 test

5     4     加拿大     1     40 select

6     5     加拿大     2     80 co

7     6     英国     1     55 cas

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 美洲

相关文章
相关标签/搜索