Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@gordonje
Created September 22, 2016 15:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gordonje/b620fc29f80a57087842d1f51f91f88e to your computer and use it in GitHub Desktop.
Save gordonje/b620fc29f80a57087842d1f51f91f88e to your computer and use it in GitHub Desktop.
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,
others."FILER_TYPE" AS other_filer_type,
links.id
FROM "FILER_LINKS_CD" links
JOIN (
SELECT DISTINCT "FILER_ID"
FROM "FILER_TO_FILER_TYPE_CD"
WHERE "FILER_TYPE" = 8
) cands
ON cands."FILER_ID" = links."FILER_ID_A"
JOIN "FILER_TO_FILER_TYPE_CD" others
ON links."FILER_ID_B" = others."FILER_ID"
-- union with all link records where FILER B is a candidate
UNION ALL
SELECT
links."FILER_ID_B" AS cand_filer_id,
links."FILER_ID_A" AS other_filer_id,
"LINK_TYPE" as link_type,
others."FILER_TYPE" AS other_filer_type,
links.id
FROM "FILER_LINKS_CD" links
JOIN (
SELECT DISTINCT "FILER_ID"
FROM "FILER_TO_FILER_TYPE_CD"
WHERE "FILER_TYPE" = 8
) cands
ON cands."FILER_ID" = links."FILER_ID_B"
JOIN "FILER_TO_FILER_TYPE_CD" others
ON links."FILER_ID_A" = others."FILER_ID"
) as foo
GROUP BY 1, 2, 3
HAVING COUNT(DISTINCT other_filer_type) > 1;
@gordonje
Copy link
Author

link_group  cand_filer_id  other_filer_id  other_filer_type_count  
----------  -------------  --------------  ----------------------  
12011       1005184        1277980         2                       
12011       1293665        1293663         2                       

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment