Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
Malby
Starting Member
10 Posts |
Posted - 2002-08-28 : 22:03:48
|
| Here are my table structures;lGradesID | Name--------------------------------1 | AA2 | A3 | AB4 | B5 | C6 | D7 | E8 | XXtblGradesClientID | GradeID | TopupMax1 | 1 | 1000001 | 2 | 100001 | 3 | 10001 | 4 | 1001 | 5 | 102 | 1 | 100000002 | 2 | 10000002 | 3 | 1000002 | 4 | 100002 | 5 | 10002 | 6 | 1002 | 7 | 102 | 8 | 1Here's an example query;SELECT g.ID, g.Name, (SELECT TopupMax FROM tblGrades cg WHERE cg.GradeID = g.ID AND cg.ClientID = 1) AS TopupMaxFROM lGrades g Here is the output:ID Name TopupMax ----- ---------- ----------- 1 AA 1000002 AB 100003 A 10004 B 1005 C 106 D NULL7 E NULL8 XX NULLNow my question is... How can I do that WITHOUT using the subquery?I tried a left outer join, however, it only returns:ID Name TopupMax ----- ---------- ----------- 1 AA 1000002 AB 100003 A 10004 B 1005 C 10Not the null records. I need the null records.Help?----------------------"An eye for an eye will leave the whole world blind" - Ghandi |
|
|
timmy
Master Smack Fu Yak Hacker
1242 Posts |
Posted - 2002-08-28 : 22:45:43
|
All you need to do is this:SELECT g.ID, g.Name, cg.TopupMax FROM lGrades g LEFT JOIN tblGrades cg ON cg.GradeID = g.ID AND cg.ClientID = 1 if you put the ClientID check in the WHERE clause, it will filter the whole result set. Putting the check in the JOIN clause is a way of getting around this.Hope it helps.Tim |
 |
|
|
Malby
Starting Member
10 Posts |
Posted - 2002-08-28 : 22:49:45
|
| Note, I empted tblGrades completely, because I want the following query to rewturn all Nulls for TopupMax...SELECT g.ID, g.Name, c.TopupMaxFROM lGrades gLEFT JOIN tblGrades c ON g.ID = c.GradeIDWHERE c.ClientID = 1ID Name TopupMax ----- ----- ----------- (0 row(s) affected)----------------------"An eye for an eye will leave the whole world blind" - Ghandi |
 |
|
|
Merkin
Funky Drop Bear Fearing SQL Dude!
4970 Posts |
Posted - 2002-08-29 : 02:59:50
|
MalbyI saw this over at Whirlpool, but prefer to post here Try Timmy's query. He has put the client filter in the join clause which will only join client 1. If you filter in the where clause it will require client id to be 1 which eliminates the point of a left join.Damian |
 |
|
|
|
|
|
|
|