views:

60

answers:

4

I have two tables. The first Holds a list of classes with basically an ClassID and a ClassName. The second Holds a list of classes that a pupil does attend using ClassID and PupilID (PupilID refers to a third table).

Is it possible to return for a given pupil a list of ALL Classes irrespective of attendance yet indicate the classes they do attend using MS SQL query. I have read about SQL CASE and IF and even something about creating and assigning variables in queries but none of the examples indicate more than one table.

something like -

|ClassName | Attended
|   1      |    0 
|   2      |    0 
|   3      |    1 
|   4      |    0
A: 

Sounds like homework to me. Try looking up LEFT OUTER JOIN in your documentation.

AnthonyWJones
It's more personal development than homework, but I understand your sentiment.SELECT timetable.ID, timetable.Class_ID, timetable.Pupil_ID, classes.Class_ID AS Expr1, classes.Class_Name, classes.Class_Day, classes.Class_OrderViewFROM timetable LEFT OUTER JOIN classes ON timetable.Class_ID = classes.Class_IDGives <b>all pupils</b> classes and WHERE pupil_ID = just gives the attended classes.
Confussedinwales
Move pupil id into the join condition. Having it in the where clause will zap the left outer join.
pjp
SELECT t.ID, t.Class_ID, t.Pupil_ID, c.Class_Name, c.Class_Day, c.Class_OrderViewFROM timetable AS t LEFT OUTER JOIN classes AS c ON t.Class_ID = c.Class_ID AND t.Pupil_ID = '1'Still returns ALL rows, yet any class the pupil doesn't attend has NULL as ClassName.
Confussedinwales
A: 

And perhaps a count() for attendance?

Mirvnillith
Excuse my ignorance Mirvnillith, not sure what you meant there.
Confussedinwales
Seems like you want a count of the attendence, not just a joined entry. And as you're not even interested in the attendence rows you can just count() them directly (i.e. count(*) where "attendence of this class").
Mirvnillith
A: 
SELECT t.ID, 
       t.Class_ID, 
       t.Pupil_ID, 
       c.Class_ID AS Expr1, 
       c.Class_Name, 
       c.Class_Day, 
       c.Class_OrderView 
FROM timetable AS t
       LEFT OUTER JOIN classes as c 
       ON (t.Class_ID = c.Class_ID) AND (t.Pupil_ID = MyPupil)

The problem is that pesky WHERE when doing a JOIN

Degan
Well that sent VisualStudio SQL pane mad! I am going to see if I can find another tool that won't rewrite the query.I like the logic.
Confussedinwales
No Degan, unfortunately returned every row (lots of NULL) so obviously is filtering to some degree. I appreciate your input though.
Confussedinwales
Ran it in MS SQL Server Management studio Express by the way.
Confussedinwales
I think your FROM should be the Classes table not the Timetable table
pjp
+3  A: 

Try this - appologies to Degan for stealing your example and changing it.

SELECT t.ID, 
       t.Class_ID, 
       t.Pupil_ID, 
       c.Class_ID AS Expr1, 
       c.Class_Name, 
       c.Class_Day, 
       c.Class_OrderView 
FROM Classes AS c
LEFT OUTER JOIN timetable as t 
       ON (c.Class_ID = t.Class_ID) AND (t.Pupil_ID = MyPupil)
pjp
pjp - The server says 'The correlation name 'c' is specified multiple times in a FROM clause.' but you seem to be onto something when you said the FROM should be the classes (it returned 18 rows instead of hundreds.. was expecting 25 classes btw).
Confussedinwales
oopsie typo: LEFT OUTER JOIN timetable as t
pjp
Changed to - LEFT OUTER JOIN timetable as t and is returning too few classes but getting there.
Confussedinwales
It would help if I was looking at the correct table /embarassed. Thank you so much. AND you taught me something into the bargain.
Confussedinwales
pjp, no problem, I was away and could not respond.
Degan