How to join two SQL Server temporal tables using "for system_time" to see historic changes across two tables?
That is say tables: People, Pets within SQL Server each a temporal table. For example:
- People: ID, Name, Address, ValidFrom, ValidTo
- Pets: ID, PeopleID, Name, ValidFrom, ValidTo
Using "for system_time" (to include current and history) how does one "join" across these tables to see total joined history? That is for each row (current & history) from People, what was the pet(s) that were with them at this time.
By for each row I mean: "Select * from People for system_time all". To get rows from Pets is possible using "select * from Pets AS OF <date_time>" I understand, however, how do one create the people select table, and then include columns in the join from the Pets table using the "as of <date_time>" concept from SQL Server temporal tables.
Result may look like:
- Bob, Address 1, Rover
- Bob, Address 2, Rover
- Bob, Address 2, Rusty
- Bob, Address 3, Rusty
1条答案
按热度按时间vwkv1x7d1#
You must manually join intersected People and Pets periods.
Create tables:
Fill data:
Query history:
The best benefit of temporal table is that you can create a difficult view and query result for particular time point without any additional effort:
Result is one line for that time point