Skip to content

Instantly share code, notes, and snippets.

View gordonje's full-sized avatar

James Gordon gordonje

View GitHub Profile
@gordonje
gordonje / link_types_of_duplicate_links.sql
Created September 22, 2016 16:52
link types of duplicate links
@gordonje
gordonje / create_candidate_committees.sql
Created September 22, 2016 16:10
Create distinct candidate committees links
CREATE TABLE calaccess_processed_candidate_committees AS
SELECT
@link_type as link_group_id,
lu."CODE_DESC" AS link_type_description,
cand_filer_id,
committee_filer_id,
MIN(session) AS first_session,
MAX(session) AS last_session,
MIN(effective_date) AS first_effective_date,
MAX(effective_date) AS last_effective_date,
@gordonje
gordonje / filer_pairs_where_other_is_multiple_types.sql
Created September 22, 2016 15:45
filer pairs where non-candidate is multiple types
SELECT
@link_type as link_group,
cand_filer_id,
other_filer_id,
COUNT(DISTINCT other_filer_type) as filer_type_count
FROM (
SELECT
links."FILER_ID_A" AS cand_filer_id,
links."FILER_ID_B" AS other_filer_id,
"LINK_TYPE" as link_type,
@gordonje
gordonje / filer_a_and_b_type_combos.sql
Created September 21, 2016 21:18
Combos of filer a and filer b types
SELECT
a_types."DESCRIPTION" AS filer_a_type,
b_types."DESCRIPTION" AS filer_b_type,
COUNT(DISTINCT links.id)
FROM "FILER_LINKS_CD" links
JOIN (
SELECT "FILER_ID", "DESCRIPTION"
FROM "FILER_TO_FILER_TYPE_CD" f2ft
JOIN "FILER_TYPES_CD" ft
ON f2ft."FILER_TYPE" = ft."FILER_TYPE"
@gordonje
gordonje / 12011_filer_link_scenarios.sql
Created September 21, 2016 15:47
12011 filer_links scenarios
@gordonje
gordonje / filing_id_counts_by_table.sql
Created September 15, 2016 15:22
filing_id counts by table
SELECT table_name, filing_id_count
FROM (
SELECT 'CVR2_CAMPAIGN_DISCLOSURE_CD' as table_name, COUNT(DISTINCT a."FILING_ID") as filing_id_count
FROM "CVR2_CAMPAIGN_DISCLOSURE_CD" a
LEFT JOIN "FILINGS_CD" b
ON a."FILING_ID" = b."FILING_ID"
WHERE b."FILING_ID" IS NULL
UNION
SELECT 'CVR2_LOBBY_DISCLOSURE_CD' as table_name, COUNT(DISTINCT a."FILING_ID") as filing_id_count
FROM "CVR2_LOBBY_DISCLOSURE_CD" a
@gordonje
gordonje / filer_id_counts_by_table.sql
Created September 15, 2016 15:21
filer_id counts by table
SELECT foo.table_name, foo.filer_id_count
FROM (
SELECT 'BALLOT_MEASURES_CD' as table_name, COUNT(DISTINCT a."FILER_ID") as filer_id_count
FROM "BALLOT_MEASURES_CD" a
LEFT JOIN temp_filer_ids b
ON a."FILER_ID"::varchar = b.id
WHERE b.id IS NULL
UNION
SELECT 'CVR_CAMPAIGN_DISCLOSURE_CD' as table_name, COUNT(DISTINCT a."FILER_ID") as filer_id_count
FROM "CVR_CAMPAIGN_DISCLOSURE_CD" a
@gordonje
gordonje / with_filer_id.sql
Created September 13, 2016 15:31
joining f460s to filer_links
-- 1,471 uncaptured records
SELECT COUNT(DISTINCT cvr."FILER_ID")
FROM "CVR_CAMPAIGN_DISCLOSURE_CD" cvr
JOIN f460_summary f460
ON cvr."FILING_ID" = f460.filing_id
AND cvr."AMEND_ID" = f460.amend_id
JOIN "FILER_XREF_CD" x
ON x."XREF_ID" = cvr."FILER_ID"
LEFT JOIN (
SELECT "FILER_ID_A"::varchar as filer_id
@gordonje
gordonje / .sql
Created September 9, 2016 19:18
Compare F460 contribution and expenditure schedule and summary sheet totals
-- cases where there's a contribution total on the summary sheet, but not the schedule
select summ.filing_id, summ.amend_id, summ.monetary_contributions, a.combined_total
from f460_summary summ
join f460_schedule_a a
on summ.filing_id = a.filing_id
and summ.amend_id = a.amend_id
where summ.monetary_contributions > 0 and (a.combined_total is null or a.combined_total = 0)
order by 1 desc, 2;
-- cases where there's a contribution total on the schedule but not the summary sheet
@gordonje
gordonje / create_f460_schedule_a.sql
Created September 9, 2016 12:08
Pull apart F460 Summary totals
-- Monetary Contributions Received
CREATE TABLE f460_schedule_a AS
SELECT
cvr."FILING_ID" as filing_id,
cvr."AMEND_ID" as amend_id,
itemd."AMOUNT_A" as itemized_total,
unitemd."AMOUNT_A" as unitemized_total,
combined."AMOUNT_A" as combined_total
FROM "CVR_CAMPAIGN_DISCLOSURE_CD" cvr
-- get the itemized total