I have data in 1 table as
UserID CourseID TestID Result
1 1 1 P
1 1 2 P
2 1 1 F
2 1 2 F
this is showing that 2 Users ( UserID =1 and 2) has taken Test ( TestID=1 and 2) in CourseID =1
Now that CourseID in total has 3 Tests ( say TestID=1,2,7) as
CourseID TestID
1 1
1 2
1 7
this means none of the user has taken Test ( TestID=7) show now i want to show data as below
UserID CourseID TestID Result
1 1 1 P
1 1 2 P
1 1 7 null
2 1 1 F
2 1 2 F
2 1 7 null
I have been trying with left/right outer join with Group by, but could not get the desired result. How it can be achieved?
SQL Fiddle
MS SQL Server 2008 Schema Setup:
Query 1:
Results:
Note: If you have a table
Usersyou could replace the derived tablewith
Users as Uinstead.