-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathVersionHistoryIndex.sql
50 lines (49 loc) · 1.35 KB
/
VersionHistoryIndex.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
--------------------------------------------------------------------------------------------------------------------------------
-- VERSION HISTORY (INDEX)
--------------------------------------------------------------------------------------------------------------------------------
-- Creates a list of the last five versions for every topic in the database. This is useful for basic version rollbacks, by
-- providing a list of recent versions that can be reverted to.
--------------------------------------------------------------------------------------------------------------------------------
CREATE
VIEW [dbo].[VersionHistoryIndex]
WITH SCHEMABINDING
AS
WITH TopicVersions
AS (
SELECT DISTINCT
TopicID,
Version,
RowNumber = ROW_NUMBER() OVER (
PARTITION BY TopicID
ORDER BY Version DESC
)
FROM (
SELECT TopicID,
Version
FROM [dbo].[Attributes]
GROUP BY TopicID,
Version
UNION
SELECT TopicID,
Version
FROM [dbo].[ExtendedAttributes]
GROUP BY TopicID,
Version
UNION
SELECT Source_TopicID AS TopicID,
Version
FROM [dbo].[TopicReferences]
GROUP BY Source_TopicID,
Version
UNION
SELECT Source_TopicID AS TopicID,
Version
FROM [dbo].[Relationships]
GROUP BY Source_TopicID,
Version
) AS VersionList
)
SELECT Versions.TopicId,
Version
FROM TopicVersions Versions
WHERE RowNumber <= 5