This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to use DB2 Except in this context?

 
Thomas Kennedy
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm doing a union between two DB2 resultsets I'll call yellow and pink.

select
key, source
from
(
select distinct
key, "yellow" as source
from x


union all

select distinct
key, "pink" as source
from y

)

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?
 
Martin Vajsar
Sheriff
Pie
Posts: 3751
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic