I have a database that is storing historical data. It actually consists of two databases. There is a transactional database that contains all the current records, and then a historical database that contains all records over all periods of time. It is set up as follows
Transaction DB table
TableA
----------
TableAID (primary key)
col1
col2
...
Historical DB table
HIS_TableA
----------
HistoricalTableAID (primary key)
TimeStamp
TableAID
col1
col2
...
(note that there could be many HistoricalTableAID for one TableAID because of updates)
A trigger automatically inserts a new record into the historical table whenever a record is inserted or updated in the corresponding transactional table. Every table in the historical db only references other tables in the historical db and every table in the transactional db only references other tables in the transactional db. Now my problem occurs when I want to insert a record into HIS_TableB. HIS_TableB contains historical data that never changes and data is inserted directly into it rather than through a trigger (ie, there is no TableB). The problem is, I am given the ID for TableA and not the HistoricalTableAID so I need to get that.
HIS_TableB
----------
HistoricalTableBID (primary key)
TimeStamp
FK_HistoricalTableAID (foreign key)
...
So, as far as I can tell, there are two ways to do this.
Method 1) One would be to have a bit field that determines if a particular HistoricalTableAID is the current record for the TableAID.
Method 2) Another method would be to do
SELECT * FROM HIS_TableA a
INNER JOIN
(
SELECT Max(TimeStamp) as MaxTimeStamp, TableAID FROM HIS_TableA GROUP BY TableAID ) recentData
ON recentData.MaxTimeStamp = a.TimeStamp AND recentData.TableAID = a.TableAID
What do you smart people recommend. I know you have more DB experience than me so I really appreciate any advice
Transaction DB table
TableA
----------
TableAID (primary key)
col1
col2
...
Historical DB table
HIS_TableA
----------
HistoricalTableAID (primary key)
TimeStamp
TableAID
col1
col2
...
(note that there could be many HistoricalTableAID for one TableAID because of updates)
A trigger automatically inserts a new record into the historical table whenever a record is inserted or updated in the corresponding transactional table. Every table in the historical db only references other tables in the historical db and every table in the transactional db only references other tables in the transactional db. Now my problem occurs when I want to insert a record into HIS_TableB. HIS_TableB contains historical data that never changes and data is inserted directly into it rather than through a trigger (ie, there is no TableB). The problem is, I am given the ID for TableA and not the HistoricalTableAID so I need to get that.
HIS_TableB
----------
HistoricalTableBID (primary key)
TimeStamp
FK_HistoricalTableAID (foreign key)
...
So, as far as I can tell, there are two ways to do this.
Method 1) One would be to have a bit field that determines if a particular HistoricalTableAID is the current record for the TableAID.
Method 2) Another method would be to do
SELECT * FROM HIS_TableA a
INNER JOIN
(
SELECT Max(TimeStamp) as MaxTimeStamp, TableAID FROM HIS_TableA GROUP BY TableAID ) recentData
ON recentData.MaxTimeStamp = a.TimeStamp AND recentData.TableAID = a.TableAID
What do you smart people recommend. I know you have more DB experience than me so I really appreciate any advice