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

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 SQL Query (ADS)

Author  Topic 

cpat258
Starting Member

3 Posts

Posted - 2012-11-10 : 19:39:06
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
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-11-11 : 07:11:51
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
   

- Advertisement -