Last active
October 9, 2015 14:36
-
-
Save jmcarp/d49e3bf81472ddfd1191 to your computer and use it in GitHub Desktop.
sum aggregates by election
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| with cycles as ( | |
| select | |
| agg.*, | |
| link.election_year | |
| from ofec_sched_a_aggregate_state agg | |
| join ofec_name_linkage_mv link on | |
| agg.cmte_id = link.committee_id and | |
| agg.cycle <= link.election_year and | |
| agg.cycle > link.election_year - | |
| case link.committee_type | |
| when 'S' then 6 | |
| when 'P' then 4 | |
| else 2 | |
| end | |
| ) | |
| select | |
| cmte_id, | |
| election_year, | |
| state, | |
| max(state_full) as state_full, | |
| sum(total) as total, | |
| sum(count) as count | |
| from cycles | |
| group by | |
| cmte_id, | |
| election_year, | |
| state | |
| ; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment