ITPub博客

首页 > 数据库 > Oracle > oracle example

oracle example

原创 Oracle 作者:terryisme 时间:2009-10-09 14:24:33 0 删除 编辑

根据第一张表写出查询结果符合第2张表的语句(1--男,2—女)
表一:populations
country sex population
----------- -------- -----------------
中国 1 340
中国 2 260
美国 1 45
美国 2 55
加拿大 1 51
加拿大 2 49
英国 1 40
英国 2 60


根据表一生产如下查询结果:
国家 男 女
----------- -------- -----------
中国 340 260
美国 45 55
美加拿大 51 49
英国 40 60

代码如下:

select country, sum(decode(sex,'1',num)) 男, sum(decode(sex,'2',num)) 女 from populations

group by country;


select country,
sum(CASE WHEN(sex = '1') THEN num ELSE 0 END) 男,
sum(CASE WHEN(sex = '2') THEN num ELSE 0 END) 女,
from populations
group by country

1 select country,
2 sum(CASE sex WHEN '1' THEN num ELSE 0 END) 男,
3 sum(CASE sex WHEN '2' THEN num ELSE 0 END) 女
4 from population
5 group by country
*****************************************************************************
http://www.javaeye.com/topic/323085

[@more@]

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/93029/viewspace-1027668/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: first oracle sql
下一篇: oracle数据集
请登录后发表评论 登录
全部评论

注册时间:2009-02-23

  • 博文量
    142
  • 访问量
    632452