I am creating a view based on a system-versioned table that records account information. The primary key is an automatically-incremented "Account No". My period columns are "Valid From" and "Valid To". I also assign a new "Version No" each time the data changes.
Within this view, I would like to use the "Valid From" value from the most recent version of each account to represent the account's "Time Modified." Then, I would like to use the "Valid From" value from the oldest version of each account (i.e., where "Version No" = 1) to represent the account's "Time Created."
How should I do this?
1条答案
按热度按时间dluptydi1#
While it is possible to use
FOR SYSTEM_TIME ALL
and aggregation, it's probably easiest (and fastest) to just join the history table directly.You can also do this as an
APPLY
or scalar subquery. This may or may not be faster depending on the cardinalities involved. You should try both options.