Skip to content
Advertisement

Selecting most recent and specific version in each group of records, for multiple groups

The problem:
I have a table that records data rows in foo. Each time the row is updated, a new row is inserted along with a revision number. The table looks like:

Note that in the table the last record is a newer version of the first row.

Does anyone know of an efficient way to query for the latest version of the rows, ans a specific version of records? For instance, a query for rev=2 would return the 2, 3 and 4th row (not the replaced 1st row though) while a query for rev=1 yields those rows with rev <= 1 and in case of duplicated ids, the one with the higher revision number is chosen (record: 1, 2, 3).

I’m not actually sure if this is even possible in SQL Server…

I would not prefer to return the result in an iterative way.

Advertisement

Answer

To get only latest revisions:

To get a specific revision, in this case 1 (and if an item doesn’t have the revision yet the next smallest revision):

It might not be the most efficient way to do this, but right now I cannot figure a better way to do this.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement