SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

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

cpat258
Starting Member

USA
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

sunitabeck
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.
select 
	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  
 New 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.14 seconds. Powered By: Snitz Forums 2000