Reputation: 3
I have an output such as below
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
But I am looking for getting the same output as like below
City Male Female
Chennai 640000 623000
Blore 500000 600000
Pune 700000 700000
Appreciate your help.
Thanks
Upvotes: 0
Views: 40
Reputation: 1
Here is short code
select city,sum(male),sum(female) from tablename group by city;
Upvotes: 0
Reputation: 44874
You need conditional sum
for this
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
Upvotes: 2