Last active
December 20, 2017 21:09
-
-
Save yorek/26749c06dd469eae479d418199f94dbb to your computer and use it in GitHub Desktop.
get-latest-object-version
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
USE tempdb | |
GO | |
/* | |
Generate Sample Data | |
*/ | |
DROP TABLE IF EXISTS dbo.[Sample] | |
GO | |
WITH cte AS | |
( | |
SELECT TOP (10000000) ROW_NUMBER() OVER (ORDER BY a.object_id) AS N FROM sys.all_columns a, sys.all_columns b | |
) | |
SELECT | |
CAST(N AS INT) AS ObjectId, | |
CAST(RAND(CHECKSUM(NEWID())) * 10000 AS INT) AS VersionId | |
INTO | |
dbo.[Sample] | |
FROM | |
cte | |
GO | |
CREATE NONCLUSTERED INDEX ixc ON dbo.[Sample](ObjectId, VersionId DESC) | |
GO | |
/* | |
Get Last version | |
*/ | |
WITH cte AS | |
( | |
SELECT | |
ObjectId, | |
VersionId, | |
ROW_NUMBER() OVER (PARTITION BY ObjectId ORDER BY VersionId DESC) AS K | |
FROM | |
dbo.[Sample] | |
) | |
SELECT | |
* | |
FROM | |
cte | |
WHERE | |
ObjectId = 1234 AND K = 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment