Solved

SQL Count Two Select Statements

Posted on 2010-11-09
5
339 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
[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
  • 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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

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.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

762 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