0

I have a dataset containing duplicate rows

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

Could anyone supply me with a query to remove the duplicate lines

Thanks in advance

Jay Shankar Gupta
  • 5,918
  • 1
  • 10
  • 27
  • I think there are a lot of similiar questions here on SO, eg: https://stackoverflow.com/questions/18390574/how-to-delete-duplicate-rows-in-sql-server – mnille Mar 28 '18 at 10:57

4 Answers4

0

Use row_number() function to check duplication and remove

delete d from (
   select *,
          row_number() over (partition by Account_Code, Name order by DivID) Seq 
   from table
) d
where Seq  > 1
Yogesh Sharma
  • 49,870
  • 5
  • 26
  • 52
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

Demo

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

enter image description here

Jay Shankar Gupta
  • 5,918
  • 1
  • 10
  • 27
0

TRy this:

;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;
DineshDB
  • 5,998
  • 7
  • 33
  • 49
0

Try Using DistictFunction In Your Select Query .

     SELECT DISTINCT statement
Prakash
  • 100
  • 10