Skip to content
Advertisement

How do I merge OUTER JOIN and UNION results?

I have two tables, and I want ALL the data from both. If the tables have a matching AssetID, then join them on one row. If not, then on separate rows. A full outer join sounds like the right approach but I have a problem in how to select the keys depending on which table it comes from.

Goal:

Produces

TABLE C

and I’m missing the key

Advertisement

Answer

You can use coalesce to take the non-null assetID from whatever table has it:

Note: You probably don’t need the sub-queries, though, and omitting them can simplify the query considerably:

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement