Finding duplicate values in a SQL table

后端 未结 30 3997
南旧
南旧 2020-11-21 13:18

It\'s easy to find duplicates with one field:

SELECT name, COUNT(email) 
FROM users
GROUP BY email
HAVING COUNT(email) > 1

So if we have

相关标签:
30条回答
  • 2020-11-21 14:03

    If you want to find duplicate data (by one or several criterias) and select the actual rows.

    with MYCTE as (
        SELECT DuplicateKey1
            ,DuplicateKey2 --optional
            ,count(*) X
        FROM MyTable
        group by DuplicateKey1, DuplicateKey2
        having count(*) > 1
    ) 
    SELECT E.*
    FROM MyTable E
    JOIN MYCTE cte
    ON E.DuplicateKey1=cte.DuplicateKey1
        AND E.DuplicateKey2=cte.DuplicateKey2
    ORDER BY E.DuplicateKey1, E.DuplicateKey2, CreatedAt
    

    http://developer.azurewebsites.net/2014/09/better-sql-group-by-find-duplicate-data/

    0 讨论(0)
  • To delete records whose names are duplicate

    ;WITH CTE AS    
    (
    
        SELECT ROW_NUMBER() OVER (PARTITION BY name ORDER BY name) AS T FROM     @YourTable    
    )
    
    DELETE FROM CTE WHERE T > 1
    
    0 讨论(0)
  • 2020-11-21 14:04

    To Check From duplicate Record in a table.

    select * from users s 
    where rowid < any 
    (select rowid from users k where s.name = k.name and s.email = k.email);
    

    or

    select * from users s 
    where rowid not in 
    (select max(rowid) from users k where s.name = k.name and s.email = k.email);
    

    To Delete the duplicate record in a table.

    delete from users s 
    where rowid < any 
    (select rowid from users k where s.name = k.name and s.email = k.email);
    

    or

    delete from users s 
    where rowid not in 
    (select max(rowid) from users k where s.name = k.name and s.email = k.email);
    
    0 讨论(0)
  • 2020-11-21 14:07

    Another easy way you can try this using analytic function as well:

    SELECT * from 
    
    (SELECT name, email,
    
    COUNT(name) OVER (PARTITION BY name, email) cnt 
    
    FROM users)
    
    WHERE cnt >1;
    
    0 讨论(0)
  • 2020-11-21 14:07

    You can use the SELECT DISTINCT keyword to get rid of duplicates. You can also filter by name and get everyone with that name on a table.

    0 讨论(0)
  • 2020-11-21 14:08

    Try this:

    SELECT name, email
    FROM users
    GROUP BY name, email
    HAVING ( COUNT(*) > 1 )
    
    0 讨论(0)
提交回复
热议问题