在 T-SQL 查询中从表中删除重复项的最简单方法是什么?
列是 A 和 B。
Oneliners 是最受欢迎的。
像这样的东西怎么样
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
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
以下 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
尝试这个
SELECT A, B, count(*)
FROM table
GROUP BY A, B
HAVING count(*) > 1