Solved

SQL Count Two Select Statements

Posted on 2010-11-09
5
331 Views
Last Modified: 2012-05-10
I am working on a SQL query that will return data in the following format

"Company","Female","Male"
"Cage Data","3","4"
"Google","400","534"

The record set defines a company, the amount of female employees, and the amount of male employees.  Currently this is my query which returns the number of male employees
SELECT tusers.company, count(tclassextenddata.baseid)
FROM tusers
INNER JOIN tclassextenddatauseractivity AS tclassextenddata
ON tclassextenddata.baseid = tusers.userid
INNER JOIN tclassextendattributes
ON tclassextenddata.attributeid = tclassextendattributes.attributeid
WHERE tusers.subtype = 'Student'
  AND tclassextendattributes.name = 'gender'
  AND tclassextenddata.attributevalue = 'Female'
  AND tusers.inactive = 0
GROUP BY company

Open in new window


This is from a CMS engine so it isn't the best means of database layout, but I can't change it.
0
Comment
Question by:paldie
  • 2
  • 2
5 Comments
 
LVL 7

Accepted Solution

by:
tlovie earned 250 total points
ID: 34096678
Something like this might work:
SELECT tusers.company, sum(case tclassextenddata.attributevalue when 'Female' then 1 else 0 end) NumFemale, sum(case tclassextenddata.attributevalue when 'Female' then 0 else 1 end) NumMale
FROM tusers
INNER JOIN tclassextenddatauseractivity AS tclassextenddata
ON tclassextenddata.baseid = tusers.userid
INNER JOIN tclassextendattributes
ON tclassextenddata.attributeid = tclassextendattributes.attributeid
WHERE tusers.subtype = 'Student'
  AND tclassextendattributes.name = 'gender'
  AND tusers.inactive = 0
GROUP BY company
0
 
LVL 58

Assisted Solution

by:cyberkiwi
cyberkiwi earned 250 total points
ID: 34096728
tlovie's code should work, IF the attributevalue is ALWAYS 'Female' or anything else should be counted as "Male".
I added the condition in the where clause to make it perform better, and tweaked the sums to count Male/Female explicitly.

SELECT tusers.company,
sum(case tclassextenddata.attributevalue when 'Female' then 1 end) NumFemale,
sum(case tclassextenddata.attributevalue when 'Male' then 1 end) NumMale
FROM tusers
INNER JOIN tclassextenddatauseractivity AS tclassextenddata
ON tclassextenddata.baseid = tusers.userid
INNER JOIN tclassextendattributes
ON tclassextenddata.attributeid = tclassextendattributes.attributeid
WHERE tusers.subtype = 'Student'
  AND tclassextendattributes.name = 'gender'
  AND tclassextenddata.attributevalue IN ('Female', 'male')   ---- add this
  AND tusers.inactive = 0
GROUP BY company
0
 

Author Closing Comment

by:paldie
ID: 34096746
Thanks guys.  That query seems to be perfect.
0
 

Author Comment

by:paldie
ID: 34096760
One more side question.  Is it possible to give the records a default value of 0 if none of one gender is found?  Example Company A has 4 males and no females so the query returned would be:

[code]
"Company","Male","Female"
"Comp A","4","0"
[/code]
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34096802
Seems I oversimplified :)  Didn't think it likely there would be no male/female
The "else 0" from tlovie would have handled your requirement.

SELECT tusers.company,
sum(case tclassextenddata.attributevalue when 'Female' then 1 else 0 end) NumFemale,
sum(case tclassextenddata.attributevalue when 'Male' then 1 else 0 end) NumMale
FROM tusers
INNER JOIN tclassextenddatauseractivity AS tclassextenddata
ON tclassextenddata.baseid = tusers.userid
INNER JOIN tclassextendattributes
ON tclassextenddata.attributeid = tclassextendattributes.attributeid
WHERE tusers.subtype = 'Student'
  AND tclassextendattributes.name = 'gender'
  AND tclassextenddata.attributevalue IN ('Female', 'male')   ---- add this
  AND tusers.inactive = 0
GROUP BY company
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Creating and Managing Databases with phpMyAdmin in cPanel.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now