I'm doing a union between two DB2 resultsets I'll call yellow and pink.
key, "yellow" as source
key, "pink" as source
The requirement is to omit from pink all those rows where key is in the yellow resultset. (Actually the union is between three resultsets but I have omitted the middle one.) How do I fit the EXCEPT in here?
What do you need to help with: the syntax, or the set operations? (Or both?)
Perhaps I'm not experienced enough, but this seems like a requirement that doesn't easily fit into SQL constructs. You could use EXCEPT, but you'd have to do this before adding the SOURCE column, because that would cause the otherwise identical "yellow" and "pink" records to differ. So you'd have to process "yellow" records twice (once with the EXCEPT clause, and once with the UNION clause).
Another approach could be to create a union including the SOURCE column, and then use a GROUP BY to keep only one version of each row. SOURCE would not be part of the group by, but you'd use MAX(SOURCE) which would choose "yellow" in rows that have both "pink" and "yellow" versions. Handling three colors this way could be tricky, but you could always use numbers to make the handling easier and translate it to a string later.
It would be probably good to try several approaches and see which one has the best execution plan.
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads