Skip to content

Instantly share code, notes, and snippets.

@jbothma
Created May 9, 2017 12:45
Show Gist options
  • Select an option

  • Save jbothma/512b93cf7c850abccf14ef30d85d4722 to your computer and use it in GitHub Desktop.

Select an option

Save jbothma/512b93cf7c850abccf14ef30d85d4722 to your computer and use it in GitHub Desktop.
Import wazimap voter turnout for 2016 municipal elections from all-ward-turnout dump obtained directly from IEC tech department
CREATE TEMPORARY TABLE voter_turnout
(
province TEXT,
muni TEXT,
ward TEXT,
registeredvoters TEXT,
mec7votes TEXT,
voterturnout TEXT,
percentvoterturnout TEXT
) ON COMMIT DROP;
\copy voter_turnout (province, muni, ward, registeredvoters, mec7votes, voterturnout, percentvoterturnout) FROM '/home/jdb/proj/code4sa/wazimap-za/2011-census-2016-boundaries/VoterTurnout_WardLevel_20170508_LGE2011 LGE2016.csv' DELIMITER ',' CSV HEADER;
-- wards
insert into voter_turnout_municipal_2016 (geo_level, geo_code, registered_voters, total_votes, geo_version) (
select 'ward', ward, registeredvoters::int + mec7votes::int, voterturnout::int, '2016' from voter_turnout);
-- wards into local and metro munis
insert into voter_turnout_municipal_2016 (geo_level, geo_code, registered_voters, total_votes, geo_version) (
select g.parent_level, g.parent_code, sum(registered_voters::int), sum(total_votes::int), '2016' from wazimap_geography g, voter_turnout_municipal_2016 v where g.version = '2016' and g.geo_code = v.geo_code and g.geo_level = v.geo_level and v.geo_level = 'ward' group by g.parent_code, g.parent_level) ;
-- local munis into district munis
insert into voter_turnout_municipal_2016 (geo_level, geo_code, registered_voters, total_votes, geo_version) (
select g.parent_level, g.parent_code, sum(registered_voters::int), sum(total_votes::int), '2016' from wazimap_geography g, voter_turnout_municipal_2016 v where g.version = '2016' and g.geo_code = v.geo_code and g.geo_level = v.geo_level and g.parent_level = 'district' group by g.parent_code, g.parent_level) ;
-- district and metro munis into provinces
insert into voter_turnout_municipal_2016 (geo_level, geo_code, registered_voters, total_votes, geo_version) (
select g.parent_level, g.parent_code, sum(registered_voters::int), sum(total_votes::int), '2016' from wazimap_geography g, voter_turnout_municipal_2016 v where g.version = '2016' and g.geo_code = v.geo_code and g.geo_level = v.geo_level and g.parent_level = 'province' group by g.parent_code, g.parent_level) ;
COMMIT;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment