Here's the query:
SELECT
DISTINCT
((substring(r2.email FROM position('@' IN r2.email) + 1))) AS unknown_domain,
count(DISTINCT (c2.id)) as contact_count,
count(DISTINCT (dc.id)) as dc_count
FROM
(SELECT
unnest(emails) AS email,
id
FROM contacts c
WHERE account_id = 612) AS r2
INNER JOIN domains d
ON d.domain = ((substring(r2.email FROM position('@' IN r2.email) + 1)))
INNER JOIN contacts c2
on c2.id = r2.id
INNER JOIN device_contacts dc
on dc.contact_id = c2.id
where d.freebie = 'f'
AND (NOT exists(SELECT 1
FROM (SELECT unnest(email_domains) :: CHARACTER VARYING AS domain
FROM companies) AS results
WHERE results.domain = ((substring(r2.email FROM position('@' IN r2.email) + 1)))))
AND (defunct = FALSE OR defunct IS NULL)
AND (dc.emails && ARRAY[r2.email]::character varying[])
GROUP BY unknown_domain
ORDER BY dc_count DESC, contact_count DESC;
I have no idea how to convert this into an active record query though. I've looked at numerous rails sites . I have no idea how to approach this at all though. I've tried a few different attempts, and all have failed.
Aucun commentaire:
Enregistrer un commentaire