MSSQL Server-在多个字段上查找重复项

时间:2018-09-18 10:34:58

标签: sql sql-server

我正在尝试查找gtid唯一的重复方,但是有重复的记录重复了名字,姓氏和DOB。因此,例如,可能有以下多个记录:

gtid:105,但以下示例的记录不应超过1条:

gtid : 105, first name : john, last name : smith , dob: 12/05/1992
gtid : 105, first name : john, last name : smith, dob: 12/05/1992 

可接受:

gtid : 105, first name : john, last name : smith , dob: 12/05/1992
gtid : 106, first name : john, last name : smith, dob: 12/05/1992 

我有以下内容,但不起作用:

SELECT DISTINCT GtId, CrmPartyId, LegalName, BusinessClass, RmFullName, PbeFullName, OverallClientStatus, OverallRpStatus, 

名字+''+姓氏全名,Dob 从Core.WeccoParty

4 个答案:

答案 0 :(得分:1)

您不希望gtid中的GROUP BY

SELECT FIRST_NAME, LAST_NAME, BIRTH_DATE,
       MIN(gtid), MAX(gdid), COUNT(*)
FROM Staging.WECCO_Party_In
GROUP BY FIRST_NAME, LAST_NAME, BIRTH_DATE
HAVING COUNT(*) > 1

答案 1 :(得分:0)

FIRST_NAME +''+ LAST_NAME应该在分组依据中:

 SELECT 
        FIRST_NAME + ' ' + LAST_NAME  AS NAME, BIRTH_DATE
    FROM
        Staging.WECCO_Party_In
    GROUP BY
    FIRST_NAME + ' ' + LAST_NAME, BIRTH_DATE
    HAVING COUNT(FIRST_NAME + ' ' + LAST_NAME+' '+BIRTH_DATE) > 1

答案 2 :(得分:0)

COUNT gdidGROUP BY子句中将其删除:

SELECT FIRST_NAME + ' ' + LAST_NAME Name, BIRTH_DATE, COUNT(*) TotalGdids
FROM Staging.WECCO_Party_In
GROUP BY FIRST_NAME + ' ' + LAST_NAME, BIRTH_DATE
HAVING COUNT(*)>1

答案 3 :(得分:0)

您必须在与之不同的子句的分组依据中使用相同的子句,请尝试以下查询

SELECT DISTINCT gtid,
    FIRST_NAME + ' ' + LAST_NAME  AS NAME, BIRTH_DATE, COUNT(*)
FROM
    Staging.WECCO_Party_In
GROUP BY
gtid,FIRST_NAME + ' ' + LAST_NAME, BIRTH_DATE
HAVING 
    COUNT(*) > 1