-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcong_dist_queries.sql
544 lines (390 loc) · 10.2 KB
/
cong_dist_queries.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
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
/*--------------------------------------
----------------------------------------
A QUERY TO PRODUCE A TABLE TO
JOIN HISTORIC CONGRESSPERSONS TO
SHAPEFILE OF HISTORIC DISTRICTS.
----------------------------------------
--------------------------------------*/
--See all notes in gerrymandering_process.txt
---located in root of Gerrymandering folder.
----------------------------------------
--CREATE AND LOAD TABLES
----------------------------------------
---------------------------------
--TABLE: CONGRESSPERSON
---For congressperson data, originally from ISPCR data file.
---------------------------------
--CREATE TABLE
DROP TABLE IF EXISTS congressperson CASCADE;
CREATE TABLE congressperson
(
NUM integer,
ICPSR integer,
STARTCONGPERS integer,
STATENAME character varying(250),
DISTRICT character varying(250),
PARTY character varying(250),
POL_PARTY character varying(250),
-----------------------------------------------
--WHEN ADDING NEW COLUMN ADD LIKE "congressperson", LIKE SO:
----NOTE THAT COLUMN IS NOT IN COMMON BETWEEN BOTH TABLES.
----THIS AFFECTS HOW TO WRITE THE QUERY/QUERIES THAT FOLLOW.
CONGRESSPERSON character varying(250),
-----------------------------------------------
STARTPERS integer,
ENDPERS integer,
NOTES character varying(250),
NOTES2 character varying(250)
);
--LOAD TABLE
copy congressperson from
'D:\Gerrymandering\congressperson\congressperson.csv'
delimiter as ',' csv header;
--Open to check table
select * from congressperson
--Drop Notes column
ALTER TABLE congressperson
DROP COLUMN NOTES
RESTRICT;
--Drop Notes2 column
ALTER TABLE congressperson
DROP COLUMN NOTES2
RESTRICT;
--Drop NUM column
ALTER TABLE congressperson
DROP COLUMN NUM
RESTRICT;
--Open to check table
select * from congressperson
---------------------------------
--TABLE: DISTRICT_TABLE
---Exported from district shapefile
---------------------------------
--CREATE TABLE
DROP TABLE IF EXISTS district_table CASCADE;
CREATE TABLE district_table
(
FID integer,
STATENAME character varying(250),
-----------------------------------------------
--WHEN ADDING NEW COLUMN ADD LIKE "ICPSR", LIKE SO:
----NOTE THAT COLUMN IS NOT IN COMMON BETWEEN BOTH TABLES.
----THIS AFFECTS HOW TO WRITE THE QUERY/QUERIES THAT FOLLOW.
ID character varying(250),
-----------------------------------------------
DISTRICT character varying(250),
STARTCONG integer,
ENDCONG integer
);
--LOAD TABLE
copy district_table from
'D:\Gerrymandering\districts\districts_table.csv'
delimiter as ',' csv header;
--Drop NUM column
ALTER TABLE district_table
DROP COLUMN FID
RESTRICT;
--Open table
select * from district_table
--ORDER BY statename, district
;
---------------------------------
--TABLE: CONGRESSES
---For dates of congresses to join to District_Table
---------------------------------
--CREATE TABLE
DROP TABLE IF EXISTS congresses CASCADE;
CREATE TABLE congresses
(
CONGRESS integer,
STARTDIST integer,
ENDDIST integer,
NOTES character varying(250)
);
copy congresses from
'D:\Gerrymandering\congresses\congresses.csv'
delimiter as ',' csv header;
--Open table
select * from congresses;
ALTER TABLE congresses
DROP COLUMN NOTES;
---------------------
--JOINING DATES TO DISTRICT_TABLE
--FROM CONGRESS_TABLE
---------------------
--Joining below is very messy because of common columns.
--Clean up if possible.
--First join; Join CONGRESS to STARTCONG
DROP TABLE IF EXISTS district2 CASCADE;
SELECT * INTO district2
FROM congresses C
RIGHT JOIN district_table G
ON C.CONGRESS = G.STARTCONG;
--Open table
select * from district2;
--Drop Column
ALTER TABLE district2
DROP COLUMN ENDDIST
RESTRICT;
--Duplicate congress to remove start_dist
DROP TABLE IF EXISTS congresses2 CASCADE;
SELECT * INTO congresses2
FROM congresses;
--Open table
select * from congresses2;
--Drop Column
ALTER TABLE congresses2
DROP COLUMN STARTDIST
RESTRICT;
--Rename column so not duplicated
ALTER TABLE congresses2
RENAME COLUMN CONGRESS TO CONGRESS2;
--Second join; Join congress to end_cong
DROP TABLE IF EXISTS district3 CASCADE;
SELECT * INTO district3
FROM congresses2 C
RIGHT JOIN district2 G
ON C.CONGRESS2 = G.ENDCONG;
--DROP TABLE
DROP TABLE IF EXISTS congresses2 CASCADE;
--Open table
select * from district3;
--Remove columns
ALTER TABLE district3
DROP COLUMN congress,
DROP COLUMN congress2
RESTRICT;
--Drop tables
DROP TABLE IF EXISTS district_table CASCADE;
DROP TABLE IF EXISTS district2 CASCADE;
--Rename table
ALTER TABLE district3
RENAME TO district_table;
--Open table
select * from district_table;
--Correcting Nulls and '0' for At-Large
--- for both district_table and congressperson
--district_table
UPDATE district_table SET district = 99
WHERE
district = '0';
select distinct district from district_table
order by district;
--Congressperson
UPDATE congressperson SET district = 99
WHERE
district = '0';
select distinct district from congressperson
order by district;
select * from congressperson
order by district;
UPDATE congressperson SET district = 6
WHERE
district IS null
AND statename = 'Ohio';
UPDATE congressperson SET district = 99
WHERE
district is null
AND statename = 'Nevada';
---------------------------------
--TABLE: CONGRESSPERSON_DISTRICT
---Joining congressperson to district_table
---Final table for export
---------------------------------
--CREATE TABLE
---Single join
DROP TABLE IF EXISTS congressperson_district CASCADE;
select * into congressperson_district from (
select
r.STATENAME,
c.STARTPERS,
c.ENDPERS,
c.PARTY,
coalesce(c.DISTRICT, d.DISTRICT) district,
d.STARTDIST,
d.ENDDIST,
-----------------------------------------------
--WHEN ADDING COLUMNS THAT ARE NOT COMMON BETWEEN
----TABLES, ADD LIKE
----"congressperson" and
----"ICPSR", like so:
c.CONGRESSPERSON,
c.ICPSR,
d.ID,
-----------------------------------------------
--And more:
d.STARTCONG,
d.ENDCONG,
c.STARTCONGPERS,
c.POL_PARTY,
-------------------------
STARTCOMB,
ENDCOMB,
case when d.district is not null then STARTCOMB end FINALSTART,
case when d.district is not null then ENDCOMB end FINALEND
from (
with dates as (
select
*
from (
SELECT
c.STATENAME,
c.DISTRICT,
STARTPERS date
FROM congressperson c
union
SELECT
c.STATENAME,
c.DISTRICT,
ENDPERS
FROM congressperson c
union
SELECT
d.STATENAME,
d.DISTRICT,
STARTDIST
FROM district_table d
union
SELECT
d.STATENAME,
d.DISTRICT,
ENDDIST
FROM district_table d
) DATES
group by
STATENAME,
DISTRICT,
date
order by
STATENAME,
DISTRICT,
date
)
select
dates.STATENAME,
dates.DISTRICT,
dates.date STARTCOMB,
(select
d.date
from
dates d
where
d.STATENAME = dates.STATENAME and
d.DISTRICT = dates.DISTRICT and
d.date > dates.date
order by
d.date
limit 1
) ENDCOMB
from
dates) r
left join congressperson c on c.STATENAME = r.STATENAME and
c.DISTRICT = r.DISTRICT and
STARTCOMB between c.STARTPERS and c.ENDPERS and
ENDCOMB between c.STARTPERS and c.ENDPERS
left join district_table d on d.STATENAME = r.STATENAME and
d.district = r.district and
STARTCOMB between d.STARTDIST and d.ENDDIST and
ENDCOMB between d.STARTDIST and d.ENDDIST
where
ENDCOMB is not null
order by
r.STATENAME, coalesce(c.DISTRICT, d.DISTRICT), STARTCOMB, ENDCOMB, STARTPERS, ENDPERS
) t
--View table
select * from congressperson_district
order by finalstart desc;
--EXPORT TABLE AS CSV
COPY congressperson_district TO 'D:\congressperson_district.csv' DELIMITER ',' CSV HEADER;
--------------------
--Create reduced table for testing shapefile, removing the following:
---(1) Districts:
----From page 5 of codebook:
----final_start and final_end nulls
----district = '0' (At Large conversion)
----<0 = Unknown, -1
---->49 = General Ticket, At-Large, Unknown or Ascertained.
----Any NULL
---(2) finalstart > 0
--------------------
--------------------
--Turns out that district is a character rather than
---number, so operators won't work.
--Converting character to number.
--Add column dist_num
ALTER TABLE congressperson_district
ADD dist_num character varying(250);
--Transfer district to dist_num
UPDATE congressperson_district SET dist_num = district;
--Change dist_num type from character to integer
alter table "congressperson_district" alter column "dist_num" TYPE integer USING("dist_num"::integer)
--View
select * from congressperson_district
DROP TABLE IF EXISTS congressperson_district_reduced CASCADE;
select * into congressperson_district_reduced
from congressperson_district
where dist_num > '0'
and dist_num <'50'
and finalstart > 0
--order by finalstart desc;
--view table
select * from congressperson_district_reduced
--EXPORT TABLE AS CSV
--Does not work here:
---COPY congressperson_district_reduced TO 'C:\Users\nitty\Desktop\congressperson_district_reduced.csv' DELIMITER ',' CSV HEADER;
--Open up psql command prompt (type in windows menu) and copy:
\copy congressperson_district_reduced to 'C:\Users\nitty\Desktop\congressperson_district_reduced.csv' with delimiter ',' csv header encoding 'windows-1251';
--------
--ISSUES THAT NEED SOLVING
--------
--A few problems are appearing.
--Use the following queries to inspect on New Jersey:
--DISTRICT_TABLE, NEW JERSEY
select * from district_table
WHERE statename = 'New Jersey'
ORDER BY startdist, district
;
--CONGRESSPERSON TABLE, NEW JERSEY
select * from congressperson
WHERE statename = 'New Jersey'
ORDER BY startpers, district, icpsr
;
--CONGRESSPERSON_DISTRICT TABLE, NEW JERSEY
select * from congressperson_district
WHERE statename = 'New Jersey'
ORDER BY startcomb, district, icpsr
;
--Select distincts of whole tables
--DISTRICT_TABLE
select distinct district from district_table
--WHERE statename = 'New Jersey'
ORDER BY district
;
--CONGRESSPERSON TABLE
select distinct district from congressperson
ORDER BY district
;
select * from congressperson
ORDER BY district
;
--NULLS
--Looks like nulls in congressperson are only 2 for:
---Nevada in 1965-1967
---Ohio, 1817-1819
--Checking Ohio
/*
select * from congressperson
WHERE statename = 'Ohio'
ORDER BY startpers, district, icpsr
;
*/
-- Looks like null for Ohio should have been '6'
--- Fill in.
--Looks like null for Nevada should have been '0'
--- Fill in
/*
select * from congressperson
WHERE statename = 'Nevada'
ORDER BY startpers, district, icpsr
;
*/