| Author |
Topic |
|
BlackKnight
Starting Member
8 Posts |
Posted - 2007-05-02 : 05:24:20
|
Hi,I have 3 tables which I need to join together to get a user's test results. The following is a simplified version.Users: UserID(primary,int), Name(varchar)Subjects: SubjectID(primary,int), SubjectName(varchar)Scores: ScoreID(primary,int), UserID(int), SubjectID(int), Score(int)So the scores table contains the scores for each user for each test.Now not all users take all tests, but I want these to show as NULLs by using an outer join.I currently have:select * from scores sjoin Users u on s.UserID=u.UserIDright join Subjects subs on s.SubjectID=sub.SubjectIDand u.UserID=1 Which returns something like:ScoreID/SubjectID/Score/UserID/Name/SubjectName1/1/80/1/Joe Bloggs/Test 12/2/70/1/Joe Bloggs/Test 2NULL/3/NULL/NULL/NULL/Test 3if Joe Bloggs did not take Test 3.The problem is the 3rd row has NULLs for the User's name and the UserID - even though they are known because the query contains u.UserId=1.How can I adapt the query to replace the NULLs with the user's name? |
|
|
pbguy
Constraint Violating Yak Guru
319 Posts |
Posted - 2007-05-02 : 05:49:39
|
| declare @users table (userid int , name varchar(20))insert @usersselect 1, 'joe' unionselect 2, 'mark'declare @subject table(subjectid int, subjectname varchar(20))insert @subjectselect 1, 'maths' unionselect 2, 'science' unionselect 3, 'history'declare @score table(ScoreID int, UserID int, SubjectID int, Score int)insert @score select 1,1,1, 90 unionselect 2,1,2, 80select * from @users u join @subject su on u.userid=1 left outer join @score s on s.subjectid = su.subjectid and s.userid = u.userid |
 |
|
|
BlackKnight
Starting Member
8 Posts |
Posted - 2007-05-02 : 07:19:59
|
| Thanks, that's great.However I also have a Terms table, which contains TermID(primary,int) and TermName. (1, Autumn), (2,Winter), (3,Spring), (4,Summer)My Scores table has an additional column for the TermID.How do I now adapt the query to put NULLs for each term if a particular user did not take a particular test in that term.E.g. If User 1 took Maths in term 1, but not in terms 1,2 & 3 - I want rows like User 1, Maths, Term 1, 80User 1, Maths, Term 2, NULLUser 1, Maths, Term 3, NULLUser 1, Maths, Term 4, NULLandUser 1, Science, Term 1, 50User 1, Science, Term 2, 60User 1, Science, Term 3, 70User 1, Science, Term 4, NULL |
 |
|
|
pbguy
Constraint Violating Yak Guru
319 Posts |
Posted - 2007-05-02 : 08:01:15
|
| declare @users table (userid int , name varchar(20))insert @usersselect 1, 'joe' unionselect 2, 'mark'declare @subject table(subjectid int, subjectname varchar(20))insert @subjectselect 1, 'maths' unionselect 2, 'science' unionselect 3, 'history'declare @score table(ScoreID int, UserID int, SubjectID int, Score int, termid int)insert @score select 1,1,1, 90,1 unionselect 2,1,3, 80,3declare @term table (termid int, termname varchar(20))insert @termselect 1, 'Autumn' unionselect 2, 'Winter' unionselect 3, 'spring' unionselect 4, 'summer'select * from @users u join @subject su on u.userid=1 left outer join @score s on s.subjectid = su.subjectid and s.userid = u.userid left outer join @term t on t.termid = s.termid |
 |
|
|
|
|
|