SQL如何删除重复记录

时间:2019-12-06 09:16:11

标签: sql sql-server duplicates sql-delete

如何通过删除重复的记录来清理表?

+----------+--------+------------+
| clientID | status | Insertdate |
+----------+--------+------------+
|        1 | new    |   20191206 |
|        1 | new    |   20191206 |
|        2 | old    |   20191206 |
|        2 | old    |   20191206 |
|        3 | new    |   20191205 |
|        3 | new    |   20191205 |
+----------+--------+------------+

我没有任何身份字段。

2 个答案:

答案 0 :(得分:1)

请找到以下查询。您可以使用行号。

;WITH cte as (
  select clientid
    , status, Insertdate
    , ROW_NUMBER() over (partition by clientid, status, Insertdate order by clientid) RowNumber 
  from Yourtable
) 
delete from cte where RowNumber > 1

答案 1 :(得分:-1)

如果您正在运行MySQL数据库,希望这会有所帮助

SELECT clientID, status, Insertdate, count(*)
FROM table_name
GROUP BY clientID, status, Insertdate
having count(*) > 1