I have a table of contacts. The table contains a mobile_phone column as well as a home_phone column. I'd like to fetch all pairs of duplicate contacts where a pair is two contacts sharing a phone number.
Note that if contact A's mobile_phone matches contact B's home_phone, this is also a duplicate. Here is an example of three contacts that should match.
contact_id|mobile_phone|home_phone|other columns such as email.......|...
-------------------------------------------------------------------------
111 |9748777777 |1112312312|..................................|...
112 |1112312312 |null |..................................|...
113 |9748777777 |0001112222|..................................|...
Specifically, I would like to bring back a table where each row contains the contact_ids of the two matching contacts. For example,
||contact_id_a|contact_id_b||
||-------------------------||
|| 145155 | 145999 ||
|| 145158 | 145141 ||
With the help of @Erwin here enter link description here I was able to write a query close to what I am trying to achieve brings back a list of contact_ids of all contacts in the list that share a phone number with other contacts in the list.
SELECT c.contact_id
FROM contacts c
WHERE EXISTS (
SELECT FROM contacts x
WHERE (x.data->>'mobile_phone' is not null and x.data->>'mobile_phone' IN (c.data->>'mobile_phone', c.data->>'home_phone'))
OR (x.data->>'home_phone' is not null and x.data->>'home_phone' IN (c.data->>'mobile_phone', c.data->>'home_phone'))
AND x.contact_id <> c.contact_id -- except self
);
The output only contains contact_ids like this...
||contact_id||
--------------
|| 2341514 ||
|| 345141 ||
I'd like to bring back the contact_ids of matching contacts in a single row as shown above.