sakar
sakar

Reputation: 243

Group By results as individual column names

How to get the groupby result as column names

if staff table is there when we use

select count(*) from staff group by gender; gives me as

enter image description here

but i need them as columns Male | Female | None

Upvotes: 1

Views: 117

Answers (1)

Bill Gregg
Bill Gregg

Reputation: 7147

select sum(case when gender = 'Male' then 1 else 0 end) as 'Male',
sum(case when gender = 'Female' then 1 else 0 end) as 'Female',
sum(case when gender not in ('Male','Female') or gender is null then 1 else 0 end) as 'None'
from staff;

Upvotes: 6

Related Questions