I have a table that looks like this
dbo.Box
ID SourceID OverrideQueueID
1 1 NULL
2 1 2
3 2 NULL
I need to figure out a way to say if the OverrideQueueID IS NULL then just do a join from dbo.Box to dbo.Source.ID, otherwise if OverrideQueueID IS NOT NULL join to the dbo.Queue.ID instead. Is this possible to do in one select since it is joining to different tables?
I am trying to do this without introducing a bunch of left joins if at all possible.
Try this. You can modify the WHERE clause to fit your needs.
I hope a union will help you, like given below.
One possible way:
You have two records for SourceID = 1.. One as NULL, another as the override. Which one wins, I would assume #2 ID, but could there be an even higher number that should take precidence?
Since you are probably concerned with the per source, it should be as simple as a max() and no joins at all... something like
The coalesce as applied will basically take any of the override queues and just replace them with its own source. So, for records 1 and 3 will point back to themselves as queue 1 and 3 to be applied. Only on sourceID = 1 where there are two records will the #2 queue supersede the #1 (NULL override) queue.
So, the max for sourceID = 1 will be queue 2 and for SourceID = 2 will be queue 3.