Solved

SQL Count Two Select Statements

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Instering to MySQL table 5 38
Consolidating oracle query results to a single line 8 52
Amazon Redshift 2 25
PHP: concatenate query 12 33
'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
A short film showing how OnPage and Connectwise integration works.

914 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

18 Experts available now in Live!

Get 1:1 Help Now