2

I am using SQL Server . i have creating SQL table have more than 20000 lines. i have Filter duplicate Using Following Query.

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

now I want to delete Duplicate from SQL Server How to add delete Following above Query ?

Shadow
  • 33,525
  • 10
  • 51
  • 64
thenna
  • 423
  • 1
  • 5
  • 26

3 Answers3

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;

The “ROW_NUMBER()” in the above query returns the sequential number of a row within a partition of a result set, starting at 1 for the first row in each partition. The “ORDER BY” clause determines the order in which the ROW_NUMBER value is assigned to the rows in a partition. The “PARTITION BY” clause used here to divide the result set into partitions;

The new column RowNum shows row numbers of the duplicate rows.

In case if you want to see the duplicate rows you can use select instead of delete

   ;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;
Mitz
  • 561
  • 8
  • 21
  • @P.Salmon if you are not clear with the answer pls look go through this article http://blog.sqlauthority.com/2009/06/23/sql-server-2005-2008-delete-duplicate-rows/ – Mitz Sep 27 '16 at 09:03
  • The link does not explain how it works.Can you explain or provide the appropriate link? what happens if there is more than one table in select statement? – xyz Sep 27 '16 at 09:21
  • 1
    sorry @thenna there was a comma added by mistake. Check now – Mitz Sep 27 '16 at 09:32
  • The link does not say how deleting from CTE deletes from actual table – xyz Sep 27 '16 at 09:32
  • This could explain, i think: https://learn.microsoft.com/en-us/sql/t-sql/statements/delete-transact-sql notes that with-subquery must be "updatable and reference exactly one base table in the FROM clause of the view definition. For more information about updatable views, see CREATE VIEW (Transact-SQL) (https://learn.microsoft.com/en-us/sql/t-sql/statements/create-view-transact-sql)." – Nashev Apr 06 '18 at 09:06
0

Here's a (simplified) example

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)

result

1> select * from t
2> ;
3> go
id
-----------
          2
P.Salmon
  • 17,104
  • 2
  • 12
  • 19
0

you could also create a new regular table with the same structure, insert distinct data in that table, then delete your original table and rename new table to the same name as original table had

Jason Clark
  • 1,307
  • 6
  • 26
  • 51