Question : Union

Is there any trick to use a UNION operator to combine these two queries and ELIMINATE THE DUPLICATES if the values in the first three columns are the same, but different in column4?

select 'a' column1, 'b' column2, 'c' column3, 'Note 1' column4 from dual
union all
select 'a' column1, 'b' column2, 'c' column3, 'Note 2' column4 from dual

result:
a      b      c      Note 1
a      b      c      Note 2

Need to get the first record only and ignore the 2nd one:
a      b      c      Note 1

Answer : Union

give each query a dummy column that uniquely identifies it
and then use row_number to pick just the first value


SELECT column1, column2, column3, column4
FROM (SELECT x.*, ROW_NUMBER() OVER (PARTITION BY column1, column2, column3 ORDER BY dummy) rn
FROM (SELECT 1 dummy, 'a' column1, 'b' column2, 'c' column3, 'Note 1' column4 FROM DUAL
UNION ALL
SELECT 2 dummy, 'a' column1, 'b' column2, 'c' column3, 'Note 2' column4 FROM DUAL) x)
WHERE rn = 1

Random Solutions  
 
programming4us programming4us