Solved

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

Posted on 2004-08-18
4
1,238 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 50 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

778 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