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.
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 gettingdesired 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_Deletegroup by C1,C2,C3,C4having COUNT(*) >= 1ORDER BY COUNT(*) DESCCan anyone please help me how to resolve this issue ?Current Data:C1 C2 C3 C4 C5 C6 201 203 224 325E 0000 0999201 203 224 325E 0000 9999201 203 224 325E 1000 1999201 203 224 325E 3000 3999201 203 224 325E 4000 4999201 203 224 325E 5000 5999201 203 224 325E 6000 6999201 203 224 325E 7000 7999201 203 224 325E 8000 8999201 203 224 325E 9000 9999201 203 224 389C 2000 2999 1Desired Output:C1 C2 C3 C4 201 203 224 325E 10201 203 224 389C 1Thanks and Regard'sHarish 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 gettingdesired 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_Deletegroup by C1,C2,C3,C4having COUNT(*) >= 1ORDER BY COUNT(*) DESCCan anyone please help me how to resolve this issue ?Current Data:C1 C2 C3 C4 C5 C6 201 203 224 325E 0000 0999201 203 224 325E 0000 9999201 203 224 325E 1000 1999201 203 224 325E 3000 3999201 203 224 325E 4000 4999201 203 224 325E 5000 5999201 203 224 325E 6000 6999201 203 224 325E 7000 7999201 203 224 325E 8000 8999201 203 224 325E 9000 9999201 203 224 389C 2000 2999 1Desired Output:C1 C2 C3 C4 201 203 224 325E 10201 203 224 389C 1Thanks and Regard'sHarish 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_Deletegroup 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; |
 |
|
prett
Posting Yak Master
212 Posts |
Posted - 2012-05-24 : 01:09:08
|
Use following query to Delete Duplicate Records:DELETEFROM MyTableWHERE ID NOT IN(SELECT MAX(ID)FROM MyTableGROUP BY DuplicateColumn1, DuplicateColumn2, DuplicateColumn3)Ref: http://blog.sqlauthority.com/2007/03/01/sql-server-delete-duplicate-records-rows/ |
 |
|
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] |
 |
|
|
|
|
|
|