Created
September 22, 2016 15:45
-
-
Save gordonje/b620fc29f80a57087842d1f51f91f88e to your computer and use it in GitHub Desktop.
filer pairs where non-candidate is multiple types
This file contains 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
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; |
Author
gordonje
commented
Sep 22, 2016
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment