Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Coutnting Records in each category 1

Status
Not open for further replies.

Mayoor

Programmer
Jan 16, 2004
198
GB
Im trying to count the number of males, the number of females and then the number of people who havent specified either, i.e people will NULL in the gnder field. I need to table to appear like this

Male 23
Female 34
None Specified 45

Can anyone help?
 
have you tried using something like union selects :
select u.male, u.female from (count(field) as male where field = "male" Union select count(field) as female where = "female"...) as u
(note this wont actually run, but its a start)
 
Try this as a guide:
Code:
select gender, count(gender)
from table
where gender = 'Male'

union all

select gender, count(gender)
from table
where gender = 'Female'

union all

select 'None Specified', count(gender)
from table
where gender <> 'Male'
and gender <> 'Female'

~Brian
 
How about this

select gender =
case gender
when 'm' then 'Male'
when 'f' then 'Female'
else 'None Specified' end, count(*) cnt from table group by gender order by cnt

Tim
 
Or:

select gender =
case gender
when 'male' then 'Male'
when 'female' then 'Female'
else 'None Specified' end, count(*) cnt from table group by gender order by cnt

Not sure how the gender field is displayed in your table

Tim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top