删除表中的重复元组

时间:2012-09-18 12:15:50

标签: sql sql-server sql-server-2008 duplicate-removal

我的数据库中存在一些问题

Select * from table where JobId='329188628'

结果:

ID     JobId      ONETID
-----------------------
32951  329188628    532
32951  329188628    532
32951  329188628    532
32951  329188628    532 

我需要删除重复的行。 任何人都可以帮帮我吗?

3 个答案:

答案 0 :(得分:3)

请尝试:

with c as(
select *, row_number() over(partition by id order by (select 0)) as n
from table where JobID='329188628'
)delete from c
where n > 1;

答案 1 :(得分:3)

试试这个。

with cte(cnt) as
(
  select row_number() over(partition by ID,JobId,ONETID order by getdate())
  from table where JobId='329188628'
)
delete from cte where cnt>1

并且仅使用JobId和OnetId进行分组。

with cte(cnt) as
(
  select row_number() over(partition by JobId,ONETID order by getdate())
  from table where JobId='329188628'
)
delete from cte where cnt>1

答案 2 :(得分:0)

使用DISTINCT关键字。

Select DISTINCT * from table where JobId='329188628'