删除重复行SQL服务器

时间:2016-09-27 08:32:54

标签: sql sql-server

我正在使用SQL Server。我创建的SQL表有20000多行。我使用以下查询过滤重复。

SELECT
    Entity,ExpenseType,Amount,Description,APSupplierID,ExpenseReportID,Employee,ExpenseDate,COUNT(*)
FROM
    TotalsByGLCenter
GROUP BY
    Entity,ExpenseType,Amount,Description,APSupplierID,ExpenseReportID,Employee,ExpenseDate
HAVING 
    COUNT(*) > 1

现在我要从SQL Server中删除Duplicate如何添加删除以上查询?

3 个答案:

答案 0 :(得分:6)

;WITH cte
         AS (Select ROW_NUMBER() OVER  (PARTITION BY Entity,ExpenseType,Amount,Description,APSupplierID,ExpenseReportID,Employee,ExpenseDate ORDER BY ( SELECT 0)) RowNum
             FROM   TotalsByGLCenter)
    Delete FROM cte
    WHERE  RowNum > 1;

上述查询中的“ROW_NUMBER()”返回结果集分区中行的序号,从1开始,每个分区的第一行。 “ORDER BY”子句确定将ROW_NUMBER值分配给分区中的行的顺序。此处使用“PARTITION BY”子句将结果集划分为分区;

新列RowNum显示重复行的行号。

如果您想查看重复的行,可以使用选择而不是删除

   ;WITH cte
         AS (Select ROW_NUMBER() OVER  (PARTITION BY Entity,ExpenseType,Amount,Description,APSupplierID,ExpenseReportID,Employee,ExpenseDate ORDER BY ( SELECT 0)) RowNum,Entity,ExpenseType,Amount,Description,APSupplierID,ExpenseReportID,Employee,ExpenseDate 
             FROM   TotalsByGLCenter)
    Select * FROM cte
    WHERE  RowNum > 1;

答案 1 :(得分:0)

这是一个(简化的)例子

drop table t
create  table t (id int)

insert into t values (1),(1),(1),(2),(3),(3)

;WITH CTE AS(
  select id,count(*) as dupes
  from t
  group by t.id
  having count(*) > 1

)
DELETE  t where t.id  in (select  cte.id from cte)

结果

1> select * from t
2> ;
3> go
id
-----------
          2

答案 2 :(得分:0)

您还可以创建一个具有相同结构的新常规表,在该表中插入不同的数据,然后删除原始表并将新表重命名为与原始表相同的名称