Finding duplicate values in a SQL table

后端 未结 30 3998
南旧
南旧 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 13:47

    We can use having here which work on aggregate functions as shown below

    create table #TableB (id_account int, data int, [date] date)
    insert into #TableB values (1 ,-50, '10/20/2018'),
    (1, 20, '10/09/2018'),
    (2 ,-900, '10/01/2018'),
    (1 ,20, '09/25/2018'),
    (1 ,-100, '08/01/2018')  
    
    SELECT id_account , data, COUNT(*)
    FROM #TableB
    GROUP BY id_account , data
    HAVING COUNT(id_account) > 1
    
    drop table #TableB
    

    Here as two fields id_account and data are used with Count(*). So, it will give all the records which has more than one times same values in both columns.

    We some reason mistakely we had missed to add any constraints in SQL server table and the records has been inserted duplicate in all columns with front-end application. Then we can use below query to delete duplicate query from table.

    SELECT DISTINCT * INTO #TemNewTable FROM #OriginalTable
    TRUNCATE TABLE #OriginalTable
    INSERT INTO #OriginalTable SELECT * FROM #TemNewTable
    DROP TABLE #TemNewTable
    

    Here we have taken all the distinct records of the orignal table and deleted the records of original table. Again we inserted all the distinct values from new table to the original table and then deleted new table.

    0 讨论(0)
  • 2020-11-21 13:48

    Try the following:

    SELECT * FROM
    (
        SELECT Id, Name, Age, Comments, Row_Number() OVER(PARTITION BY Name, Age ORDER By Name)
            AS Rank 
            FROM Customers
    ) AS B WHERE Rank>1
    
    0 讨论(0)
  • 2020-11-21 13:48

    try this code

    WITH CTE AS
    
    ( SELECT Id, Name, Age, Comments, RN = ROW_NUMBER()OVER(PARTITION BY Name,Age ORDER BY ccn)
    FROM ccnmaster )
    select * from CTE 
    
    0 讨论(0)
  • 2020-11-21 13:51

    A little late to the party but I found a really cool workaround to finding all duplicate IDs:

    SELECT GROUP_CONCAT( id )
    FROM users
    GROUP BY email
    HAVING ( COUNT(email) > 1 )
    
    0 讨论(0)
  • 2020-11-21 13:51

    By Using CTE also we can find duplicate value like this

    with MyCTE
    as
    (
    select Name,EmailId,ROW_NUMBER() over(PARTITION BY EmailId order by id) as Duplicate from [Employees]
    
    )
    select * from MyCTE where Duplicate>1
    
    0 讨论(0)
  • 2020-11-21 13:52

    This is the easy thing I've come up with. It uses a common table expression (CTE) and a partition window (I think these features are in SQL 2008 and later).

    This example finds all students with duplicate name and dob. The fields you want to check for duplication go in the OVER clause. You can include any other fields you want in the projection.

    with cte (StudentId, Fname, LName, DOB, RowCnt)
    as (
    SELECT StudentId, FirstName, LastName, DateOfBirth as DOB, SUM(1) OVER (Partition By FirstName, LastName, DateOfBirth) as RowCnt
    FROM tblStudent
    )
    SELECT * from CTE where RowCnt > 1
    ORDER BY DOB, LName
    
    0 讨论(0)
提交回复
热议问题