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
 Printing average-value from two tables with PHP/SQ

Author  Topic 

ijjed
Starting Member

1 Post

Posted - 2012-01-30 : 11:48:03
Hi,

Firstly, these are my tables:

CLASS
class_id class_name
1 Donkeys
2 Monkeys
4 Classic
9 Humans

COURSES
class_id courses_number
9 6
9 2
1 3
2 2


I would like to print average course_number for each class_name.
So for class_name Donkeys would have average course_number 3.
And for clasS_name Humans, average course_number is 4.
If the class_name does not have any average number, it would still be listed. In this example, class_name Classic, would not be listed.

Do you get my point?

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-01-30 : 12:38:49
[code]
SELECT c.Class_name,COALESCE(cr.AvgCrs,0) AS AvgCrs
FROM CLASS c
LEFT JOIN (SELECT Class_id,AVG(courses_number*1.0) AS AvgCrs
FROM COURSES
GROUP BY Class_Id)cr
ON cr.Class_Id = c.Class_id
[/code]

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

Go to Top of Page
   

- Advertisement -