Do the group by on the column that is in the table you are selecting from. That should not make a difference in the grouping because you are doing inner joins to those columns. So....FROM ReportAStudentCount WHERE InstructorID = i.InstructorID AND ClassSubjectID = k.ClassSubjectID Group By InstructorID ) WHEN 1 THEN (SELECT sum(StudentCount) FROM ReportAStudentCount WHERE CenterID = k.CenterID AND ClassSubjectID = k.ClassSubjectID Group By CenterID) ELSE (SELECT sum(StudentCount) FROM ReportAStudentCount WHERE CenterID = k.CenterID AND ClassSubjectID = k.ClassSubjectID Group By CenterID) END) as Count ....
Ideally, you should be qualifying the column names in those inner selects with the table names/aliases - for example:....WHEN 1 THEN (SELECT sum(ra.StudentCount) FROM ReportAStudentCount ra WHERE ra.CenterID = k.CenterID AND ra.ClassSubjectID = k.ClassSubjectID Group By ra.CenterID)....