Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Count Where Sex = 'Male' .... ?

Posted on 2004-08-18
4
Medium Priority
?
1,254 Views
Last Modified: 2011-10-03
Im trying to do a count on an employee sex in a view

I have the following table (example)

Department          Sex
IT                         M
IT                         F
IT                         M
Other                    F
Other                    M
Other                    F

i want the results as follows (example)

Department    Female        Male          
IT                   1                 2
Other              2                 1
0
Comment
Question by:Y2Kingswood
  • 2
4 Comments
 
LVL 15

Expert Comment

by:jdlambert1
ID: 11837274
Try this:

SELECT sub1.Department, Female, Male
FROM
(SELECT Distinct Department FROM TableA) sub1
 LEFT JOIN (SELECT Department, Count(*) as Female
    FROM TableA
    WHERE Sex = 'F'
    GROUP BY Department
) sub2 ON sub1.Department = sub2.Department
 LEFT JOIN (SELECT Department, Count(*) as Male
    FROM TableA
    WHERE Sex = 'M'
    GROUP BY Department
) sub3 ON sub1.Department = sub3.Department
ORDER BY sub1.Department
0
 
LVL 10

Accepted Solution

by:
AaronAbend earned 200 total points
ID: 11837280
select department, sum(case sex when 'F' then 1 else 0 end) as feMale , sum(case sex when 'M' then 1 else 0 end) as Male
group by department
0
 
LVL 15

Expert Comment

by:jdlambert1
ID: 11837313
Y2Kingswood, not only is AaronAbend's query shorter, it runs much faster than mine, too.
0
 

Author Comment

by:Y2Kingswood
ID: 11837443
i have tried both and you are right, im happy to split the points or give them all to Aaron, let me know what you think is fair
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question