SQLScholar
Programmer
Hey all,
I have a join that is used in many queries and stored procs. I keep thinking that i must be able to not have 3 joins all to the same table but i cant figure it out.
here is effectively what i have:
Now I cant change the data in the tables - they are what they are.
Whenever we want to pull back data from these tables we use coalesce. Priority goes to org, then org2, then org3.
The joins may join on 1,2 or even all 3 of the joins (all three is unlikely) but we only ever want the data on the first join that finds a record as ordered by above.
This has been bugging me for a while - not because its incredibly important just that its had me thinking that it shouldnt be three joins..... but i cant work out if its possible to make it into one join (and if its quicker).
Any advice much appreciated.
Dan
----------------------------------------
Be who you are and say what you feel, because those who mind don't matter and those who matter don't mind - Bernard Baruch
Computer Science is no more about computers than astronomy is about telescopes - EW Dijkstra
----------------------------------------
I have a join that is used in many queries and stored procs. I keep thinking that i must be able to not have 3 joins all to the same table but i cant figure it out.
here is effectively what i have:
SQL:
select * from table1 t1
LEFT JOIN dbo.tblMI_Team_Structure ORG ON T1.TeamName = ORG.TeamName
AND ORG.Practice IN ( '*All' )
LEFT JOIN dbo.tblMI_Team_Structure ORG2 ON T1.TeamName = ORG2.TeamNam
AND T1.Practice= ORG2.Practice
LEFT JOIN dbo.tblMI_Team_Structure ORG3 ON T1.TeamName = ORG3.TeamName AND org3.Practice = 'Non-Practice'
Now I cant change the data in the tables - they are what they are.
Whenever we want to pull back data from these tables we use coalesce. Priority goes to org, then org2, then org3.
The joins may join on 1,2 or even all 3 of the joins (all three is unlikely) but we only ever want the data on the first join that finds a record as ordered by above.
This has been bugging me for a while - not because its incredibly important just that its had me thinking that it shouldnt be three joins..... but i cant work out if its possible to make it into one join (and if its quicker).
Any advice much appreciated.
Dan
----------------------------------------
Be who you are and say what you feel, because those who mind don't matter and those who matter don't mind - Bernard Baruch
Computer Science is no more about computers than astronomy is about telescopes - EW Dijkstra
----------------------------------------