SQL获得第一个匹配的行

时间:2017-02-25 22:32:11

标签: sql postgresql inner-join greatest-n-per-group

我有两个巨大的数据库表名称" AR"和"全部",我正在尝试匹配" AR"至于"全部",请注意,我们没有唯一的标识符,因此我使用名字,姓氏,dob和ssn进行一种模糊匹配以获得匹配。我的匹配查询正在运行。

All表格中有一列" MID"我想为我的每个匹配记录获取,但当我尝试我的查询时,我得到了数千条记录。我在网上搜索了很多,但无法弄明白。

我正在尝试从" All"获得第一个匹配的记录。表格以及相应的MId,用于我的" AR"中的每一条记录。表。有人可以帮我从这里出去吗。我的查询如下:

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"!=''
            );

如果我从第一行删除r。" MID",但是当我获取r时,查询给出了正确的输出。" MID"输出记录很多都有重复,而不是。

2 个答案:

答案 0 :(得分:0)

我认为问题在于您正在使用3 OR条件进行内部联接,因此当记录与多个条件匹配时,您会得到重复项。尝试下面你离开加入" MID"表3次,只保留至少一个匹配的结果。

Select distinct a.*, 
       nvl(nvl(r."MID",r2."MID"),r3."MID") as MID
  from "public"."AR" a
       left 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"!='')  
             )
        left join "public"."All" r2 
           On (     left(a."First Name",4) = left(r2."FirstName",4)
              and (   left(a."Last Name",4) = left(r2."LastName",4) 
                   OR right(a."Last Name",4) = right(r2."LastName",4)
                  )
              and (   right(a."SSN",4) = r2."SSN" 
                   OR a."cDOB" = r2."cDOB"
                  ) 
              and (   a."SSN"!='' 
                   OR r2."SSN"!=''
                  )
            )
        left join "public"."All" r3 
            (    a."MelID (Original) " = r3."Prp" 
             and a."cDOB" = r3."cDOB" 
             and r3."Prp"!=''
            )
    WHERE (r."MID" IS NOT NULL OR r2."MID" IS NOT NULL OR r3."MID" IS NOT NULL)
;

答案 1 :(得分:0)

要为MID中的每一行从All获取“第一个”AR,您可以使用DISTINCT ON

SELECT DISTINCT ON (a.undisclosed_pk_column)
       a.*, r."MID"
FROM ...
...
ORDER  BY a.undisclosed_pk_column, r.undisclosed_columns_defining_first;

相关: