Please start any new threads on our new site at We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 SQL Query (ADS)
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Starting Member

3 Posts

Posted - 11/10/2012 :  19:39:06  Show Profile  Reply with Quote
I have to list the average student reviewers’ age by gender. I can get them individually but how do i put them together so i have one code and not two separate ones?

SELECT AVG(Age) FROM Viewer WHERE Occupation='student' AND Gender='M';
SELECT AVG(Age) FROM Viewer WHERE Occupation='student' AND Gender='F';

Chirag Patel

Flowing Fount of Yak Knowledge

5155 Posts

Posted - 11/11/2012 :  07:11:51  Show Profile  Reply with Quote
You can include the conditions in the where clause in a case expression. Be sure to not include an else clause in the case expression.
	avg(case when Occupation='student' AND Gender='M' then Age end) as AvgMStudent,
	avg(case when Occupation='student' AND Gender='F' then Age end) as AvgFStudent
Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.02 seconds. Powered By: Snitz Forums 2000