-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathPMDB.Wbs formatted names and Udf values in Pivot.sql
192 lines (188 loc) · 4.55 KB
/
PMDB.Wbs formatted names and Udf values in Pivot.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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*-----------------------------------------------------------------------+
| Purpose: User defined fields formatted values
| Note: SQLCmdMode Script
+------------------------------------------------------------------------*/
:setvar _server "Server1"
:setvar _user "***username***"
:setvar _password "***password***"
:setvar _database "PMDB_1"
:connect $(_server) -U $(_user) -P $(_password)
USE [$(_database)];
GO
WITH
project_filter
AS
(
SELECT '12345' as proj_id
)
,
wbs_relate (wbs_id, parent_wbs_id, proj_id, wbs_format_name, wbs_short_name, wbs_name, wbs_level_nbr)
AS
(
SELECT
pwbs.wbs_id
, pwbs.parent_wbs_id
, pwbs.proj_id
, cast(pwbs.wbs_short_name as varchar(max))
, pwbs.wbs_short_name
, pwbs.wbs_name
, 0
FROM
PROJWBS pwbs
INNER JOIN PROJWBS rwbs ON pwbs.parent_wbs_id = rwbs.wbs_id
INNER JOIN project_filter pf ON pwbs.proj_id = pf.proj_id
WHERE
pwbs.proj_node_flag = 'Y' -- parent record
UNION ALL
SELECT
pwbs.wbs_id
, pwbs.parent_wbs_id
, pwbs.proj_id
, rwbs.wbs_format_name + '.' + pwbs.wbs_short_name
, pwbs.wbs_short_name
, pwbs.wbs_name
, wbs_level_nbr + 1
FROM
PROJWBS pwbs
INNER JOIN wbs_relate rwbs ON pwbs.parent_wbs_id = rwbs.wbs_id
INNER JOIN project_filter pf ON pwbs.proj_id = pf.proj_id
WHERE
pwbs.proj_node_flag = 'N' -- child record
)
,
udf_values_case
AS
(
SELECT
uv.proj_id
, ut.udf_type_label
, udf_value =
CASE
WHEN ut.logical_data_type IN('FT_TEXT','FT_STATICTYPE') THEN CONVERT(VARCHAR, uv.udf_text)
WHEN ut.logical_data_type IN('FT_START_DATE','FT_END_DATE') THEN REPLACE(CONVERT(VARCHAR(11),uv.udf_date,113),' ','-')
WHEN ut.logical_data_type IN('FT_FLOAT_2_DECIMALS','FT_INT', 'FT_MONEY') THEN CONVERT(VARCHAR, uv.udf_number)
ELSE '### The - ' + ut.logical_data_type + ' is NOT coded for. ###'
END
FROM
UDFTYPE ut
INNER JOIN UDFVALUE uv ON ut.udf_type_id = uv.udf_type_id
INNER JOIN project_filter pf ON uv.proj_id = pf.proj_id
)
,
activity_code_pivot
AS
(
SELECT
proj_id
, task_id
, [Region]
, [Discipline]
, [Asset Lead]
, [Responsible Engineer]
, [Priority]
FROM
(
SELECT
ta.proj_id
, ta.task_id
, ac.short_name
, at.actv_code_type
FROM
TASKACTV ta
LEFT JOIN ACTVTYPE at ON at.actv_code_type_id = ta.actv_code_type_id
LEFT JOIN ACTVCODE ac ON at.actv_code_type_id = ac.actv_code_type_id
INNER JOIN project_filter pf ON ta.proj_id = pf.proj_id
WHERE
1=1
AND at.actv_code_type IN('Region', 'Discipline', 'Asset Lead', 'Responsible Engineer', 'Priority')
) pL
PIVOT
(
MAX(short_name)
FOR actv_code_type
IN
(
[Region]
, [Discipline]
, [Asset Lead]
, [Responsible Engineer]
, [Priority]
)
) AS pvt
)
,
udf_values_pivot
AS
(
SELECT
proj_id
, task_id
, [Indicative Cost]
, [Control Budget]
, [Actual Cost]
, [Asset Location]
, [Focal Point]
, [Onsite Tech Support]
, [Specific Discipline]
FROM
(
SELECT
pj.proj_id
, tk.task_id
, uv.udf_type_label
, uv.udf_value
FROM
udf_values_case uv
INNER JOIN PROJECT pj ON pj.proj_id = uv.proj_id
INNER JOIN TASK tk ON pj.proj_id = tk.proj_id
INNER JOIN project_filter pf ON uv.proj_id = pf.proj_id
WHERE
1=1
AND tk.task_type IN('tt_mile','tt_finmile')
) pL
PIVOT
(
MAX(udf_value)
FOR udf_type_label
IN
(
[Indicative Cost]
, [Control Budget]
, [Actual Cost]
, [Asset Location]
, [Focal Point]
, [Onsite Tech Support]
, [Specific Discipline]
)
) AS pvt
)
SELECT
ac.[Region]
, ac.[Discipline]
, ac.[Asset Lead]
, udf.[Indicative Cost]
, [Activity ID] = tk.task_code
, [Activity Name] = tk.task_name
, [Activity % Complete] = tk.phys_complete_pct
, [Actual Start] = tk.act_start_date
, [Actual Finish] = tk.act_end_date
, [Start] = tk.early_start_date
, [Finish] = tk.early_end_date
, udf.[Control Budget]
, udf.[Actual Cost]
, [WBS Name] = wbs.wbs_name
, [WBS Path] = wbs.wbs_format_name
, udf.[Asset Location]
, udf.[Focal Point]
, udf.[Onsite Tech Support]
, udf.[Specific Discipline]
, ac.[Asset Lead1]
, ac.[Responsible Engineer]
, ac.[Asset Lead2]
, ac.[Priority]
FROM
wbs_relate wbs
INNER JOIN TASK tk ON tk.wbs_id = wbs.wbs_id AND tk.proj_id = wbs.proj_id
INNER JOIN udf_values_pivot udf ON udf.proj_id = tk.proj_id AND udf.task_id = tk.task_id
LEFT JOIN activity_code_pivot ac ON ac.task_id = tk.task_id
GO