Certainly,
please assume the following:
1. accounts table: columns: account_number, creation_date, dealer_code
2. dealers table: columns: dealer_code, address
there are many queries using these tables.
some just query the accounts, some just the dealers, and some join them
using the dealer_code, and plotting:
account_number,dealer_code,address,creation_date
1,100,New York,Aug09
2,100,New York,Nov09
3,69,Washington,Jan09
4,100,New York,Mar09
5,71,Richmond,Aug09
....
a requirement came in to keep track of the different addresses that dealers change, and
whenever a query to join the tables is made, the dealer address shown, should be relevant
to the point in time that the account was created.
HOWEVER, no queries or reports that are using the tables should be changed.
that is, if I have a report that does:
select * from accounts left join dealers on accounts.dealer_code = dealers.dealer_code
it should stay exactly the same, just return the correct address per the creation date
of the account.
I thought of doing the following, create a history tracking table:
dealer_code, address, change_date
and create a view or function for either the accounts or dealers table (by using the same name as the table, and renaming the table, this way
no reports needs to be changed) that will do this trick.
The view will merely join the history tracking table, and select the correct address based on the closest oldest
change date to account creation date.
meaning if a dealer changed address in Jun09, and an account was created in Jul09, then the Jul09 account will get the new address,
while all accounts before Jun09 will get the old address.
so far so easy. THE PROBLEM is,
if I create this view instead of the accounts table, that the view will return the correct address,
I will have to modify all reports as they manually join the dealers table and
take the address from the dealers table.
If I create this view instead of the dealers table, how do I program it that in case it is joined
with an account, it checks for the correct address, and in case it is not joined with the accounts
table, simply returns the newest address?
I could have the dealers table always returned a list of all accounts, the dealer_no and the corresponding address,
however, in case the report wishes to query the dealers table, just to get the dealers list, it will have lots of duplicates.
or any other solution you can think of, will be great.
Thank you!