从数据集中删除重复的行

时间:2018-03-28 10:50:25

标签: sql sql-server

我有一个包含重复行的数据集

DivID  Account_Code  Name
1         CDF400      Caledon
1         CDF400      Caledon
1         CDF400      Caledon
1         BRF205      Brightside
1         BRF205      Brightside
1         FRO400      Fireburn
1         FRO400      Fireburn

任何人都可以向我提供删除重复行的查询

提前致谢

4 个答案:

答案 0 :(得分:0)

使用row_number()功能检查重复并删除

delete d from (
   select *,
          row_number() over (partition by Account_Code, Name order by DivID) Seq 
   from table
) d
where Seq  > 1

答案 1 :(得分:0)

WITH CTE AS(

   SELECT DivID,
          Account_Code,
          Name,
        ROW_NUMBER() OVER (PARTITION BY DivID,Account_Code,Name ORDER BY (SELECT 0)) AS RN
   FROM Table1
)
DELETE FROM CTE WHERE RN > 1

演示

  

http://dbfiddle.uk/?rdbms=sqlserver_2017&fiddle=c17dad6539da425bc1371614f0cbb1b2

enter image description here

答案 2 :(得分:0)

试试这个:

;WITH cte
     AS (SELECT *, ROW_NUMBER() OVER (PARTITION BY DivID,Account_Code,Name 
                                       ORDER BY ( SELECT 0)) RN
         FROM   YourTable)
DELETE FROM cte
WHERE  RN > 1;

答案 3 :(得分:0)

尝试在您的选择查询中使用Distict功能。

     SELECT DISTINCT statement