Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hy,

I have a problem to solve, I have to display through one single sql statement how many women and how many men work in a department (The display must be made like this: 'W:M' )..I only know how to display them through a group by statement, like this:
select sex,count(sex) from employees group by sex;

But I have to display them using subselects and in the way I mentioned upper.

Is there anybody there who can help me?

Thanks a lot
Posted
Updated 13-Apr-11 3:38am
v2
Comments
[no name] 13-Apr-11 9:39am    
What do you mean "display"? The display, or rendering, of your data is separate from the actual query
stephy_ 13-Apr-11 10:01am    
Display the result in the bottom of the screen
stephy_ 13-Apr-11 10:02am    
for example, if i have 12 men and 15 women in my employees table, the result must be:
12:15

Do you mean something like this?
SQL
select MenCount   =(select count(*) from employees where sex='M'),
       WomenCount =(select count(*) from employees where sex='W')
 
Share this answer
 
Comments
stephy_ 13-Apr-11 10:00am    
doesn't work
ORA-00923: FROM keyword not found where expected [SQL State=42000, DB Errorcode=923]
And if i put 'from employees' is the same.. what are MenCount and WomenCount?
stephy_ 13-Apr-11 10:04am    
also i have to display the result as 'W:M' , for example, if i have 15 women and 12 men in my employees table, the result must be:

15:12
Try this
SQL
select woman_count
      ,man_count
  from ( select count(sex) woman_count
               ,0 man_count
           from employees
          where sex='W'
         union all
         select 0 woman_count
               ,count(sex) man_count
           from employees
          where sex='M'
        )
;


Not sure if this is really what you are after, but hoperfully it gives you a starting point. :)
 
Share this answer
 
Comments
stephy_ 13-Apr-11 10:10am    
thanks .. you know how can i can put a ':' character between the number of men and tha number of women ? (like , 12:15 where 12 is the number of men and 15 the number of women) ?
Chris Meech 13-Apr-11 10:31am    
Change it to select to_char(woman_count,'9999')||':'||to_char(man_count,'9999'). Do a search for differrent format specifiers for the to_char function, too.
You'd need to convert the results to string and concat them to display how you want

SQL
SELECT CONVERT(VARCHAR(100), (
    SELECT count(*) FROM employees WHERE sex = 'W')) + ':'
+ CONVERT(VARCHAR(10), (SELECT COUNT(*) FROM employees WHERE sex = 'M'))


Seems a bit pointless though! As Mark mentioned, formatting data should be kept separate from your query.
 
Share this answer
 
i finally made it, thanks for help:

the solution is here if someone is interested:

select to_char((select count(*) from employees where sex='M') ||':' ||
(select count(*) from employees where sex='W')) from employees
group by 1;

:)have a nice evening
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900