I have a database that tracks inventory movement. The main tables are Orders, Shipments, ShippedItems and Items. The relationships are: a Shipment may or may not have an Order, Shipments will have 1 or more Items and Items can be attached to 1 or more Shipments. The ShippedItems table is a link table and the PK is ShipmentId, ItemId giving the many to many relation.
Each of these 4 tables have associated history tables into which a row with the current (new) values inserted via triggers whenever a row is inserted, updated or deleted in the main table. Each of the history tables has a CreateDt and a uniquely named HistId (e.g. ShipmentsHistId) field. The main tables hold only the current values.
I now need to report on the state of the Items as of a particular point in time - specifically as of a given month end. Obviously, I need to pull the data from the history tables, but I'm stumbling on how to determine the latest values across all the history tables at any given point in time.
Using the same relationships as the main tables, results in multiple rows for each Item. I need a flattened row that contains fields from all four tables with the values as of a month end.
One thought I had was to create a FlatHistory table that would be updated with all the fields I need to report on whenever any of the main tables were changed. That seemed somewhat redundant, though it would make the reports run quickly since there wouldn't need to be any joins.
Another thought was that since I already have all the history, I just need to connect the history Ids to one another to get the complete picture. The Flat History table would then become pointers into the other history tables with a Create Date to determine which rows are the last ones for a month. Again, I'm stumped on how to get the latest history id in all of the tables when only one main table is changing.
If anyone has any suggestions as to how I can do this (and keep some hair on my head!), I would be ever so grateful!
Cheers, May
Each of these 4 tables have associated history tables into which a row with the current (new) values inserted via triggers whenever a row is inserted, updated or deleted in the main table. Each of the history tables has a CreateDt and a uniquely named HistId (e.g. ShipmentsHistId) field. The main tables hold only the current values.
I now need to report on the state of the Items as of a particular point in time - specifically as of a given month end. Obviously, I need to pull the data from the history tables, but I'm stumbling on how to determine the latest values across all the history tables at any given point in time.
Using the same relationships as the main tables, results in multiple rows for each Item. I need a flattened row that contains fields from all four tables with the values as of a month end.
One thought I had was to create a FlatHistory table that would be updated with all the fields I need to report on whenever any of the main tables were changed. That seemed somewhat redundant, though it would make the reports run quickly since there wouldn't need to be any joins.
Another thought was that since I already have all the history, I just need to connect the history Ids to one another to get the complete picture. The Flat History table would then become pointers into the other history tables with a Create Date to determine which rows are the last ones for a month. Again, I'm stumped on how to get the latest history id in all of the tables when only one main table is changing.
If anyone has any suggestions as to how I can do this (and keep some hair on my head!), I would be ever so grateful!
Cheers, May