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 ?

4
  • Which is your Primary Key in your table? Commented Sep 27, 2016 at 8:34
  • All the duplicate entries or do you want to retain one of them? Commented Sep 27, 2016 at 8:34
  • All the Duplicates Entires P.Salmon Commented Sep 27, 2016 at 8:36
  • See this link stackoverflow.com/questions/18932/… Commented Sep 27, 2016 at 8:56

3 Answers 3

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;
Sign up to request clarification or add additional context in comments.

5 Comments

@P.Salmon if you are not clear with the answer pls look go through this article blog.sqlauthority.com/2009/06/23/…
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?
sorry @thenna there was a comma added by mistake. Check now
The link does not say how deleting from CTE deletes from actual table
This could explain, i think: learn.microsoft.com/en-us/sql/t-sql/statements/… 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) (learn.microsoft.com/en-us/sql/t-sql/statements/…)."
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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.