• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

How to use DB2 Except in this context?

 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I brought this back from the farm where they grow the tiny ads:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic