Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Select Statement

Author  Topic 

soori457
Yak Posting Veteran

85 Posts

Posted - 2008-02-26 : 03:55:47
Gender (TableName)
Gen (ColoumnName)
M(male)
M
F(female)
M
F
F
M
F
M

Now, I have to retrieve the count of Male and Female. The Output should be

Male 5
Female 4

Any one Help me plzz

Suresh Kumar

RickD
Slow But Sure Yak Herding Master

3608 Posts

Posted - 2008-02-26 : 04:01:14
Look up COUNT and GROUP BY in Book Online.
Go to Top of Page

pravin14u
Posting Yak Master

246 Posts

Posted - 2008-02-26 : 04:03:24
select

case when Gen='m' then 'Male'
else 'Female'
end
as Gender,count(*)

from Gender

group by
Gen
Go to Top of Page

sunil
Constraint Violating Yak Guru

282 Posts

Posted - 2008-02-26 : 04:04:49
declare @Gender table
(
gen varchar(10)
)
Insert @Gender
select 'M' union all
select 'M' union all
select 'F' union all
select 'M' union all
select 'M' union all
select 'F' union all
select 'M' union all
select 'M'

Select 'Female' ,count(GEN) from @Gender group by gen having gen='F'
union all
Select 'Male', count(GEN) from @Gender group by gen having gen='M'
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-02-26 : 04:23:35
[code]SELECT 'Male '+CAST(SUM(CASE WHEN Gen='M' THEN 1 ELSE 0 END) AS varchar(4)) + CHAR(13) +
'Female '+ CAST(SUM(CASE WHEN Gen='F' THEN 1 ELSE 0 END) AS varchar(4))
FROM Table[/code]
Go to Top of Page

ratheeshknair
Posting Yak Master

129 Posts

Posted - 2008-02-26 : 08:24:55
SELECT MALE,FEMALE FROM
(SELECT COUNT(GEN) AS MALE FROM GENDER WHERE GEN = 'M') AS MALE,
(SELECT COUNT(GEN) AS FEMALE FROM GENDER WHERE GEN = 'F') AS FEMALE

RKNAIR
Go to Top of Page
   

- Advertisement -