Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL Count Two Select Statements

Posted on 2010-11-09
5
Medium Priority
?
344 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 1000 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 1000 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

688 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