-1

I have two SQL Server tables...

MODULES        ENROLMENTS
GroupNo        StudentNo
Title          GroupNo
Tutor          CourseworkMark
DayNo          ExamMark
Time
Room
Semester

In Access I have created a form based on another table which holds records of course numbers and names. For each course, I wish to create a subform about the modules within that particular course.

In the subform view I wish to have GroupNo, Title, Tutor, DayNo, Time, Room, Semester, Average of CourseworkMark and Average of ExamMark.

I have constructed this query

SELECT tblEnrolments.GroupNo, Tutor, DayNo, Time, Room, Semester, 
       AVG(CourseworkMark) AS AvgCourseworkMark, AVG(ExamMark) AS AvgExamMark
FROM tblEnrolments INNER JOIN tblModules ON tblEnrolments.GroupNo = tblModules.GroupNo
GROUP BY tblEnrolments.GroupNo;

however, I get the error

Msg 8120, Level 16, State 1, Line 1 Column 'tblModules.Tutor' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

when I run it. How do I get the Tutor, DayNo, Time, Room and Semester fields in the query as they are not part of the GROUP BY or AVG. Thank you

jpw
  • 44,361
  • 6
  • 66
  • 86
markthornton90
  • 241
  • 1
  • 3
  • 13

2 Answers2

0

You have to group by all values in your select list , so the query will look like :

SELECT tblEnrolments.GroupNo, Tutor, DayNo, Time, Room, Semester, 
       AVG(CourseworkMark) AS AvgCourseworkMark, AVG(ExamMark) AS AvgExamMark
FROM tblEnrolments INNER JOIN tblModules ON tblEnrolments.GroupNo = tblModules.GroupNo
GROUP BY tblEnrolments.GroupNo, Tutor, DayNo, Time, Room, Semester;
Nightmaresux
  • 538
  • 3
  • 12
0

As this error saying

Msg 8120, Level 16, State 1, Line 1 Column 'tblModules.Tutor' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

and also mention in the comment @Gordon Linoff:

You need to include all the non-aggregated columns in the group by

SELECT tblEnrolments.GroupNo, 
                     Tutor, 
                     DayNo, 
                     Time, 
                     Room, 
                     Semester, 
                     AVG(CourseworkMark) AS AvgCourseworkMark, 
                     AVG(ExamMark) AS AvgExamMark
FROM tblEnrolments 
INNER JOIN tblModules ON tblEnrolments.GroupNo = tblModules.GroupNo
GROUP BY tblEnrolments.GroupNo ,Tutor, DayNo, Time, Room, Semester ;
Khurram Ali
  • 1,659
  • 4
  • 20
  • 37