I am working in an oracle database and looking to get some queries to identify any changes of address from a customer. I am creating a report from the results in a daily batch process. I will simplify the table structure as an example.
I have 2 tables I am working with. Each is an audit table which keeps track of changes such as updates and inserts via a trigger. Each table also has a column to store the date of change.
One table contains a county code, the other contains the street address: t_county and t_address
t_county
-
id: NUMBER(9)
-
county: NUMBER(2)
-
date: DATE
t_address
-
id: NUMBER(9)
-
address: VARCHAR2(60)
-
date: DATE
I need to build a query which selects the county and address if either one has a row on a specific day.
Say there is a change of address on a specific day, but there is no change in the county. I would like for the query to have a result such as below
+---------+---------+--------+
| ID | County | Address|
+---------+---------+--------+
| 1 | NULL |123 test|
+---------+---------+--------+
If there is a change of county but the address is the same:
+---------+---------+--------+
| ID | County | Address|
+---------+---------+--------+
| 1 | 32 | NULL |
+---------+---------+--------+
How can I build such a query?
Best Answer
Here's one option, which uses
UNION
of those tables. Aggregate (MAX
) is used to display changes in both county and address in a single row; otherwise, you'd get two separate rows. This won't work correctly if there are several changes within a single date.