Solved

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

Posted on 2004-08-18
4
1,244 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

756 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