FROM vHWDesktopMonitor A
FULL OUTER JOIN ResourceAssociation B
on A._ResourceGuid = B.ParentResourceGuid
LEFT JOIN vComputer C
on B.ChildResourceGuid = C.Guid
LEFT JOIN vAsset D
on C.ChildResourceGuid = D._ResourceGuid
So the above will return
- All FROM A and ALL records from B (Full Outer between A, B)
- Only records from C that are in B (LEFT Between B and C)
- Only records from D that are in C (LEFT Between C and D)
However, if you apply any where clause limits, it could reduce records which otherwise would be kept due to the left or outer joins...
For example if A._ResourceGuid ='7' exists in A but isn't in B;
and you set where B._ResourceGuid ='7' then the A record would otherwise would be kept due to the full outer join would then be excluded (making the full outer join the same as an INNER JOIN)!
a Full outer would return data like this:
A B
7 7
2
3
if you add a where clause where B=7 then you may be expecting to get because of the full outer since you said return all records from both...
A B
7 7
2
But you would end up getting
A B
7 7
Because the where clause occurs AFTER the full outer and therefore reduces the A.2 record.
To compensate for this you either have to put teh limits on teh join before the full outer executes or handle it in a where clause (but this method is VERY messy and prone to error and performance issues)
So when using outer joins, you MUST put the limiting criteria on the JOIN itself like below..
FROM vHWDesktopMonitor A
FULL OUTER JOIN ResourceAssociation B
on A._ResourceGuid = B.ParentResourceGuid
and B._resourceGuid = '7'
LEFT JOIN vComputer C
on B.ChildResourceGuid = C.Guid
LEFT JOIN vAsset D
on C.ChildResourceGuid = D._ResourceGuid
You could also put it in the where clause but you must remember to account for all the outer joins on the table and include null values for the other (this is just messy and slow)
FROM vHWDesktopMonitor A
FULL OUTER JOIN ResourceAssociation B
on A._ResourceGuid = B.ParentResourceGuid
LEFT JOIN vComputer C
on B.ChildResourceGuid = C.Guid
LEFT JOIN vAsset D
on C.ChildResourceGuid = D._ResourceGuid
WHERE (A._ResourceGuid is null OR B.ParentResourceGuid ='7')