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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Please sugges me a query for the proble

Status
Not open for further replies.

kishorecvr

Programmer
Feb 21, 2002
19
0
0
IN
I have a table like

SEX NAME
----- --------
M KISHORE
M KUMAR
M DILIP
M CVR
M CKR
F REKHA
F RANI
F PANDU

and I want to retrieve data like

SEX TOTAL MALE/FEMALE PERCENTAGE
------ ---------------- ----------
M 5 62.5
F 3 37.5

Can any one suggest me a solution?

Thanks in advance.

Kishore.
 
This is one of those that will probably vary by RDBMS.
Here's one way to do it in Oracle:

SELECT m.males,
m.males/(m.males + f.females) m_pct,
f.females,
f.females/(m.males + f.females) f_pct
FROM
(SELECT count(*) males
FROM my_table
WHERE sex = 'M') m,
(SELECT COUNT(*) females
FROM my_table
WHERE sex = 'F') f;
 
This should work on most sql databases

select sex, count(*) total,
count(*) / (select count(*) from mytable) percentage
from mytable
group by sex order by sex
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top