100% sure that the first condition from the OR statement in the JOIN will be executed first?
Not. There is no guaranteed evaluation order, and even if where the whole expression will still be evaluated with the same value and will not affect which lines are matched in the connection.
Your query will produce the following result:
Name Percentage ---------- ----------- PA 100 PA 20 PA 80 PB 100
I think you are looking for something like this.
select P.Name, coalesce(D1.Percentage, D2.Percentage) as Percentage from Personnel as P left outer join Department as D1 on P.Dep_Code = D1.Code and P.SubDep_Code = D1.SubCode left outer join Department as D2 on P.Dep_Code = D2.Code and D2.SubCode = '*' where coalesce(D1.Percentage, D2.Percentage) is not null
You can try the queries here using SQL Server 2008. http://data.stackexchange.com/stackoverflow/qt/118492/
Mikael eriksson
source share