Thursday, October 30, 2014

Three ballotcounted dispositions mapped per precinct for two elections plus their difference


precinctid | ge2013blank | ge2012blank | bdiff | ge2013count | ge2012count | cdiff | ge2013notcount | ge2012notcount | ncdiff

       801 |          38 |          63 |       -25 |           311 |           504 |        -193 |              339 |              121 |            218
       701 |          31 |          56 |       -25 |           387 |           578 |        -191 |              340 |              124 |            216
       611 |          32 |          54 |       -22 |           343 |           468 |        -125 |              206 |               59 |            147
       610 |          42 |          72 |       -30 |           567 |           734 |        -167 |              267 |               70 |            197
       609 |          44 |          82 |       -38 |           530 |           740 |        -210 |              335 |               87 |            248
       608 |          35 |          66 |       -31 |           413 |           570 |        -157 |              270 |               82 |            188
       607 |          28 |          49 |       -21 |           429 |           555 |        -126 |              218 |               71 |            147
       606 |          46 |          68 |       -22 |           449 |           637 |        -188 |              312 |              102 |            210
       605 |          21 |          36 |       -15 |           289 |           386 |         -97 |              158 |               46 |            112
       604 |          45 |          73 |       -28 |           389 |           578 |        -189 |              298 |               81 |            217
       603 |          50 |          77 |       -27 |           341 |           529 |        -188 |              298 |               83 |            215
...

This postgres SQL (9.5) takes the three ballotcounted dispositions (null, 0, 1) and maps them each per precinct for two elections and shows the difference for each disposition between the two elections. This was over 140 lines of SQL; so there must be a simpler but more optimized method of  obtaining the same result with the OVER or WITH statements. My joins are lacking in sophistication. However, despite the fact that my code is wordy and unreadable; there is no noticeable delay in function or speed. Political piece here.

drop table voterdb;
Select precinctid,
ballotcounted_1,
ballotcounted_2
Into voterdb
From
voterdb102914
where statuscode = 'A'
;

drop table tGE2013blank;
Select
Distinct(precinctid),
count(ballotcounted_1) as GE2013blank
Into tGE2013blank
From voterdb
Where
precinctid in (Select precincts42 from ld42) and
ballotcounted_1 = ''
Group By
precinctid,
ballotcounted_1
Order By
precinctid
DESC
;

drop table tGE2012blank;
Select
Distinct(precinctid),
count(ballotcounted_2) as GE2012blank
Into tGE2012blank
From voterdb
Where
precinctid in (Select precincts42 from ld42) and
ballotcounted_2 = ''
Group By
precinctid,
ballotcounted_2
Order By
precinctid
DESC
;

drop table tGE2013counted;
Select
Distinct(precinctid),
count(ballotcounted_1) as GE2013counted
Into tGE2013counted
From voterdb
Where
precinctid in (Select precincts42 from ld42) and
ballotcounted_1 = '1'
Group By
precinctid,
ballotcounted_1
Order By
precinctid
DESC
;

drop table tGE2012counted;
Select
Distinct(precinctid),
count(ballotcounted_2) as GE2012counted
Into tGE2012counted
From voterdb
Where
precinctid in (Select precincts42 from ld42) and
ballotcounted_2 = '1'
Group By
precinctid,
ballotcounted_2
Order By
precinctid
DESC
;

drop table tGE2013notcounted;
Select
Distinct(precinctid),
count(ballotcounted_1) as GE2013notcounted
Into tGE2013notcounted
From voterdb
Where
precinctid in (Select precincts42 from ld42) and
ballotcounted_1 = '0'
Group By
precinctid,
ballotcounted_1
Order By
precinctid
DESC
;

drop table tGE2012notcounted;
Select
Distinct(precinctid),
count(ballotcounted_2) as GE2012notcounted
Into tGE2012notcounted
From voterdb
Where
precinctid in (Select precincts42 from ld42) and
ballotcounted_2 = '0'
Group By
precinctid,
ballotcounted_2
Order By
precinctid
DESC
;

Select
tGE2013blank.precinctid,
tGE2013blank.GE2013blank,
tGE2012blank.GE2012blank,
(tGE2013blank.GE2013blank - tGE2012blank.GE2012blank) as blankdiff,
tGE2013counted.GE2013counted,
tGE2012counted.GE2012counted,
(tGE2013counted.GE2013counted - tGE2012counted.GE2012counted) as counteddiff,
tGE2013notcounted.GE2013notcounted,
tGE2012notcounted.GE2012notcounted,
(tGE2013notcounted.GE2013notcounted - tGE2012notcounted.GE2012notcounted) as notcounteddiff
From
tGE2012blank,
tGE2013blank,
tGE2013counted,
tGE2012counted,
tGE2013notcounted,
tGE2012notcounted
Where
tGE2012blank.precinctid = tGE2013blank.precinctid and
tGE2013blank.precinctid = tGE2012counted.precinctid and
tGE2012counted.precinctid = tGE2013counted.precinctid and
tGE2013counted.precinctid = tGE2012notcounted.precinctid and
tGE2012notcounted.precinctid = tGE2013notcounted.precinctid
Group By
tGE2013blank.precinctid,
tGE2013blank.GE2013blank,
tGE2012blank.GE2012blank,
tGE2013counted.GE2013counted,
tGE2012counted.GE2012counted,
tGE2013notcounted.GE2013notcounted,
tGE2012notcounted.GE2012notcounted
Order By
tGE2013blank.precinctid
DESC
;


No comments:

Post a Comment