SQL Server : find duplicate record by column value

半世苍凉 提交于 2021-02-11 18:16:06

问题


I have a value like below dataset. Now how I can find the duplicate DataSetID like: 201 & 401 is duplicate record.


回答1:


Use PIVOT and ROW_Number

For Non Duplicates

FIDDLE DEMO

SELECT * FROm Tbl WHERE DateSetID IN 
(
    SELECT DateSetID FROM 
    (
       SELECT DateSetID,[Name], [Age], [Gender],ROW_NUMBER() OVER (PARTITION BY [Name], [Age], 
        [Gender] ORDER BY DateSetID) RN
    FROM  (SELECT * FROM Tbl) AS SourceTable  
    PIVOT(MAX(ColumnB) FOR ColumnA IN ([Name], [Age], [Gender])
    ) AS PivotTable)Tmp WHERE RN = 1
);

For Duplicates alone

FIDDLE DEMO

SELECT T.* FROM Tbl T JOIN (
SELECT DatasetID, ColumnA, ColumnB
FROM 
(
   SELECT DatasetID, [Name], [Age], [Gender], ROW_NUMBER() OVER (PARTITION BY [Name], [Age], [Gender] ORDER BY DatasetID) RN
   FROM  (SELECT * FROM Tbl) AS SourceTable  
    PIVOT(MAX(ColumnB) FOR ColumnA IN ([Name], [Age], [Gender])) AS PivotTable
)Tmp 
UNPIVOT
(
    ColumnB
    FOR ColumnA in ([Name], [Age], [Gender])
) AS UnpivotOp
WHERE RN > 1
)X ON T.ColumnA = X.ColumnA AND T.ColumnB = X.ColumnB;



回答2:


You need to count pivoted rows using ubounded rows window (default)

SELECT * 
FROm Tbl 
WHERE DatasetID IN (
    SELECT DatasetID 
    FROM (
       SELECT DatasetID, [Name], [Age], [Gender]
         ,count(*) OVER (PARTITION BY [Name], [Age], [Gender]) cnt
       FROM Tbl  
       PIVOT(MAX(ColumnB) FOR ColumnA IN ([Name], [Age], [Gender])
     ) AS PivotTable
    )Tmp 
    WHERE cnt > 1
);

Fiddle




回答3:


concat the columns, and apply group by.

select distinct Datesetid from tableA
where concat(columnA, columnB) in (
    select  concat(columnA, columnB) from
    tableA
    group by concat(columnA, columnB)
    having count(1) > 1)

see dbfiddle.




回答4:


write give below query & find duplicate value in table

SELECT DISTINCT FirstName, LastName, MobileNo FROM CUSTOMER;



来源:https://stackoverflow.com/questions/59403985/sql-server-find-duplicate-record-by-column-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!