-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRelationshipIndex.sql
32 lines (31 loc) · 1.13 KB
/
RelationshipIndex.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
--------------------------------------------------------------------------------------------------------------------------------
-- RELATIONSHIPS (INDEX)
--------------------------------------------------------------------------------------------------------------------------------
-- Filters the Relationships table by the latest version for each topic and relationship key. For most use cases, this should be
-- the primary sources for retrieving topic relationships, since it excludes historical versions.
--------------------------------------------------------------------------------------------------------------------------------
CREATE
VIEW [dbo].[RelationshipIndex]
WITH SCHEMABINDING
AS
WITH Relationships AS (
SELECT Source_TopicID,
RelationshipKey,
Target_TopicID,
IsDeleted,
Version,
RowNumber = ROW_NUMBER() OVER (
PARTITION BY Source_TopicID,
RelationshipKey,
Target_TopicID
ORDER BY Version DESC
)
FROM [dbo].[Relationships]
)
SELECT Relationships.Source_TopicID,
Relationships.RelationshipKey,
Relationships.Target_TopicID,
Relationships.IsDeleted,
Relationships.Version
FROM Relationships
WHERE RowNumber = 1