Skip to content

Instantly share code, notes, and snippets.

@jmcarp
Last active October 9, 2015 14:36
Show Gist options
  • Select an option

  • Save jmcarp/d49e3bf81472ddfd1191 to your computer and use it in GitHub Desktop.

Select an option

Save jmcarp/d49e3bf81472ddfd1191 to your computer and use it in GitHub Desktop.
sum aggregates by election
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