使用SQL Server 2008 tsql时,我试图在一组值重复时删除表中的所有记录。所以如果我的表看起来像这样:
ID COL1 COL2
1 A 1
2 A 1
3 A 2
4 乙 1
5 乙 1
6 B 2
第1,2,4,5行将全部删除。
答案 0 :(得分:3)
;WITH d AS
(
SELECT col1, col2, c = COUNT(*) OVER
(PARTITION BY col1, col2 ORDER BY col1)
FROM dbo.yourtable
)
DELETE d WHERE c > 1;
实际上它可以稍微更整洁:
;WITH d AS
(
SELECT id, c = COUNT(*) OVER
(PARTITION BY col1, col2 ORDER BY col1)
FROM dbo.yourtable
)
DELETE d WHERE c > 1;
我会说,我在SQL Server 2012上测试了上述内容,但是我忘了改变SQL Server 2008的小提琴。对于SQL Server 2012之前的版本,这里有一个变体:
;WITH d AS
(
SELECT col1, col2
FROM dbo.yourtable AS t
GROUP BY col1, col2
HAVING COUNT(*) > 1
)
DELETE t --*
FROM dbo.yourtable AS t
WHERE EXISTS
(
SELECT 1 FROM d
WHERE col1 = t.col1 AND col2 = t.col2
);
DELETE d;
就可以了,但你得到:Msg 4403,Level 16,State 1,Line 2
无法更新视图或函数'd',因为它包含聚合,DISTINCT或GROUP BY子句,或PIVOT或UNPIVOT运算符。
答案 1 :(得分:0)
试试这个:
DELETE t
FROM dbo.yourTabe t
JOIN (
SELECT col1,col2,COUNT(1) cnt
FROM dbo.YourTable
GROUP BY col1, col2
HAVING COUNT(1)>1
) s
ON t.col1 = s.col1
AND t.col2 = s.col2