1

enter image description here

I'm dealing with the following union between two different tables in an Oracle Database.

the first query refers to vessels that are part of the current fleet of the payer_no. The second query refers to all vessels transacted with at some time.

If an item appears only in the first query, I want a custom column that says "current fleet" and if an item only appears in the second query, I want a column that says "past fleet."

Output would look like this:

enter image description here

2
  • What should happen if an item (whatever that means) appears in BOTH members of the UNION? (On a separate note - especially if you expect the UNION to be disjoint anyway, and the members have no duplicate rows or you don't care if they have duplicate rows - use UNION ALL instead of UNION for better performance). Commented Dec 4, 2017 at 18:49
  • if an item appears in both, it needs to be tagged to the first query ("current fleet") Commented Dec 7, 2017 at 3:25

1 Answer 1

2

Pending clarification of what should happen if an "item" appears in both members of the UNION:

The usual way to do what you need is to add a column with hard-coded values and with the same name to both members of the union. It doesn't matter if one or both members is/are the result of a join, or whatever other simple or complicated computation.

select col_1, col_2, ... , col_n, 'Current Fleet' as status
from   ( big join or whatever here; do prefix columns with table aliases in a join! )
UNION    --  Or UNION ALL  !
select col_1, col_2, ... , col_n, 'Past Fleet'    as status
from   .........
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.