Solved

SQL Count Two Select Statements

Posted on 2010-11-09
5
341 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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

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