Skip to content
Advertisement

Sql to count m and f based on age [closed]

i have sql table like this

table name: person

age gander
21 m
21 m
21 f
22 m
22 f

what is query to get output like this:

age count_m count_f
21 2 1
22 1 1

i want to count m and f based on age

Advertisement

Answer

A little bit of conditional aggregation will do the trick.

select age
, count(case when gander = 'm' then 1 end) as count_m
, count(case when gander = 'f' then 1 end) as count_f
from person
group by age
order by age
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement