-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathPMDB.Wbs formatted names and Udf values in Pivot.sql
More file actions
144 lines (138 loc) · 4.59 KB
/
PMDB.Wbs formatted names and Udf values in Pivot.sql
File metadata and controls
144 lines (138 loc) · 4.59 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
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
/*-----------------------------------------------------------------------+
| Purpose: User-defined fields formatted values
| Note: Optimized SQL 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 AS (
SELECT
pwbs.wbs_id,
pwbs.parent_wbs_id,
pwbs.proj_id,
CAST(pwbs.wbs_short_name AS NVARCHAR(MAX)) AS wbs_format_name,
pwbs.wbs_short_name,
pwbs.wbs_name,
0 AS wbs_level_nbr
FROM PROJWBS pwbs
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,
rwbs.wbs_level_nbr + 1
FROM PROJWBS pwbs
JOIN wbs_relate rwbs ON pwbs.parent_wbs_id = rwbs.wbs_id
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,
CASE
WHEN ut.logical_data_type IN ('FT_TEXT','FT_STATICTYPE') THEN uv.udf_text
WHEN ut.logical_data_type IN ('FT_START_DATE','FT_END_DATE') THEN FORMAT(uv.udf_date, 'dd-MMM-yyyy')
WHEN ut.logical_data_type IN ('FT_FLOAT_2_DECIMALS','FT_INT', 'FT_MONEY') THEN CAST(uv.udf_number AS NVARCHAR)
ELSE '### The - ' + ut.logical_data_type + ' is NOT coded for. ###'
END AS udf_value
FROM UDFTYPE ut
JOIN UDFVALUE uv ON ut.udf_type_id = uv.udf_type_id
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
JOIN project_filter pf ON ta.proj_id = pf.proj_id
WHERE 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
JOIN PROJECT pj ON pj.proj_id = uv.proj_id
JOIN TASK tk ON pj.proj_id = tk.proj_id
JOIN project_filter pf ON uv.proj_id = pf.proj_id
WHERE 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],
tk.task_code AS [Activity ID],
tk.task_name AS [Activity Name],
tk.phys_complete_pct AS [Activity % Complete],
tk.act_start_date AS [Actual Start],
tk.act_end_date AS [Actual Finish],
tk.early_start_date AS [Start],
tk.early_end_date AS [Finish],
udf.[Control Budget],
udf.[Actual Cost],
wbs.wbs_name AS [WBS Name],
wbs.wbs_format_name AS [WBS Path],
udf.[Asset Location],
udf.[Focal Point],
udf.[Onsite Tech Support],
udf.[Specific Discipline],
ac.[Responsible Engineer],
ac.[Priority]
FROM wbs_relate wbs
JOIN TASK tk ON tk.wbs_id = wbs.wbs_id AND tk.proj_id = wbs.proj_id
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