Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Delete rows from table.

Author  Topic 

hspatil31
Posting Yak Master

182 Posts

Posted - 2012-05-16 : 05:34:06
Dear All,

In my table I have following current data. I want to delete duplicate records from table.

I am trying following SQL statement, So I am getting
desired output which is orange color in current data. I want to keep the desired output records and other all should be deleted.
But complection is that in current data values in C5 and C6 are different.

select C1,C2,C3,C4,COUNT(*) from table_Delete
group by C1,C2,C3,C4
having COUNT(*) >= 1
ORDER BY COUNT(*) DESC

Can anyone please help me how to resolve this issue ?

Current Data:

C1 C2 C3 C4 C5 C6
201 203 224 325E 0000 0999

201 203 224 325E 0000 9999

201 203 224 325E 1000 1999
201 203 224 325E 3000 3999
201 203 224 325E 4000 4999
201 203 224 325E 5000 5999
201 203 224 325E 6000 6999
201 203 224 325E 7000 7999
201 203 224 325E 8000 8999
201 203 224 325E 9000 9999

201 203 224 389C 2000 2999 1

Desired Output:

C1 C2 C3 C4
201 203 224 325E 10
201 203 224 389C 1

Thanks and Regard's
Harish Patil

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-05-16 : 07:09:01
quote:
Originally posted by hspatil31

Dear All,

In my table I have following current data. I want to delete duplicate records from table.

I am trying following SQL statement, So I am getting
desired output which is orange color in current data. I want to keep the desired output records and other all should be deleted.
But complection is that in current data values in C5 and C6 are different.

select C1,C2,C3,C4,COUNT(*) from table_Delete
group by C1,C2,C3,C4
having COUNT(*) >= 1
ORDER BY COUNT(*) DESC

Can anyone please help me how to resolve this issue ?

Current Data:

C1 C2 C3 C4 C5 C6
201 203 224 325E 0000 0999

201 203 224 325E 0000 9999

201 203 224 325E 1000 1999
201 203 224 325E 3000 3999
201 203 224 325E 4000 4999
201 203 224 325E 5000 5999
201 203 224 325E 6000 6999
201 203 224 325E 7000 7999
201 203 224 325E 8000 8999
201 203 224 325E 9000 9999

201 203 224 389C 2000 2999 1

Desired Output:

C1 C2 C3 C4
201 203 224 325E 10
201 203 224 389C 1

Thanks and Regard's
Harish Patil

Your query should produce the output you described - Assuming your table is named table_Delete, simply remove the having count(*) > 1:
select C1,C2,C3,C4,COUNT(*) from table_Delete
group by C1,C2,C3,C4
If you are trying to delete duplicates, use the following code:
;WITH cte AS
(
SELECT *,
ROW_NUMBER() OVER (PARTITION BY c1,c2,c3,c4 ORDER BY c6) AS RN
FROM YourTable
)
DELETE FROM cte WHERE RN > 1;
Go to Top of Page

prett
Posting Yak Master

212 Posts

Posted - 2012-05-24 : 01:09:08
Use following query to Delete Duplicate Records:

DELETE
FROM MyTable
WHERE ID NOT IN
(
SELECT MAX(ID)
FROM MyTable
GROUP BY DuplicateColumn1, DuplicateColumn2, DuplicateColumn3)

Ref: http://blog.sqlauthority.com/2007/03/01/sql-server-delete-duplicate-records-rows/
Go to Top of Page

peterdixon410
Starting Member

5 Posts

Posted - 2014-08-29 : 07:53:43
I have used SQL server repair & recovery software for corruption , the software provide you three scanning mode facility to repair and Recovery Toolbox for SQL Server. Visit and read this : http://www.sql.recoverytoolbox.com/[url][/url]
Go to Top of Page
   

- Advertisement -