用于输出的Oracle SQL查询调优

时间:2014-12-12 08:17:49

标签: sql oracle

我有一个输出,如下面的

select City, Gender, count(*) from  tablename group by City, Gender ;

City      Gender   count(*)
Chennai   Male     640000
Chennai   Female   623000
Blore     Male     500000
Blore     Female   600000
Pune      Male     700000
Pune      Female   700000

但我正在寻找与下面相同的输出

City          Male          Female
Chennai       640000        623000
Blore         500000        600000
Pune          700000        700000

感谢您的帮助。

由于

2 个答案:

答案 0 :(得分:2)

您需要有条件的sum

select
City,
sum(case when Gender = 'Male' then 1 else 0 end) as Male,
sum(case when Gender = 'Female' then 1 else 0 end) as Female
from  tablename group by City

答案 1 :(得分:0)

这是简短的代码

从城市的表名组中选择城市,总和(男性),总和(女性);