2011-07-08 16 views
1

SQLテーブルSQLテーブルから重複レコードを選択する方法は?

重複レコードを選択するには、カーソルを使用する必要がありますか? RxNbrとコラム:後

が列ならば...

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

私のクエリで[日付を記入]が同じ値を持っており、複数回繰り返され、私はそれらのrxnbr /を選択する必要があり、[日付を記入します]記録。

答えて

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 
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 
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; 
関連する問題