Author |
Topic |
X002548
Not Just a Number
15586 Posts |
Posted - 2007-10-11 : 12:46:42
|
I must be having a rough day, plus I'm getting slammed from every direction from every platformIf I have this query SELECT x.Value FROM XREF x WHERE x.TableName = 'StaffType' AND x.ColumnName = 'StaffType' SELECT x.Value, s.Present, s.Proposed, s.OnBoard FROM XREF x LEFT JOIN PIFStaff s ON x.Value = s.StaffType WHERE x.TableName = 'StaffType' AND x.ColumnName = 'StaffType' AND s.PIFRecID IN (11,18) Why would the Value "Workstations (Staff)" be missing from the 2nd query?Value -------------------------------------------------- Private Offices (Agent)Private Offices (Staff)Workstations (Agent)Workstations (Staff)(4 row(s) affected)Value Present Proposed OnBoard -------------------------------------------------- ----------- ----------- ----------- Private Offices (Agent) 10 20 25Private Offices (Agent) 100 200 300Private Offices (Staff) 11 21 26Private Offices (Staff) 110 210 310Workstations (Agent) 12 22 27Workstations (Agent) 120 220 320 Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxAdd yourself!http://www.frappr.com/sqlteam |
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-10-11 : 13:01:42
|
I guess your IN condition is preventing the missing row from being included in the result set. I think this should do it:SELECT x.Value, s.Present, s.Proposed, s.OnBoard FROM XREF x LEFT JOIN PIFStaff s ON x.Value = s.StaffType WHERE x.TableName = 'StaffType' AND x.ColumnName = 'StaffType' AND s.PIFRecID IN (11,18) or s.PIFRecID IS NULL Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
X002548
Not Just a Number
15586 Posts |
Posted - 2007-10-11 : 13:03:09
|
Moved the predicate up to the JOIN SELECT x.Value, COALESCE(s.Present,0), COALESCE(s.Proposed,0), COALESCE(s.OnBoard,0) FROM XREF x LEFT JOIN PIFStaff s ON x.Value = s.StaffType AND s.PIFRecID IN (11,18) WHERE x.TableName = 'StaffType' AND x.ColumnName = 'StaffType' OK, so what's going on though?Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxAdd yourself!http://www.frappr.com/sqlteam |
 |
|
X002548
Not Just a Number
15586 Posts |
|
X002548
Not Just a Number
15586 Posts |
Posted - 2007-10-11 : 13:16:50
|
Huh, I did the same thing for the final query, because I had to use a different key SELECT pm.MEPrecID, pm.PIFRecID, x.Value AS StaffType , COALESCE(s.Present,0) AS Present, COALESCE(s.Proposed,0) AS Proposed, COALESCE(s.OnBoard,0) AS OnBoard FROM XREF x LEFT JOIN PIFStaff s ON x.Value = s.StaffTypeINNER JOIN PIF_MEP pm ON pm.PIFRecID = s.PIFRecID AND pm.MEPRecID = 1 WHERE x.TableName = 'StaffType' AND x.ColumnName = 'StaffType' And then change it to this and it worked..I'm so confused SELECT xxx.MEPrecID, xxx.PIFRecID, x.Value AS StaffType , COALESCE(xxx.Present,0) AS Present, COALESCE(xxx.Proposed,0) AS Proposed, COALESCE(xxx.OnBoard,0) AS OnBoard FROM XREF x LEFT JOIN ( SELECT pm.MEPrecID, pm.PIFRecID, s.StaffType, s.Present, s.Proposed, s.OnBoard FROM PIFStaff s INNER JOIN PIF_MEP pm ON pm.PIFRecID = s.PIFRecID AND pm.MEPRecID = 1) AS XXX ON x.Value = xxx.StaffType WHERE x.TableName = 'StaffType' AND x.ColumnName = 'StaffType' Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxAdd yourself!http://www.frappr.com/sqlteam |
 |
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2007-10-11 : 13:45:30
|
Does this help at all?http://weblogs.sqlteam.com/jeffs/archive/2007/05/14/60205.aspxA rule of thumb is: unless you are simply doing an "Is Null" check, NEVER reference an outer joined table in your criteria. Always put criteria in the join or use a derived table. I can't make sense of what works and what doesn't in your last examples -- can you provide some DDL and sample data so you can demonstrate?UPDATE: Looking at your example again, I see you are doing a LEFT OUTER JOIN to one table, and then from there joining that table via an INNER JOIN to another. That eliminates any NULL values returned from the outer join, so it essentially behaves the same as an INNER JOIN, causing your issue.- Jeffhttp://weblogs.sqlteam.com/JeffS |
 |
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2007-10-11 : 14:00:33
|
quote: Originally posted by jsmith8858 I can't make sense of what works and what doesn't in your last examples -- can you provide some DDL and sample data so you can demonstrate?
Brett, see this link for how to do it:http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/ |
 |
|
X002548
Not Just a Number
15586 Posts |
|
jsmith8858
Dr. Cross Join
7423 Posts |
|
X002548
Not Just a Number
15586 Posts |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-10-12 : 02:01:49
|
Double left join?SELECT pm.MEPrecID, pm.PIFRecID, x.Value AS StaffType, COALESCE(s.Present, 0) AS Present, COALESCE(s.Proposed, 0) AS Proposed, COALESCE(s.OnBoard, 0) AS OnBoardFROM XREF AS xLEFT JOIN PIFStaff AS s ON s.StaffType = x.ValueLEFT JOIN PIF_MEP AS pm ON pm.PIFRecID = s.PIFRecID AND pm.MEPRecID = 1WHERE x.TableName = 'StaffType' AND x.ColumnName = 'StaffType' E 12°55'05.25"N 56°04'39.16" |
 |
|
jsmith8858
Dr. Cross Join
7423 Posts |
|
|