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
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Problem with GROUP BY

Author  Topic 

mattastic
Starting Member

15 Posts

Posted - 2006-10-05 : 05:47:46
Hi Folks,

I need to select course information from 2 different tables schoolid is the foreign key. so far I have this:

  SELECT schools.schoolid, schools.schoolname, courses.courseschool, courses.coursetype  FROM courses, schools WHERE courses.courseschool = schools.schoolid and courses.coursetype = 'pt' GROUP BY schools.schoolid


But i get this error:

Column 'schools.SchoolName' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

What I would like to do is display the schoolname, and then all the courses in that school. Can anyone help?

Thankyou

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2006-10-05 : 05:56:52
Why grouping on School ID? Simple join wil give you what you want:

SELECT 
s.schoolname, c.courseschool, c.coursetype
FROM courses c join schools s
on c.courseschool = s.schoolid
and c.coursetype = 'pt'


If this is not the case, please explain your scenario more clearly.

Harsh Athalye
India.
"Nothing is Impossible"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-10-05 : 05:57:46
GROUP BY acts as DISTINCT too!


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-10-05 : 11:46:26
All non-aggregate columns of the Select should be part of Group by Clause if you use it

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -