-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathReferenceIndex.sql
29 lines (28 loc) · 1.07 KB
/
ReferenceIndex.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
--------------------------------------------------------------------------------------------------------------------------------
-- REFERENCES (INDEX)
--------------------------------------------------------------------------------------------------------------------------------
-- Filters the TopicReferences table by the latest version for each topic and reference key. For most use cases, this should be
-- the primary sources for retrieving topic references, since it excludes historical versions.
--------------------------------------------------------------------------------------------------------------------------------
CREATE
VIEW [dbo].[ReferenceIndex]
WITH SCHEMABINDING
AS
WITH TopicReferences AS (
SELECT Source_TopicID,
ReferenceKey,
Target_TopicID,
Version,
RowNumber = ROW_NUMBER() OVER (
PARTITION BY Source_TopicID,
ReferenceKey
ORDER BY Version DESC
)
FROM [dbo].[TopicReferences]
)
SELECT TopicReferences.Source_TopicID,
TopicReferences.ReferenceKey,
TopicReferences.Target_TopicID,
TopicReferences.Version
FROM TopicReferences
WHERE RowNumber = 1