-
Notifications
You must be signed in to change notification settings - Fork 0
/
capital-planning.sql
471 lines (423 loc) · 13.7 KB
/
capital-planning.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
TRUNCATE
managing_code,
agency,
capital_project,
capital_project_fund,
capital_commitment_type,
capital_project_checkbook,
agency_budget,
budget_line,
capital_commitment,
capital_commitment_fund
CASCADE;
DROP TABLE IF EXISTS source_capital_commitment_id CASCADE;
INSERT INTO managing_code
SELECT DISTINCT
m_agency as id
FROM source_capital_project;
-- Move managing agencies from project table into agency table
INSERT INTO agency
SELECT DISTINCT
m_agency_acro as initials,
m_agency_name as name
FROM source_capital_project;
-- Move sponsoring agencies from commitment table into agency table
INSERT INTO agency
SELECT DISTINCT
s_agency_acro as initials,
s_agency_name as name
FROM source_capital_commitment
WHERE s_agency_acro NOT IN (SELECT initials FROM agency);
-- Move project source into project target
INSERT INTO capital_project
SELECT
m_agency AS managing_code,
proj_id AS id,
m_agency_acro AS managing_agency,
description,
min_date,
max_date,
-- The enum in the API database drops the oxford comma
-- This was unintentional but the simplest way to rectify
-- the data source with the API database is to coerce the
-- source value to drop the oxford comma
CASE
WHEN type_category = 'Fixed Asset' OR
type_category = 'Fixed Asset' OR
type_category IS NULL
THEN type_category::capital_project_category
WHEN type_category = 'ITT, Vehicles, and Equipment'
THEN 'ITT, Vehicles and Equipment'::capital_project_category
END AS category
FROM source_capital_project;
-- I know what you're thinking; this is a lot of repeated code.
-- It's tempting to transform the table and use a regex on the column name to try generalizing the logic.
-- However, this goes against the grain for Postgres and introducing python is yet another tool to manage.
-- Once you get beyond the gut check, this code is fine. In fact, it's simple and expressive.
-- Also, as much as there is repeated code, there is non-repeated code that needs to be specified somewhere.
-- Don't spend too much time worrying about it or trying to "fix" it.
-- adopt
INSERT INTO capital_project_fund
SELECT
gen_random_uuid() AS id,
m_agency AS managing_code,
proj_id AS capital_project_id,
'city-non-exempt'::capital_fund_category AS category,
'adopt' AS stage,
adopt_ccnonexempt AS value
FROM source_capital_project;
INSERT INTO capital_project_fund
SELECT
gen_random_uuid() AS id,
m_agency AS managing_code,
proj_id AS capital_project_id,
'city-exempt'::capital_fund_category AS category,
'adopt' AS stage,
adopt_ccexempt AS value
FROM source_capital_project;
INSERT INTO capital_project_fund
SELECT
gen_random_uuid() AS id,
m_agency AS managing_code,
proj_id AS capital_project_id,
'city-cost'::capital_fund_category AS category,
'adopt' AS stage,
adopt_citycost AS value
FROM source_capital_project;
INSERT INTO capital_project_fund
SELECT
gen_random_uuid() AS id,
m_agency AS managing_code,
proj_id AS capital_project_id,
'non-city-state'::capital_fund_category AS category,
'adopt' AS stage,
adopt_nccstate AS value
FROM source_capital_project;
INSERT INTO capital_project_fund
SELECT
gen_random_uuid() AS id,
m_agency AS managing_code,
proj_id AS capital_project_id,
'non-city-federal'::capital_fund_category AS category,
'adopt' AS stage,
adopt_nccfederal AS value
FROM source_capital_project;
INSERT INTO capital_project_fund
SELECT
gen_random_uuid() AS id,
m_agency AS managing_code,
proj_id AS capital_project_id,
'non-city-other'::capital_fund_category AS category,
'adopt' AS stage,
adopt_nccother AS value
FROM source_capital_project;
INSERT INTO capital_project_fund
SELECT
gen_random_uuid() AS id,
m_agency AS managing_code,
proj_id AS capital_project_id,
'non-city-cost'::capital_fund_category AS category,
'adopt' AS stage,
adopt_noncitycost AS value
FROM source_capital_project;
-- allocate
INSERT INTO capital_project_fund
SELECT
gen_random_uuid() AS id,
m_agency AS managing_code,
proj_id AS capital_project_id,
'city-non-exempt'::capital_fund_category AS category,
'allocate' AS stage,
allocate_ccnonexempt AS value
FROM source_capital_project;
INSERT INTO capital_project_fund
SELECT
gen_random_uuid() AS id,
m_agency AS managing_code,
proj_id AS capital_project_id,
'city-exempt'::capital_fund_category AS category,
'allocate' AS stage,
allocate_ccexempt AS value
FROM source_capital_project;
INSERT INTO capital_project_fund
SELECT
gen_random_uuid() AS id,
m_agency AS managing_code,
proj_id AS capital_project_id,
'city-cost'::capital_fund_category AS category,
'allocate' AS stage,
allocate_citycost AS value
FROM source_capital_project;
INSERT INTO capital_project_fund
SELECT
gen_random_uuid() AS id,
m_agency AS managing_code,
proj_id AS capital_project_id,
'non-city-state'::capital_fund_category AS category,
'allocate' AS stage,
allocate_nccstate AS value
FROM source_capital_project;
INSERT INTO capital_project_fund
SELECT
gen_random_uuid() AS id,
m_agency AS managing_code,
proj_id AS capital_project_id,
'non-city-federal'::capital_fund_category AS category,
'allocate' AS stage,
allocate_nccfederal AS value
FROM source_capital_project;
INSERT INTO capital_project_fund
SELECT
gen_random_uuid() AS id,
m_agency AS managing_code,
proj_id AS capital_project_id,
'non-city-other'::capital_fund_category AS category,
'allocate' AS stage,
allocate_nccother AS value
FROM source_capital_project;
-- commit
INSERT INTO capital_project_fund
SELECT
gen_random_uuid() AS id,
m_agency AS managing_code,
proj_id AS capital_project_id,
'city-non-exempt'::capital_fund_category AS category,
'commit' AS stage,
commit_ccnonexempt AS value
FROM source_capital_project;
INSERT INTO capital_project_fund
SELECT
gen_random_uuid() AS id,
m_agency AS managing_code,
proj_id AS capital_project_id,
'city-exempt'::capital_fund_category AS category,
'commit' AS stage,
commit_ccexempt AS value
FROM source_capital_project;
INSERT INTO capital_project_fund
SELECT
gen_random_uuid() AS id,
m_agency AS managing_code,
proj_id AS capital_project_id,
'city-cost'::capital_fund_category AS category,
'commit' AS stage,
commit_citycost AS value
FROM source_capital_project;
INSERT INTO capital_project_fund
SELECT
gen_random_uuid() AS id,
m_agency AS managing_code,
proj_id AS capital_project_id,
'non-city-state'::capital_fund_category AS category,
'commit' AS stage,
commit_nccstate AS value
FROM source_capital_project;
INSERT INTO capital_project_fund
SELECT
gen_random_uuid() AS id,
m_agency AS managing_code,
proj_id AS capital_project_id,
'non-city-federal'::capital_fund_category AS category,
'commit' AS stage,
commit_nccfederal AS value
FROM source_capital_project;
INSERT INTO capital_project_fund
SELECT
gen_random_uuid() AS id,
m_agency AS managing_code,
proj_id AS capital_project_id,
'non-city-other'::capital_fund_category AS category,
'commit' AS stage,
commit_nccother AS value
FROM source_capital_project;
INSERT INTO capital_project_fund
SELECT
gen_random_uuid() AS id,
m_agency AS managing_code,
proj_id AS capital_project_id,
'non-city-cost'::capital_fund_category AS category,
'commit' AS stage,
commit_noncitycost AS value
FROM source_capital_project;
-- spent
INSERT INTO capital_project_fund
SELECT
gen_random_uuid() AS id,
m_agency AS managing_code,
proj_id AS capital_project_id,
'city-non-exempt'::capital_fund_category AS category,
'spent' AS stage,
spent_ccnonexempt AS value
FROM source_capital_project;
INSERT INTO capital_project_fund
SELECT
gen_random_uuid() AS id,
m_agency AS managing_code,
proj_id AS capital_project_id,
'city-exempt'::capital_fund_category AS category,
'spent' AS stage,
spent_ccexempt AS value
FROM source_capital_project;
INSERT INTO capital_project_fund
SELECT
gen_random_uuid() AS id,
m_agency AS managing_code,
proj_id AS capital_project_id,
'city-cost'::capital_fund_category AS category,
'spent' AS stage,
spent_citycost AS value
FROM source_capital_project;
INSERT INTO capital_project_fund
SELECT
gen_random_uuid() AS id,
m_agency AS managing_code,
proj_id AS capital_project_id,
'non-city-state'::capital_fund_category AS category,
'spent' AS stage,
spent_nccstate AS value
FROM source_capital_project;
INSERT INTO capital_project_fund
SELECT
gen_random_uuid() AS id,
m_agency AS managing_code,
proj_id AS capital_project_id,
'non-city-federal'::capital_fund_category AS category,
'spent' AS stage,
spent_nccfederal AS value
FROM source_capital_project;
INSERT INTO capital_project_fund
SELECT
gen_random_uuid() AS id,
m_agency AS managing_code,
proj_id AS capital_project_id,
'non-city-other'::capital_fund_category AS category,
'spent' AS stage,
spent_nccother AS value
FROM source_capital_project;
INSERT INTO capital_project_fund
SELECT
gen_random_uuid() AS id,
m_agency AS managing_code,
proj_id AS capital_project_id,
'non-city-cost'::capital_fund_category AS category,
'spent' AS stage,
spent_noncitycost AS value
FROM source_capital_project;
-- Move data from commitment source to commitment_type
INSERT INTO capital_commitment_type
SELECT DISTINCT
commitment_code AS code,
commitment_description AS description
FROM source_capital_commitment;
-- Move data from commitment source to agency_budget
INSERT INTO agency_budget
SELECT DISTINCT
SPLIT_PART(budget_line, '-', 1) AS code,
project_type AS type,
s_agency_acro AS sponsor
FROM source_capital_commitment;
-- Move data from commitment source to budget_line
INSERT INTO budget_line
SELECT DISTINCT
SPLIT_PART(budget_line, '-', 1) AS code,
SPLIT_PART(budget_line, '-', 2) AS id
FROM source_capital_commitment;
-- Provide uuids to commitment source
SELECT gen_random_uuid() as id, *
INTO source_capital_commitment_id
FROM source_capital_commitment;
-- Move data from commitment source id to commitment
INSERT INTO capital_commitment
SELECT
id,
commitment_code AS type,
TO_DATE(plan_comm_date, 'MM/YY') AS planned_date,
m_agency AS managing_code,
project_id AS capital_project_id,
SPLIT_PART(budget_line, '-', 1) AS budget_line_code,
SPLIT_PART(budget_line, '-', 2) AS budget_line_id
FROM source_capital_commitment_id;
-- Move data from commitment source id to commitment_fund
INSERT INTO capital_commitment_fund
SELECT
gen_random_uuid() AS id,
id AS capital_commitment_id,
'city-non-exempt'::capital_fund_category AS category,
plannedcommit_ccnonexempt AS value
FROM source_capital_commitment_id;
INSERT INTO capital_commitment_fund
SELECT
gen_random_uuid() AS id,
id AS capital_commitment_id,
'city-exempt'::capital_fund_category AS category,
plannedcommit_ccexempt AS value
FROM source_capital_commitment_id;
INSERT INTO capital_commitment_fund
SELECT
gen_random_uuid() AS id,
id AS capital_commitment_id,
'non-city-state'::capital_fund_category AS category,
plannedcommit_nccstate AS value
FROM source_capital_commitment_id;
INSERT INTO capital_commitment_fund
SELECT
gen_random_uuid() AS id,
id AS capital_commitment_id,
'non-city-federal'::capital_fund_category AS category,
plannedcommit_nccfederal AS value
FROM source_capital_commitment_id;
INSERT INTO capital_commitment_fund
SELECT
gen_random_uuid() AS id,
id AS capital_commitment_id,
'non-city-other'::capital_fund_category AS category,
plannedcommit_nccother AS value
FROM source_capital_commitment_id;
INSERT INTO capital_commitment_fund
SELECT
gen_random_uuid() AS id,
id AS capital_commitment_id,
'non-city-cost'::capital_fund_category AS category,
plannedcommit_noncitycost AS value
FROM source_capital_commitment_id;
INSERT INTO capital_commitment_fund
SELECT
gen_random_uuid() AS id,
id AS capital_commitment_id,
'total'::capital_fund_category AS category,
plannedcommit_total AS value
FROM source_capital_commitment_id;
WITH capital_project_spatial AS (
SELECT
COALESCE(source_capital_project_m_poly.magency, source_capital_project_m_pnt.magency) AS managing_code,
COALESCE(source_capital_project_m_poly.projectid, source_capital_project_m_pnt.projectid) id,
source_capital_project_m_poly.wkt AS m_poly,
source_capital_project_m_pnt.wkt AS m_pnt
FROM source_capital_project_m_poly
FULL OUTER JOIN source_capital_project_m_pnt
ON source_capital_project_m_poly.magency = source_capital_project_m_pnt.magency AND
source_capital_project_m_poly.projectid = source_capital_project_m_pnt.projectid
)
UPDATE capital_project
-- Need to transform to 2263 because it is imported as 4326
SET li_ft_m_pnt = ST_Transform(capital_project_spatial.m_pnt, 2263),
li_ft_m_poly = ST_Transform(capital_project_spatial.m_poly, 2263),
mercator_label = CASE
WHEN capital_project_spatial.m_pnt IS NOT NULL THEN ST_Transform(ST_PointOnSurface(capital_project_spatial.m_pnt), 3857)
WHEN capital_project_spatial.m_poly IS NOT NULL THEN ST_Transform((ST_MaximumInscribedCircle(capital_project_spatial.m_poly)).center, 3857)
END,
mercator_fill_m_pnt = ST_Transform(capital_project_spatial.m_pnt, 3857),
mercator_fill_m_poly = ST_Transform(capital_project_spatial.m_poly, 3857)
FROM capital_project_spatial
WHERE capital_project_spatial.managing_code = capital_project.managing_code AND
capital_project_spatial.id = capital_project.id;
COPY managing_code TO '/var/lib/postgresql/data/managing_code.csv';
COPY agency TO '/var/lib/postgresql/data/agency.csv';
COPY capital_project TO '/var/lib/postgresql/data/capital_project.csv';
COPY capital_project_fund TO '/var/lib/postgresql/data/capital_project_fund.csv';
COPY capital_commitment_type TO '/var/lib/postgresql/data/capital_commitment_type.csv';
COPY capital_project_checkbook TO '/var/lib/postgresql/data/capital_project_checkbook.csv';
COPY agency_budget TO '/var/lib/postgresql/data/agency_budget.csv';
COPY budget_line TO '/var/lib/postgresql/data/budget_line.csv';
COPY capital_commitment TO '/var/lib/postgresql/data/capital_commitment.csv';
COPY capital_commitment_fund TO '/var/lib/postgresql/data/capital_commitment_fund.csv';