INSERT INTO
mk_clearbit.emails_to_process
WITH cte_new_signups AS
(
SELECT DISTINCT
LOWER(t2.email) AS email_address
FROM
analysis_contacts_for_clearbit t1
INNER JOIN
contacts t2
ON t1.contact_id = t2.contact_id
)
SELECT
t1.email_address,
GETDATE() AS created_at
FROM
cte_new_signups t1
LEFT JOIN
(
SELECT
DISTINCT
LOWER(email_address) AS email_address
FROM
mk_clearbit.emails_to_process
UNION
SELECT
DISTINCT
LOWER(mk_email)
FROM
mk_clearbit.persons
) AS t2
ON LOWER(t1.email_address) = LOWER(t2.email_address)
LEFT JOIN
(
SELECT
DISTINCT
LOWER(domain) AS domain
FROM
mk_clearbit.companies
WHERE
emailProvider = true
) t3
ON LOWER(SUBSTRING(t1.email_address FROM POSITION('@' IN t1.email_address) + 1 )) = LOWER(t3.domain)
WHERE
t2.email_address IS NULL
AND t3.domain IS NULL
AND LOWER(SUBSTRING(t1.email_address FROM POSITION('@' IN t1.email_address)+1)) NOT IN ('hotmail.com', 'qq.com', 'outlook.com')
AND LOWER(SUBSTRING(t1.email_address FROM POSITION('@' IN t1.email_address)+1)) NOT LIKE 'hotmail.%'
AND LOWER(SUBSTRING(t1.email_address FROM POSITION('@' IN t1.email_address)+1)) NOT LIKE 'yahoo.%'
AND LOWER(SUBSTRING(t1.email_address FROM POSITION('@' IN t1.email_address)+1)) NOT LIKE 'live.%'
;
INSERT INTO
mk_clearbit.domains_to_process
WITH cte_new_signups AS
(
SELECT DISTINCT
LOWER(SUBSTRING(t2.email FROM POSITION('@' IN t2.email)+1)) AS domain
FROM
analysis_contacts_for_clearbit t1
INNER JOIN
contacts t2
ON t1.contact_id = t2.contact_id
)
SELECT
t1.domain,
GETDATE() AS created_at
FROM
cte_new_signups t1
LEFT JOIN
(
SELECT
LOWER(domain) AS domain
FROM
mk_clearbit.domains_to_process
UNION
SELECT
LOWER(mk_domain)
FROM
mk_clearbit.companies
) AS t2
ON LOWER(t1.domain) = LOWER(t2.domain)
WHERE
t2.domain IS NULL
;