SELECT
sub_cf.element_id
FROM
custom_field_value sub_cfv
INNER JOIN
custom_field sub_cf
ON sub_cfv.custom_field_id = sub_cf.id
INNER JOIN
contact sub_c
ON sub_c.id = sub_cf.element_id
WHERE
sub_cf.element_type=0
AND sub_cfv.value ILIKE ANY(ARRAY['%John%', '%XYZ%'])
GROUP BY sub_cf.element_id
HAVING COUNT(sub_cfv.value) > 1