如何从表中选择重复的行?

时间:2010-04-09 05:14:31

标签: sql-server select duplicate-data

我有下表:

CREATE TABLE TEST(ID TINYINT NULL, COL1 CHAR(1))
INSERT INTO TEST(ID,COL1) VALUES (1,'A')
INSERT INTO TEST(ID,COL1) VALUES (2,'B')
INSERT INTO TEST(ID,COL1) VALUES (1,'A')
INSERT INTO TEST(ID,COL1) VALUES (1,'B')

INSERT INTO TEST(ID,COL1) VALUES (1,'B')
INSERT INTO TEST(ID,COL1) VALUES (2,'B')

我想从该表中选择重复的行。我该如何选择它们?

我尝试了以下内容:

SELECT TEST.ID,TEST.COL1
FROM TEST WHERE TEST.ID IN
(SELECT ID
FROM TEST WHERE TEST.COL1 IN
(SELECT COL1
FROM TEST WHERE TEST.ID IN
(SELECT ID
FROM TEST
GROUP BY ID
HAVING COUNT(*) > 1)
GROUP BY COL1
HAVING COUNT(*) > 1)
GROUP BY ID
HAVING COUNT(*) > 1)

哪里出错?我需要修改什么?

我希望它显示为:

ID   COL1
---- ----
1    A
1    A
1    B
1    B

(4 row(s) affected)

4 个答案:

答案 0 :(得分:4)

SELECT id, col1
FROM Test
GROUP BY id, col1
HAVING COUNT(*) > 1

使用时

SELECT id, col1, COUNT(*) AS cnt
FROM Test
GROUP BY id, col1
HAVING COUNT(*) > 1

你几乎拥有所有重复的行以及它们出现的频率。无论如何都无法单独识别它们。

一种较慢的方式是:

SELECT id, col1
FROM Test T
WHERE (SELECT COUNT(*)
       FROM Test I
       WHERE I.id = T.id AND I.col1 = T.col1) > 1

答案 1 :(得分:1)

使用Sql Server 2005+和CTE,您可以尝试

;WITH Dups AS (
    SELECT *,
            ROW_NUMBER() OVER(PARTITION BY ID, Col1 ORDER BY ID, Col1) Rnum
    FROM @TEST t
)
SELECT *
FROM Dups
WHERE Rnum > 1

或者只是标准

SELECT  ID, 
        Col1,
        COUNT(1) Cnt
FROM    @TEST
GROUP BY    ID, 
            Col1
HAVING  COUNT(1) > 1

修改

显示重复的行

SELECT  t.*
FROM    @Test t INNER JOIN
        (
            SELECT  ID,  
                    Col1, 
                    COUNT(1) Cnt 
            FROM    @TEST 
            GROUP BY    ID,  
                        Col1 
            HAVING  COUNT(1) > 1 
        ) dups  ON  t.ID = dups.ID 
                AND t.Col1 = dups.Col1

答案 2 :(得分:0)

该组数据中的每一行都是重复的

select id, col1, count(*)
from test
group by id, col1

显示了这个

如果要排除明确需要的2行,B行

例如

SELECT id, col1
FROM Test
WHERE NOT (id = 2 and col1 = 'B')

答案 3 :(得分:0)

SELECT t.* 
FROM TEST t
           INNER JOIN (
                       SELECT ID,COL1 
                       from test 
                       GROUP BY ID,COL1
                       HAVING COUNT(*) > 1
                      ) 
                       AS t2 
                       ON t2.ID = t.ID AND t2.COL1 =t.COL1
order by t.ID,t.COL1