Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Help with Group By Query

$
0
0

Given the two tables below:

Note that for CourseID=3 there is only one Student record and it is marked as deleted.

insert into dbo.Course([CourseID],[Type],[InstructorID],[Location])
 VALUES (1, '1', '1', '1');
insert into dbo.Course([CourseID],[Type],[InstructorID],[Location])
 VALUES ((SELECT MAX(CourseID) FROM dbo.[Course])+1, '2', '1', '1');
insert into dbo.Course([CourseID],[Type],[InstructorID],[Location])
 VALUES ((SELECT MAX(CourseID) FROM dbo.[Course])+1, '3', '1', '1');
insert into dbo.Course([CourseID],[Type],[InstructorID],[Location])
 VALUES ((SELECT MAX(CourseID) FROM dbo.[Course])+1, '4', '1', '1');
insert into dbo.Course([CourseID],[Type],[InstructorID],[Location])
 VALUES ((SELECT MAX(CourseID) FROM dbo.[Course])+1, '5', '1', '1');
 GO

insert into dbo.Student([StudentID],[CourseID],[LastName],[FirstName],[Deleted])
 VALUES (1, 1, 'Doe', 'John', '');
insert into dbo.Student([StudentID],[CourseID],[LastName],[FirstName],[Deleted])
 VALUES ((SELECT MAX(StudentID) FROM dbo.Student)+1, 1, 'Doe', 'Jane', '');
insert into dbo.Student([StudentID],[CourseID],[LastName],[FirstName],[Deleted])
 VALUES ((SELECT MAX(StudentID) FROM dbo.Student)+1, 1, 'Smith', 'Mary', '');

insert into dbo.Student([StudentID],[CourseID],[LastName],[FirstName],[Deleted])
 VALUES ((SELECT MAX(StudentID) FROM dbo.Student)+1, 2, 'Jones', 'Sandra', '');
insert into dbo.Student([StudentID],[CourseID],[LastName],[FirstName],[Deleted])
 VALUES ((SELECT MAX(StudentID) FROM dbo.Student)+1, 2, 'Williams', 'Paul', '');
insert into dbo.Student([StudentID],[CourseID],[LastName],[FirstName],[Deleted])
 VALUES ((SELECT MAX(StudentID) FROM dbo.Student)+1, 2, 'Young', 'Jeff', '');

insert into dbo.Student([StudentID],[CourseID],[LastName],[FirstName],[Deleted])
 VALUES ((SELECT MAX(StudentID) FROM dbo.Student)+1, 3, 'Test', 'Deleted', '1');

I would like to get a count of the number of students in each course, but if there is a single "deleted" record for a course I cannot figure out how to get the course to show with 0 students.  Here is the query I am trying to use (Note CourseID = 3 is not showing up):

SELECT DISTINCT Student.StudentID, Course.CourseID,
Count(Student.StudentID) AS [Count], Student.CourseID AS Student_CourseID
FROM Course LEFT JOIN Student ON Course.CourseID = Student.CourseID
GROUP BY Course.CourseID, Student.StudentID, Student.CourseID, Student.Deleted
HAVING (((Course.CourseID) Is Not Null) AND ((Student.Deleted)=0 OR Student.Deleted is null))
ORDER BY Student.StudentID, Course.CourseID;
GO

StudentID    CourseID    Count    Student_CourseID
NULL    4    0    NULL
NULL    5    0    NULL
1    1    1    1
2    1    1    1
3    1    1    1
4    2    1    2
5    2    1    2
6    2    1    2

Thanks for your help


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>