I'm designing a UNION query to merge two tables with customer informations in a oracle 11g database. The first table a is the 'major' source the second table b is a additional source with new and duplicate entries.
The duplicates in b can not be eliminated by using UNION in fact of not equal fields like the autoincremented ID which is necessary to select.
Table a
ID CUSTOMER_NUMBER NAME STREET
1 4711 Dirk Downstreet 4
2 4721 Hans Mainstreet 5
Table b
ID CUSTOMER_NUMBER NAME STREET
44 4711 Dirk Downstreet 4 <== Duplicate
4 4741 Harry Crossroad 9 <== new
Expected Result
ID CUSTOMER_NUMBER NAME STREET DATASOURCE
1 4711 Dirk Downstreet 4 SAP <== from a
2 4721 Hans Mainstreet 5 SAP <== from a
4 4741 Harry Crossroad 9 MANUAL <== from b
I'm pretty happy with the following - simplified - trial:
SELECT CUSTOMER_NUMBER,
MAX(ID) KEEP (DENSE_RANK FIRST ORDER BY DATASOURCE DESC) ID,
MAX(NAME) KEEP (DENSE_RANK FIRST ORDER BY DATASOURCE DESC) NAME,
MAX(STREET) KEEP (DENSE_RANK FIRST ORDER BY DATASOURCE DESC) STREET,
FROM
(SELECT "ID","CUSTOMER_NUMBER","NAME","STREET", 'SAP' as DATASOURCE FROM CUSTOMERS
UNION ALL
SELECT "ID","CUSTOMER_NUMBER","NAME","STREET", 'MANUAL' as DATASOURCE FROM CUSTOMERS_MANUAL) united
group by CUSTOMER_NUMBER
But I have to SELECT every single field by DENSE_RANK FIRST ORDER BY DATASOURCE DESC which are about 20 fields ...
Can anyone show me a better approch?