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 |
|
micnie_2020
Posting Yak Master
232 Posts |
Posted - 2010-04-06 : 21:31:35
|
| Dear All,May i know what is the sql statement that able to delete off duplication record & only keep 1 for each unique code?Example:DistCode DistID SourcesAU0004314 575790 AUSAU0004314 575798 AUSAU0004315 575806 AUSAU0004315 575813 AUSAU0004315 575817 AUSAU0004316 575742 AUSAU0004316 575750 AUSAU0004317 575965 AUSAU0004317 575972 AUSAU0004318 575726 AUSAU0004318 575734 AUSI want to keep only one of the record AU0004314, AU0004315,AU0004316,AU0004317 ...etcPlease Advise.Thank you.Regards,Micheale |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
micnie_2020
Posting Yak Master
232 Posts |
Posted - 2010-04-06 : 22:45:32
|
| Thank You tkizer.It's work fine.Regards,Micheale |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
DBA in the making
Aged Yak Warrior
638 Posts |
Posted - 2010-04-07 : 17:03:10
|
| [code]DELETE FROM tableNameWHERE DistID NOT IN ( SELECT DistID FROM ( SELECT DistCode, MIN(DistID) AS DistID FROM tableName GROUP BY DistCode) z)[/code]There are 10 types of people in the world, those that understand binary, and those that don't. |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-04-07 : 17:21:58
|
quote: Originally posted by DBA in the making
DELETE FROM tableNameWHERE DistID NOT IN ( SELECT DistID FROM ( SELECT DistCode MIN(DistID) AS DistID FROM tableName GROUP BY DistCode)) There are 10 types of people in the world, those that understand binary, and those that don't.
The problem is already solved.And your provided solution will not work.Edit: Because the derived table has no name and a comma is missing between DistCode and MIN(DistID) No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
DBA in the making
Aged Yak Warrior
638 Posts |
Posted - 2010-04-07 : 17:33:42
|
quote: Originally posted by webfredThe problem is already solved.
So what. quote: And your provided solution will not work.Edit: Because the derived table has no name and a comma is missing between DistCode and MIN(DistID)
Fixed.There are 10 types of people in the world, those that understand binary, and those that don't. |
 |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2010-04-07 : 22:31:09
|
| Be careful with this when you are using NOT IN.http://weblogs.sqlteam.com/mladenp/archive/2007/05/18/60210.aspx |
 |
|
|
|
|
|
|
|