Are col1, col2 and col3 going to be identical in all three tables?  You
don't need to know which table they came from?

Offhand it seems to me you want a subquery, something like this:

  Select first col1 col2 col3, src
  From (
    select col1, col2 col3, 'source 1'
    from table1
    where something
      union
    select col1, col2, col3,, 'source 2'
    from table2
    where something
      union 
    select col1, col2, col3, 'source 3'
    from table3
    where something)

...Hm, it seems my SQL has collected a year or two's rust; I'd have to think
out exactly how this would work.  But maybe this is enough of a hint for you
to get the rest of it (he finishes weakly).

---
Bob Bridges, [email protected], cell 336 382-7313

/* One of the most practical of our present safeguards of privacy is the
fragmented nature of personal information.  It is scattered in little bits
across the geography and years of our life.  Retrieval is impractical and
often impossible.  A central data bank removes completely this safeguard.
-Congressman Frank Horton, in the early 1970s before the Internet */

-----Original Message-----
From: IBM Mainframe Discussion List <[email protected]> On Behalf Of
Binyamin Dissen
Sent: Monday, April 18, 2022 13:48

Seems that I have been knocked off of the DB2-L listserv.

I am doing a union of three queries where it is possible that the critical
columns are in more than one of the queries.

For example:

                 select col1, col2 col3, 'source 1'
                          from table1
                          where something
                 union
                 select col1, col2, col3,, 'source 2'
                           from table2
                          where something
                 union 
                 select col1, col2, col3, 'source 3'
                            from table3
                            where something

I would like a single row even if the data (col1, col2, col3) is in more
than one of the queries, so that 'source1' is returned if in table1 and
table2 and/or table3, 'source 2' if not in table1 but in table2 (and perhaps
table3) and 'source 3' if only in table 3.

----------------------------------------------------------------------
For IBM-MAIN subscribe / signoff / archive access instructions,
send email to [email protected] with the message: INFO IBM-MAIN

Reply via email to