SQL duplicates with different primary keys

前端 未结 7 1334
青春惊慌失措
青春惊慌失措 2021-01-24 19:56

I have a table in this form:

id | firstname | lastname
---+-----------+----------
1  | alex      | marti
2  | mark      | finger
3  | alex      | marti
4  | ted          


        
相关标签:
7条回答
  • 2021-01-24 20:30
    select a.* from t a,
    (select first, last from t group by first, last having count(*) > 1) b
    where a.first = b.first and a.last = b.last
    
    0 讨论(0)
  • 2021-01-24 20:31
    Select Id, First_Name, Last_Name
    FROM
    (
    Select Id, First_Name, Last_Name,
    Count() Over (Partition By First_Name,Last_Name) Count
    From Emp
    ) AS T
    Where T.Count > 1
    
    0 讨论(0)
  • 2021-01-24 20:34

    I ran into the same problem and this is what I did to resolve it. First I identified the dups with the following query:

     SELECT COUNT(*) as num, ID, Firstname, Lastname FROM TableA GROUP BY ID, Firstname, Lastname;
    

    Then I created a temp table. called TempTableA Which had the same columns as TableA and extra Column called Dups, you will see why further.

    then I did the following insert:

    INSERT INTO TempTableA(Dups, ID, Firstname, Lastname) SELECT COUNT(*) as num, ID, Firstname, Lastname FROM TableA GROUP BY ID, Firstname, Lastname having count(*)>=1;
    

    By now you might know why we added an extra column called dups. anywho..

    After that I did the following delete statement:

    DELETE FROM TableA Where ID NOT IN (SELECT t.ID  FROM TempTableA t);
    

    And presto that did the work for me remove the rest of the dups.

    Its not a one step process but it did do the job right.

    NOTE: you need to change the tableA to the correct name that you have as well as the column names in order for it to work. Let me know if you run into any issues.

    0 讨论(0)
  • 2021-01-24 20:44
    --Remove Duplicate Rows with different ID SQL SERVER
    
    CREATE TABLE #TempTable
    (
        Id        int,
        Firstname varchar(20),
        Lastname  varchar(20)
    )
    
    INSERT INTO #TempTable( Id, Firstname, Lastname) SELECT min(Id)as Id, Firstname, Lastname 
    FROM UserTable 
    GROUP BY  Firstname, Lastname
    
    delete from UserTable where Id not in(select Id from #TempTable)
    
    drop #TempTable
    
    0 讨论(0)
  • 2021-01-24 20:48

    you can do the following to show all the id column values

    SELECT GROUP_CONCAT(DISTINCT id SEPARATOR ',') AS ids, firstname, lastname FROM t GROUP BY firstname, lastname HAVING COUNT(*) > 1
    

    This should show something like this:

    ids | firstname | lastname
    ----+-----------+----------
    1,3 | alex      | marti
    
    0 讨论(0)
  • 2021-01-24 20:49

    You need to aggregate the id. If you need only the ID of one of them, for, say, deletion, you could do:

    select max(id) id, firstname, lastname from t group by firstname, lastname having count(*) > 1
    

    If you want both id's and know there will never be more than 2, you could do the following:

    select min(id) minid, max(id) maxid, firstname, lastname from t group by firstname, lastname having count(*) > 1
    

    If you want all duplicates, along with their id's, you'll have to use a derived table, as in Nitin Midha's answer.

    0 讨论(0)
提交回复
热议问题