-5

在 T-SQL 查询中从表中删除重复项的最简单方法是什么?

列是 A 和 B。

Oneliners 是最受欢迎的。

4

4 回答 4

2

像这样的东西怎么样

DECLARE @TABLE TABLE(
        A VARCHAR(10),
        B VARCHAR(10)
)

INSERT INTO @TABLE VALUES
('1','1'),
('1','2'),
('2','2'),
('1','1'),
('1','2'),
('2','2')

SELECT  *
FROM    @TABLE

;WITH Vals AS (
        SELECT  *,
                ROW_NUMBER() OVER(PARTITION BY A,B ORDER BY A,B) ROWID
        FROM    @TABLE
)
DELETE
FROM    Vals
WHERE   ROWID > 1

SELECT  *
FROM    @TABLE

SQL 小提琴演示

于 2013-08-06T09:56:51.737 回答
2

如何从 MySQL 表中删除重复值

DELETE a FROM tbl a
LEFT JOIN
(
    SELECT MIN(id) AS id, name
    FROM tbl
    GROUP BY name
) b ON a.id = b.id AND a.name = b.name
WHERE b.id IS NULL
于 2013-08-06T09:57:28.693 回答
1

以下 sql 将完成这项工作

DECLARE @TEMP AS TABLE(a Varchar(100),b VarChar(100))

INSERT INTO @TEMP (a,b)
SELECT A,B FROM MY_Table
GROUP BY A, b

DELETE My_Table

INSERT INTO My_Table (a, b)
SELECT a, b
FROM @temp

SELECT * FROM My_Table

http://sqlfiddle.com/#!3/5fdb8/1

于 2013-08-06T10:12:01.960 回答
0

尝试这个

SELECT A, B, count(*)
FROM table
GROUP BY A, B
HAVING count(*) > 1
于 2013-08-06T09:54:50.057 回答