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