如何从SQL表中选择重复记录?

时间:2011-07-08 18:23:51

标签: sql-server-2005 sql-server-2008

SQL表

如何选择重复记录,我需要使用游标???

以下是我的查询..

select RxNbr,[Fill Date],NDC,GenericCode 
    from vw_TempTransaction 
    order by GenericCode Asc

如果列:RxNbr和列:[填充日期]具有相同的值并重复多次,我需要选择那些rxnbr / [填充日期]记录。

3 个答案:

答案 0 :(得分:1)

SELECT t.RxNbr, t.[Fill Date], t.NDC, t.GenericCode
    FROM vw_TempTransaction t
        INNER JOIN (SELECT RxNbr, [Fill Date]
                        FROM vw_TempTransaction 
                        GROUP BY RxNbr, [Fill Date]
                        HAVING COUNT(*) > 1
                   ) q
            ON t.RxNbr = q.RxNbr
                AND t.[Fill Date] = q.[Fill Date]
    ORDER BY t.GenericCode ASC

答案 1 :(得分:1)

我会这样做:

with duplicated as (

    select      RxNbr,
                [Fill Date]
    from        vw_TempTransaction
    group by    RxNbr,
                [Fill Date]
    having      count(*) > 1

)
select      a.RxNbr,
            a.[Fill Date],
            a.NDC,
            a.GenericCode
from        vw_TempTransaction a
inner join  duplicated b
on          a.RxNbr = b.RxNbr
and         a.[Fill Date] = b.[Fill Date]
order by    a.GenericCode Asc

答案 2 :(得分:0)

以下示例使用校验和查找重复记录

CREATE TABLE #t1(ID INT NULL, VALUE VARCHAR(2))
INSERT INTO #t1(ID, VALUE) VALUES (1,'aa')
INSERT INTO #t1(ID, VALUE) VALUES (2,'bb')
INSERT INTO #t1(ID, VALUE) VALUES (1,'aa')
INSERT INTO #t1(ID, VALUE) VALUES (1,'aa')
INSERT INTO #t1(ID, VALUE) VALUES (3,'cc')
INSERT INTO #t1(ID, VALUE) VALUES (3,'cc')
GO

-- BINARY_CHECKSUM(<column names>): <column names> are columns that we want to compare duplicates for
-- if you want to compare the full row then change BINARY_CHECKSUM(<column names>) -> BINARY_CHECKSUM(*)

-- for SQL Server 2000+ a loop
-- save checksums and rowcounts for duplicates
SELECT BINARY_CHECKSUM(ID, VALUE) AS ChkSum, COUNT(*) AS Cnt 
INTO #t2 
FROM #t1 
GROUP BY BINARY_CHECKSUM(ID, VALUE) HAVING COUNT(*)>1

DECLARE @ChkSum BIGINT, @rc INT
-- get the first checksum and set the rowcount to the count - 1 
-- because we want to leave one duplicate
SELECT TOP 1 @ChkSum = ChkSum, @rc = Cnt-1 FROM #t2

WHILE EXISTS (SELECT * FROM #t2)
BEGIN    
    -- rowcount is one less than the duplicate rows count
    SET ROWCOUNT @rc
    DELETE FROM #t1 WHERE BINARY_CHECKSUM(ID, VALUE) = @ChkSum 
    -- remove the processed duplicate from the checksum table
    DELETE #t2 WHERE ChkSum = @ChkSum 
    -- select the next duplicate rows to delete
    SELECT TOP 1 @ChkSum = ChkSum, @rc = Cnt-1 FROM #t2    
END 
SET ROWCOUNT 0
GO

SELECT * FROM #t1 

-- for SQL Server 2005+ a cool CTE
;WITH Numbered 
AS 
(
    SELECT ROW_NUMBER() OVER (PARTITION BY ChkSum ORDER BY ChkSum) AS RN, *
    FROM (
             SELECT BINARY_CHECKSUM(ID, VALUE) AS ChkSum
             FROM #t1
         ) t
) 
DELETE FROM Numbered WHERE RN > 1;
GO

SELECT * FROM #t1 

DROP TABLE #t1;
DROP TABLE #t2;