Wednesday, October 22, 2014

Compare Precinct Voting Lists over time

precinctid cnt_db090514 cnt_db091914 cnt_db100214 cnt_db102014 difference variance
101 939 907 911 911 28 1.0307354555
102 632 622 624 627 5 1.0079744817
103 689 680 681 682 7 1.0102639296
104 428 426 427 427 1 1.0023419204
105 414 404 408 408 6 1.0147058824
106 778 768 772 774 4 1.0051679587
107 946 914 920 922 24 1.0260303688
108 1082 1054 1056 1063 19 1.0178739417
110 687 683 685 688 -1 0.9985465116

...


So we have voter lists with consistent precincts we have collected over time and we want to know how they have changed.
-- start SQL for Postgres 3.5

SELECT table_name into vdb_list FROM information_schema.tables WHERE table_name LIKE 'voterdb%14';
 Select * from vdb_list;
 /* voterdb090514
    voterdb091014
    voterdb091914
    voterdb092314
    voterdb100214
    voterdb102014 */

---unautomated
 drop table db090514;
 drop table db091014;
 drop table db091914;
 drop table db092314;
 drop table db100214;
 drop table db102014;

 Select precinctid,Count(precinctid)
 into db090514
 From voterdb090514
 Where statuscode = 'A' and
 precinctid in (Select precincts42 from ld42)
 Group By precinctid
 Order By precinctid;

 Select precinctid,Count(precinctid)
 into db091014
 From voterdb091014
 Where statuscode = 'A' and
 precinctid in (Select precincts42 from ld42)
 Group By precinctid
 Order By precinctid;

 Select precinctid,Count(precinctid)
 into db091914
 From voterdb091914
 Where statuscode = 'A' and
 precinctid in (Select precincts42 from ld42)
 Group By precinctid
 Order By precinctid;

 Select precinctid,Count(precinctid)
 into db092314
 From voterdb092314
 Where statuscode = 'A' and
 precinctid in (Select precincts42 from ld42)
 Group By precinctid
 Order By precinctid;

 Select precinctid,Count(precinctid)
 into db100214
 From voterdb100214
 Where statuscode = 'A' and
 precinctid in (Select precincts42 from ld42)
 Group By precinctid
 Order By precinctid;

 Select precinctid,Count(precinctid)
 into db102014
 From voterdb102014
 Where statuscode = 'A' and
 precinctid in (Select precincts42 from ld42)
 Group By precinctid
 Order By precinctid;

 SELECT table_name into db_list FROM information_schema.tables WHERE table_name LIKE 'db%14';
 Select * from db_list;
 /* db090514
    db091014
    db091914
    db092314
    db100214
    db102014 */

Select
 db090514.precinctid as precinctid,
 db090514.count as cnt_db090514,
 db091014.count as cnt_db091014,
 db091914.count as cnt_db091914,
 db092314.count as cnt_db092314,
 db100214.count as cnt_db100214,
 db102014.count as cnt_db102014,
  (db090514.count - db102014.count) as difference,
 (db090514.count::float / db102014.count::float) as variance
From
    db090514,
    db091014,
    db091914,
    db092314,
    db100214,
    db102014
Where
 db090514.precinctid = db091014.precinctid and
 db091014.precinctid = db091914.precinctid and
 db091914.precinctid = db092314.precinctid and
 db092314.precinctid = db100214.precinctid and
 db100214.precinctid = db102014.precinctid
;

No comments:

Post a Comment