Repository navigation
Expand file tree
/
Copy pathquery.sh
More file actions
executable file
·595 lines (570 loc) · 18.5 KB
/
Copy pathquery.sh
File metadata and controls
executable file
·595 lines (570 loc) · 18.5 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
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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
#!/bin/bash
source config.sh
# All businesses
# QUERY="
# COPY (
# SELECT agglvl_code, size_code, year, qtr, month1_emplvl, month2_emplvl, month3_emplvl
# FROM data
# WHERE area_fips = 'US000'
# AND own_code = '0'
# AND industry_code = '10'
# ) TO '$OUTPUT/results/national.csv' DELIMITER ',' CSV HEADER;"
# All businesses, by month
# QUERY="
# COPY (
# SELECT
# year,
# unnest(array[(qtr::int - 1) * 3 + 1, (qtr::int - 1) * 3 + 2, (qtr::int - 1) * 3 + 3]) as month,
# unnest(array[month1_emplvl, month2_emplvl, month3_emplvl]) as emplvl
# FROM data
# WHERE data.area_fips = 'US000'
# AND data.own_code = '0'
# AND data.industry_code = '10'
# ) TO '$OUTPUT/results/national_by_month.csv' DELIMITER ',' CSV HEADER;"
# Tyler Private Healthcare
# QUERY="
# COPY (
# SELECT
# year,
# unnest(array[(qtr::int - 1) * 3 + 1, (qtr::int - 1) * 3 + 2, (qtr::int - 1) * 3 + 3]) as month,
# unnest(array[lq_month1_emplvl, lq_month2_emplvl, lq_month3_emplvl]) as lq_emplvl
# FROM data
# WHERE data.area_fips = 'C4634'
# AND data.own_code = '5'
# AND data.industry_code = '621'
# ) TO '$OUTPUT/results/tyler_healthcare.csv' DELIMITER ',' CSV HEADER;"
# # Change in avg_weekly_wage from 1st quarter 1990 to 1st quarter 2015
# QUERY="
# COPY (
# SELECT
# first.area_fips,
# first.agglvl_code,
# area_titles.area_title,
# (first.month1_emplvl + first.month2_emplvl + first.month3_emplvl) / 3 as avg_emplvl_1990,
# first.avg_wkly_wage as avg_wkly_wage_1990,
# (last.month1_emplvl + last.month2_emplvl + last.month3_emplvl) / 3 as avg_emplvl_2015,
# last.avg_wkly_wage as avg_wkly_wage_2015,
# FROM
# data as first,
# data as last,
# area_titles.
# annual_cpi
# WHERE
# first.year = '1990' AND
# first.qtr = '1' AND
# first.own_code = '0' AND
# first.industry_code = '10' AND
# (first.size_code = '0' OR first.size_code = '') AND
#
# last.year = '2015' AND
# last.qtr = '1' AND
# last.own_code = '0' AND
# last.industry_code = '10' AND
# (last.size_code = '0' OR last.size_code = '') AND
#
# first.area_fips = last.area_fips AND
# first.agglvl_code = last.agglvl_code AND
# area_titles.area_fips = first.area_fips AND
#
# annual_cpi.year = first.year
# ) TO '$OUTPUT/results/wages_by_area.csv' DELIMITER ',' CSV HEADER;"
# Private Tyler businesses by sector and month
# QUERY="
# COPY (
# SELECT
# data.industry_code,
# industry_titles.industry_title,
# year,
# unnest(array[(qtr::int - 1) * 3 + 1, (qtr::int - 1) * 3 + 2, (qtr::int - 1) * 3 + 3]) as month,
# unnest(array[month1_emplvl, month2_emplvl, month3_emplvl]) as emplvl
# FROM
# data,
# industry_titles
# WHERE
# data.area_fips = '48423'
# AND data.own_code = '5'
# AND data.size_code = '0'
# AND data.agglvl_code = '75'
# AND industry_titles.industry_code = data.industry_code
# ) TO '$OUTPUT/results/tyler_by_sector.csv' DELIMITER ',' CSV HEADER;"
# All private businesses
# QUERY="
# COPY (
# SELECT
# data.year,
# qtr,
# (month1_emplvl + month2_emplvl + month3_emplvl) / 3 as avg_emplvl,
# avg_wkly_wage,
# (avg_wkly_wage * 237 / cpi) as avg_wkly_wage_2015_dollars
# FROM
# data,
# annual_cpi
# WHERE data.area_fips = 'US000'
# AND data.own_code = '5'
# AND (data.size_code = '0' or data.size_code = '')
# AND data.industry_code = '10'
# AND annual_cpi.year = data.year
# ) TO '$OUTPUT/results/national_by_quarter_private.csv' DELIMITER ',' CSV HEADER;"
# Area change change w/ CPI adjustment
# QUERY="
# COPY (
# SELECT
# area_fips,
# agglvl_code,
# area_title,
# avg_wkly_wage_1990,
# avg_wkly_wage_1990_adj,
# avg_wkly_wage_2015,
# (avg_wkly_wage_2015 - avg_wkly_wage_1990_adj) as change,
# (avg_wkly_wage_2015 - avg_wkly_wage_1990_adj) / avg_wkly_wage_1990_adj as pct_change,
# power(abs(avg_wkly_wage_2015 / avg_wkly_wage_1990_adj), 1.0 / 25.0) - 1 as abs_avg_annual_pct_change
# FROM (
# SELECT
# first.area_fips,
# first.agglvl_code,
# area_titles.area_title,
# first.avg_wkly_wage as avg_wkly_wage_1990,
# (first.avg_wkly_wage * 237 / cpi) as avg_wkly_wage_1990_adj,
# last.avg_wkly_wage::real as avg_wkly_wage_2015
# FROM
# data as first,
# data as last,
# area_titles,
# annual_cpi
# WHERE
# first.year = '1990' AND
# first.qtr = '1' AND
# first.own_code = '0' AND
# first.industry_code = '10' AND
# (first.size_code = '0' OR first.size_code = '') AND
#
# last.year = '2015' AND
# last.qtr = '1' AND
# last.own_code = '0' AND
# last.industry_code = '10' AND
# (last.size_code = '0' OR last.size_code = '') AND
#
# first.area_fips = last.area_fips AND
# first.agglvl_code = last.agglvl_code AND
# area_titles.area_fips = first.area_fips AND
#
# annual_cpi.year = first.year
# ) sub
# ) TO '$OUTPUT/results/wages_by_area_adjusted.csv' DELIMITER ',' CSV HEADER;"
# Area wages by year w/ CPI adjustment
# QUERY="
# COPY (
# SELECT
# area_fips,
# area_title,
# agglvl_code,
# sub.year,
# (avg_wkly_wage * 237 / cpi) as avg_wkly_wage_2015_dollars
# FROM (
# SELECT
# data.area_fips,
# area_title,
# data.agglvl_code,
# data.year,
# AVG(avg_wkly_wage) as avg_wkly_wage
# FROM
# data,
# area_titles
# WHERE
# own_code = '0' AND
# industry_code = '10' AND
# (size_code = '0' OR size_code = '') AND
#
# area_titles.area_fips = data.area_fips
# GROUP BY
# data.area_fips,
# area_title,
# data.agglvl_code,
# data.year
# ) sub,
# annual_cpi
# WHERE
# sub.year = annual_cpi.year
# ORDER BY
# sub.area_fips,
# sub.year
# ) TO '$OUTPUT/results/wages_by_year_adjusted.csv' DELIMITER ',' CSV HEADER;"
# Manhattan (New York county) industry mix
# FIPS="36061"
# AGGLVL="75"
# OWN="5"
#
# QUERY="
# COPY (
# SELECT
# industry_code,
# industry_title,
# avg_wkly_wage_1990,
# avg_wkly_wage_1990_adj,
# avg_wkly_wage_2015,
# (avg_wkly_wage_2015 - avg_wkly_wage_1990_adj) AS wage_change,
# (avg_wkly_wage_2015 - avg_wkly_wage_1990_adj) / NULLIF(avg_wkly_wage_1990_adj, 0) AS wage_pct_change,
# avg_emplvl_1990,
# avg_emplvl_2015,
# (avg_emplvl_2015 - avg_emplvl_1990) AS emplvl_change,
# (avg_emplvl_2015 - avg_emplvl_1990) / NULLIF(avg_emplvl_1990, 0) AS emplvl_pct_change
# FROM (
# SELECT
# first.industry_code,
# industry_titles.industry_title,
# first.avg_wkly_wage as avg_wkly_wage_1990,
# (first.avg_wkly_wage * 237 / cpi) AS avg_wkly_wage_1990_adj,
# last.avg_wkly_wage::real AS avg_wkly_wage_2015,
# (first.month1_emplvl + first.month2_emplvl + first.month3_emplvl) / 3 AS avg_emplvl_1990,
# (last.month1_emplvl + last.month2_emplvl + last.month3_emplvl) / 3::real AS avg_emplvl_2015
# FROM
# data as first,
# data as last,
# industry_titles,
# annual_cpi
# WHERE
# first.area_fips = '$FIPS' AND
# first.year = '1990' AND
# first.qtr = '1' AND
# first.agglvl_code = '$AGGLVL' AND
# first.own_code = '$OWN' AND
#
# last.area_fips = '$FIPS' AND
# last.year = '2015' AND
# last.qtr = '1' AND
# last.agglvl_code = '$AGGLVL' AND
# last.own_code = '$OWN' AND
#
# first.industry_code = last.industry_code AND
#
# industry_titles.industry_code = first.industry_code AND
#
# annual_cpi.year = first.year
# ) sub
# ) TO '$OUTPUT/results/manhattan_wages_by_sector_adjusted.csv' DELIMITER ',' CSV HEADER;"
# Wages and employment for all industries in a single place
# New York County: 36061
# Bronx County: 36005
# Kings County: 36047
# Queens County: 36081
# Richmond County: 36085
# FIPS="36061"
# REGION="new_york_county"
#
# QUERY="
# COPY (
# SELECT
# industry_code,
# industry_title,
# agglvl_code,
# sub.year,
# avg_qrtly_emplvl,
# (avg_wkly_wage * 237 / cpi) as avg_wkly_wage_2015_dollars,
# (avg_total_qrtrly_wages * 237 / cpi) as avg_total_qrtrly_wages_2015_dollars
# FROM (
# SELECT
# data.industry_code,
# industry_title,
# data.agglvl_code,
# data.year,
# AVG((month1_emplvl + month2_emplvl + month3_emplvl) / 3) as avg_qrtly_emplvl,
# AVG(avg_wkly_wage) as avg_wkly_wage,
# AVG(total_qtrly_wages) as avg_total_qrtrly_wages
# FROM
# data,
# industry_titles
# WHERE
# area_fips = '$FIPS' AND
# own_code = '5' AND
# (size_code = '0' OR size_code = '') AND
#
# industry_titles.industry_code = data.industry_code
# GROUP BY
# data.industry_code,
# industry_title,
# data.agglvl_code,
# data.year
# ) sub,
# annual_cpi
# WHERE
# sub.year = annual_cpi.year
# ORDER BY
# sub.industry_code,
# sub.year
# ) TO '$OUTPUT/results/${REGION}_wages_by_industry.csv' DELIMITER ',' CSV HEADER;"
# Employment time series
# QUERY="
# COPY (
# SELECT
# data.area_fips,
# area_title,
# data.agglvl_code,
# data.year,
# AVG((month1_emplvl + month2_emplvl + month3_emplvl) / 3) as avg_qrtly_emplvl
# FROM
# data,
# area_titles
# WHERE
# own_code = '0' AND
# industry_code = '10' AND
# (size_code = '0' OR size_code = '') AND
#
# area_titles.area_fips = data.area_fips
# GROUP BY
# data.area_fips,
# area_title,
# data.agglvl_code,
# data.year
# ORDER BY
# data.area_fips,
# data.year
# ) TO '$OUTPUT/results/employment_by_year_adjusted.csv' DELIMITER ',' CSV HEADER;"
# All counties industry mix
# FIPS="36061"
# AGGLVL="75"
# OWN="5"
# QUERY="
# COPY (
# SELECT
# area_fips,
# area_title,
# industry_code,
# industry_title,
# avg_wkly_wage_1990,
# avg_wkly_wage_1990_adj,
# avg_wkly_wage_2015,
# (avg_wkly_wage_2015 - avg_wkly_wage_1990_adj) AS wage_change,
# (avg_wkly_wage_2015 - avg_wkly_wage_1990_adj) / NULLIF(avg_wkly_wage_1990_adj, 0) AS wage_pct_change,
# avg_emplvl_1990,
# avg_emplvl_2015,
# (avg_emplvl_2015 - avg_emplvl_1990) AS emplvl_change,
# (avg_emplvl_2015 - avg_emplvl_1990) / NULLIF(avg_emplvl_1990, 0) AS emplvl_pct_change
# FROM (
# SELECT
# first.area_fips,
# area_titles.area_title,
# first.industry_code,
# industry_titles.industry_title,
# first.avg_wkly_wage as avg_wkly_wage_1990,
# (first.avg_wkly_wage * 237 / cpi) AS avg_wkly_wage_1990_adj,
# last.avg_wkly_wage::real AS avg_wkly_wage_2015,
# (first.month1_emplvl + first.month2_emplvl + first.month3_emplvl) / 3 AS avg_emplvl_1990,
# (last.month1_emplvl + last.month2_emplvl + last.month3_emplvl) / 3::real AS avg_emplvl_2015
# FROM
# data as first,
# data as last,
# area_titles,
# industry_titles,
# annual_cpi
# WHERE
# first.year = '1990' AND
# first.qtr = '1' AND
# first.agglvl_code = '$AGGLVL' AND
# first.own_code = '$OWN' AND
#
# last.year = '2015' AND
# last.qtr = '1' AND
# last.agglvl_code = '$AGGLVL' AND
# last.own_code = '$OWN' AND
#
# first.area_fips = last.area_fips AND
# first.industry_code = last.industry_code AND
#
# area_titles.area_fips = first.area_fips AND
#
# industry_titles.industry_code = first.industry_code AND
#
# annual_cpi.year = first.year
# ) sub
# ) TO '$OUTPUT/results/area_wages_by_sector_adjusted.csv' DELIMITER ',' CSV HEADER;"
# Employment level for all counties, all industries, average of 2015 (for Yano)
# QUERY="
# COPY (
# SELECT
# area_fips,
# sub.industry_code,
# industry_title,
# avg_emplvl
# FROM
# (SELECT
# area_fips,
# data.industry_code,
# (sum(month1_emplvl) + sum(month2_emplvl) + sum(month3_emplvl)) / 12 as avg_emplvl
# FROM
# data
# WHERE
# year = '2015' AND
# own_code = '5' AND
# agglvl_code = '76' AND
# (size_code = '0' OR size_code = '')
# GROUP BY
# area_fips,
# data.industry_code,
# year
# ) sub, industry_titles
# WHERE
# industry_titles.industry_code = sub.industry_code
#
# ) TO '$OUTPUT/results/emplvl_all_fips_all_4d_naics_2015.csv' DELIMITER ',' CSV HEADER;"
# National totals by industry (for Yano)
# QUERY="
# COPY (
# SELECT
# sub.area_fips,
# sub.industry_code,
# industry_title,
# avg_emplvl
# FROM
# (SELECT
# area_fips,
# data.industry_code,
# (sum(month1_emplvl) + sum(month2_emplvl) + sum(month3_emplvl)) / 12 as avg_emplvl
# FROM
# data
# WHERE
# year = '2015' AND
# own_code = '5' AND
# agglvl_code = '16' AND
# (size_code = '0' OR size_code = '')
# GROUP BY
# area_fips,
# data.industry_code,
# year
# ) sub, industry_titles
# WHERE
# industry_titles.industry_code = sub.industry_code
#
# ) TO '$OUTPUT/results/emplvl_national_all_4d_naics_2015.csv' DELIMITER ',' CSV HEADER;"
# County totals (for Yano)
# QUERY="
# COPY (
# SELECT
# sub.area_fips,
# area_title,
# avg_emplvl
# FROM
# (SELECT
# area_fips,
# data.industry_code,
# (sum(month1_emplvl) + sum(month2_emplvl) + sum(month3_emplvl)) / 12 as avg_emplvl
# FROM
# data
# WHERE
# year = '2015' AND
# own_code = '5' AND
# agglvl_code = '71' AND
# (size_code = '0' OR size_code = '')
# GROUP BY
# area_fips,
# data.industry_code,
# year
# ) sub, area_titles
# WHERE
# area_titles.area_fips = sub.area_fips
#
# ) TO '$OUTPUT/results/emplvl_all_fips_total_2015.csv' DELIMITER ',' CSV HEADER;"
# All Tyler MSA employment by sector and month
# QUERY="
# COPY (
# SELECT
# data.industry_code,
# industry_titles.industry_title,
# year,
# unnest(array[(qtr::int - 1) * 3 + 1, (qtr::int - 1) * 3 + 2, (qtr::int - 1) * 3 + 3]) as month,
# unnest(array[month1_emplvl, month2_emplvl, month3_emplvl]) as emplvl
# FROM
# data,
# industry_titles
# WHERE
# data.area_fips = 'C4634' AND
# own_code = '5' AND
# (size_code = '0' OR size_code = '') AND
# data.agglvl_code = '44' AND
#
# industry_titles.industry_code = data.industry_code
# ) TO '$OUTPUT/results/tyler_by_sector.csv' DELIMITER ',' CSV HEADER;"
# QUERY="
# COPY (
# SELECT
# sub.area_fips,
# sub.industry_code,
# industry_title,
# avg_emplvl
# FROM
# (SELECT
# area_fips,
# data.industry_code,
# (sum(month1_emplvl) + sum(month2_emplvl) + sum(month3_emplvl)) / 12 as avg_emplvl
# FROM
# data
# WHERE
# year = '2015' AND
# own_code = '5' AND
# agglvl_code = '16' AND
# (size_code = '0' OR size_code = '')
# GROUP BY
# area_fips,
# data.industry_code,
# year
# ) sub, industry_titles
# WHERE
# industry_titles.industry_code = sub.industry_code
#
# ) TO '$OUTPUT/results/emplvl_national_all_4d_naics_2015.csv' DELIMITER ',' CSV HEADER;"
# Total monthly employment
# QUERY="
# COPY (
# SELECT
# year,
# data.area_fips,
# area_title,
# data.industry_code,
# industry_title,
# year,
# own_code,
# agglvl_code,
# unnest(array[(qtr::int - 1) * 3 + 1, (qtr::int - 1) * 3 + 2, (qtr::int - 1) * 3 + 3]) as month,
# unnest(array[month1_emplvl, month2_emplvl, month3_emplvl]) as emplvl
# FROM
# data,
# area_titles,
# industry_titles
# WHERE
# (agglvl_code IN ('11', '14', '15', '16', '17', '18')) AND
# (own_code = '0' OR own_code = '5') AND
# (size_code = '0' OR size_code = '') AND
# area_titles.area_fips = data.area_fips AND
# industry_titles.industry_code = data.industry_code
# ) TO '$OUTPUT/results/emplvl_national_all_naics_monthly.csv' DELIMITER ',' CSV HEADER;"
# Employment level for all counties, all industries, all years, 4d NAICS (for Dan)
QUERY="
COPY (
SELECT
sub.area_fips,
area_title,
sub.industry_code,
industry_title,
year,
avg_emplvl
FROM
(SELECT
area_fips,
data.industry_code,
year,
(sum(month1_emplvl) + sum(month2_emplvl) + sum(month3_emplvl)) / 12 as avg_emplvl
FROM
data
WHERE
own_code = '5' AND
agglvl_code = '76' AND
(size_code = '0' OR size_code = '')
GROUP BY
area_fips,
data.industry_code,
year
) sub, area_titles, industry_titles
WHERE
area_titles.area_fips = sub.area_fips AND
industry_titles.industry_code = sub.industry_code
) TO '$OUTPUT/results/emplvl_all_fips_all_4d_naics_all_years.csv' DELIMITER ',' CSV HEADER;"
$PSQL -q $DATABASE -c "$QUERY"