I am new to SQL so hopefully this has an easy answer.
I have a Students table (studentID, name, statusID) and a StudentsClasses table (studentID, classID). I’ve been asked to create a view from the above tables that returns the following columns:
classID: (group-by)
count of students in each class AS Students
count of students where statusID = 1 As Actives
count of students where statusID = 2 As Inactives
I can easily group-by the classID colunm and count the number of students, but i dont think the count function can be told to count only certain rows. As soon as I add where or Having clauses I lose records that I need to be counted in other columns. Do I need to write a couple of different queries and then join the results of each?
Thanks for any help you can provide!
-David
This is ANSI SQL (should work as long as your SQL implementation is ANSI compatible):