-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTopicIndex.sql
More file actions
42 lines (41 loc) · 1.39 KB
/
TopicIndex.sql
File metadata and controls
42 lines (41 loc) · 1.39 KB
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
--------------------------------------------------------------------------------------------------------------------------------
-- TOPIC (INDEX)
--------------------------------------------------------------------------------------------------------------------------------
-- Retrieves the latest version of the key attributes for each topic and pivots them into a single record for each topic. When
-- loading or reporting topics, it's often useful to start with the Key, ContentType, and ParentID; once those are established,
-- other attributes and relationships can be pulled. This helps in that process by making all of those items available in a
-- single query.
--------------------------------------------------------------------------------------------------------------------------------
CREATE
VIEW [dbo].[TopicIndex]
WITH SCHEMABINDING
AS
WITH KeyAttributes AS (
SELECT TopicID,
AttributeKey,
AttributeValue,
RowNumber = ROW_NUMBER() OVER (
PARTITION BY TopicID,
AttributeKey
ORDER BY Version DESC
)
FROM [dbo].[Attributes]
WHERE AttributeKey
IN ( 'Key',
'ParentID',
'ContentType'
)
)
SELECT TopicID,
ContentType,
ParentID,
[Key] AS 'TopicKey'
FROM KeyAttributes
PIVOT ( MIN(AttributeValue)
FOR AttributeKey IN (
[Key],
[ParentID],
[ContentType]
)
) AS Pvt
WHERE RowNumber = 1