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
 Joining a AVG and COUNT statement...

Author  Topic 

behringer420
Starting Member

2 Posts

Posted - 2010-03-11 : 17:20:54
Hi

I have been asked to execute a statement that show's me the average number of students of a class. Now normally when I want to know how many students belong to one class I would do a simple GROUP BY and COUNT statement, however I'm not sure how I do the same but instead find out the AVG instead of finding out how many there actually are.

Anyone have any ideas?

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-03-11 : 18:00:50
I would do it like this:

select avg(studentscounter) as avg_number_of_students
from
(select class, count(*) as studentscounter from table group by class) as dt

But I am not sure if there are more elegant ways because I never needed this.


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

behringer420
Starting Member

2 Posts

Posted - 2010-03-13 : 17:32:45
Hey, thanks that worked great.

Ive just found out I need to make sure the students have a start_date and end_date that is between 1-jun-2010 to 30-jun-2010.

how would i implement this into the statement above?

thanks :)
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-03-13 : 17:38:22
Add a WHERE clause to the query with your criteria.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-14 : 00:27:03
quote:
Originally posted by behringer420

Hey, thanks that worked great.

Ive just found out I need to make sure the students have a start_date and end_date that is between 1-jun-2010 to 30-jun-2010.

how would i implement this into the statement above?

thanks :)



select avg(studentscounter) as avg_number_of_students
from
(select class, count(*) as studentscounter from table where start_date >= '20100601' and end_date <='20100630' group by class) as dt


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -