I have two huge database tables names "AR" and "All", and I am trying to match records in "AR" to "All", note here we don't have a unique identifier, so I am doing a kind of fuzzy matching using First Name, last name, dob and ssn to get the matches. My match query is working.
The All table has a column "MID" which I want to fetch for my every matched record, but when I try my query I get thousands of records. I searched a lot online but could not figure it out.
I am trying to get the first matched record from "All" table along with corresponding MId, for each and every record in my "AR" table. Can anyone help me out here. My Query is below:
Select distinct a.*,
r."MID"
from "public"."AR" a
inner join "public"."All" r
On ( a."cDOB" = r."cDOB"
and right(a."SSN",4) = right(r."SSN",4)
and left(a."Last Name",4) = left(r."LastName",4)
and (a."SSN"!='' or r."SSN"!='')
)
OR
( left(a."First Name",4) = left(r."FirstName",4)
and ( left(a."Last Name",4) = left(r."LastName",4)
OR right(a."Last Name",4) = right(r."LastName",4)
)
and ( right(a."SSN",4) = r."SSN"
OR a."cDOB" = r."cDOB"
)
and ( a."SSN"!=''
OR r."SSN"!=''
)
)
OR
( a."MelID (Original) " = r."Prp"
and a."cDOB" = r."cDOB"
and r."Prp"!=''
);
The query gives me the correct output if I remove r."MID", from the first line, but when I fetch r."MID" the output records are a lot with duplicates and what not.