-
Notifications
You must be signed in to change notification settings - Fork 224
/
Copy pathbalance_sheet_path_to_node_pdt.view.lkml
121 lines (117 loc) · 3.63 KB
/
balance_sheet_path_to_node_pdt.view.lkml
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#########################################################{
# PURPOSE
# This Persistent Derived Table (PDT) derives the full path to a node using RECURSIVE
# to navigate the parent-child relationships.
# For example, for Child Node Cash & Equivalents, the path is:
# Assets-->Current Assets-->Cash & Equivalents
#
# SOURCE
# Table `@{GCP_PROJECT}.@{REPORTING_DATASET}.BalanceSheet`
#
# REFERENCED BY
# View balance_sheet_hierarchy_selection_sdt
#
# UPDATE SCHEDULE
# triggered when distinct count of nodes changes (see datagroup trigger balance_sheet_node_count)
#########################################################}
view: balance_sheet_path_to_node_pdt {
derived_table: {
datagroup_trigger: balance_sheet_node_count
create_process: {
sql_step:
CREATE OR REPLACE TABLE ${SQL_TABLE_NAME} as
WITH
RECURSIVE n AS (
SELECT
Client,
ChartOfAccounts,
HierarchyName,
LanguageKey_SPRAS,
CAST(Level AS INT64) AS LevelNumber,
Parent,
COALESCE(REGEXP_REPLACE(ParentText,'Non[- ]Current','Noncurrent'),Parent) AS ParentText,
Node,
COALESCE(REGEXP_REPLACE(NodeText,'Non[- ]Current','Noncurrent'),Node) AS NodeText,
IsLeafNode
FROM
`@{GCP_PROJECT}.@{REPORTING_DATASET}.BalanceSheet`
GROUP BY
Client,
ChartOfAccounts,
HierarchyName,
LanguageKey_SPRAS,
LevelNumber,
Parent,
COALESCE(REGEXP_REPLACE(ParentText,'Non[- ]Current','Noncurrent'),Parent),
Node,
COALESCE(REGEXP_REPLACE(NodeText,'Non[- ]Current','Noncurrent'),Node),
IsLeafNode
),
iterations AS (
SELECT
Client,
ChartOfAccounts,
HierarchyName,
LanguageKey_SPRAS,
LevelNumber,
IsLeafNode,
Node,
NodeText,
Parent,
ParentText,
0 AS LevelSequenceNumber,
nodeText AS NodeTextPath_String,
Node AS NodePath_String,
CAST(LevelNumber as STRING) AS NodeLevelPath_String
FROM
n
WHERE
LevelNumber = 2
UNION ALL
SELECT
n.Client,
n.ChartOfAccounts,
n.HierarchyName,
n.LanguageKey_SPRAS,
n.LevelNumber,
n.IsLeafNode,
n.Node,
n.NodeText,
n.Parent,
n.ParentText,
LevelSequenceNumber+1 AS LevelSequenceNumber,
CONCAT(NodeTextPath_String, '-->',n.NodeText) AS NodeTextPath_String,
CONCAT(NodePath_String, '-->',n.Node) AS NodePath_String,
CONCAT(NodeLevelPath_String, '-->',CAST(n.LevelNumber AS STRING)) AS NodeLevelPath_String
FROM
n
JOIN
iterations i
ON
i.node = n.Parent
AND i.Client = n.Client
AND i.ChartOfAccounts = n.ChartOfAccounts
AND i.HierarchyName = n.HierarchyName
AND i.LanguageKey_SPRAS = n.LanguageKey_SPRAS
)
SELECT Client,
ChartOfAccounts,
HierarchyName,
LanguageKey_SPRAS,
IsLeafNode,
Node,
NodeText,
ParentText,
LevelNumber,
LevelSequenceNumber,
MAX(LevelNumber) OVER (PARTITION BY Client,ChartOfAccounts,HierarchyName) AS MaxLevelNumber,
NodeTextPath_String,
NodePath_String,
SPLIT(NodeTextPath_String,'-->') AS NodeTextPath,
SPLIT(NodePath_String,'-->') AS NodePath,
SPLIT(NodeLevelPath_String,'-->') AS NodeLevelPath
FROM iterations
;;
}
}
}