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 |
AskSQLTeam
Ask SQLTeam Question
0 Posts |
Posted - 2004-12-17 : 06:41:22
|
Nick writes "I was trying to basically scan through a table and delete duplicate records. But I do want one of the duplicates to remain.I ran across your post here:http://www.4guysfromrolla.com/webtech/sqlguru/q051200-2.shtmlI used the SQL code, however it deletes ALL my duplicates and does not leave one row. I'm not sure if it's your SQL code, or if it's how I changed it to fit my table. Would you be so kind as to take a look at this please? I am very thankful.Table Design:Colums= irecordid, iuserid, ithreadid, vchtypeThe irecordid column is the primary key.Here is the transformed SQL statement:DELETEFROM ftdb_subscriptionWHERE ftdb_subscription.irecordid IN(SELECT F.irecordidFROM ftdb_subscription AS FWHERE Exists (SELECT iuserid, ithreadid, vchtype, Count(irecordid)FROM ftdb_subscriptionWHERE ftdb_subscription.iuserid = F.iuserid AND ftdb_subscription.ithreadid = F.ithreadid AND ftdb_subscription.vchtype = F.vchtypeGROUP BY ftdb_subscription.iuserid, ftdb_subscription.ithreadid, ftdb_subscription.vchtypeHAVING Count(ftdb_subscription.irecordid) > 1))AND ftdb_subscription.irecordid NOT IN(SELECT Min(irecordid)FROM ftdb_subscription AS FWHERE Exists (SELECT iuserid, ithreadid, vchtype, Count(irecordid)FROM ftdb_subscriptionWHERE ftdb_subscription.iuserid = F.iuserid AND ftdb_subscription.ithreadid = F.ithreadid AND ftdb_subscription.vchtype = F.vchtypeGROUP BY ftdb_subscription.iuserid, ftdb_subscription.ithreadid, ftdb_subscription.vchtypeHAVING Count(ftdb_subscription.irecordid) > 1)GROUP BY ftdb_subscription.iuserid, ftdb_subscription.ithreadid, ftdb_subscription.vchtype);" |
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2004-12-17 : 07:12:26
|
why don't you simply use a union on your table put the reesults of that in a temp table truncate the original table and put the corrected data from the temp table into an original table.Go with the flow & have fun! Else fight the flow |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2004-12-17 : 09:08:52
|
that is definitely NOT our article, not sure why you think we wrote that!see if this helps:http://weblogs.sqlteam.com/jeffs/archive/2004/11/02/2460.aspxAlso, search the articles HERE at sqlteam for more on deleting duplicates. Accept no substitutes from other sites!! - Jeff |
|
|
Bex
Aged Yak Warrior
580 Posts |
Posted - 2004-12-17 : 10:16:56
|
I had to do a similar thing a while back. It took me quite a bit of searching, but I came up with this solution. To be honest, I cannot really remember how I did it, but this query extracts duplicate rows into another table. I think I altered the statement to delete the rows, but ran this first as a back up. select distinct J36.CycleInstanceId, J36.AccountNo, J36.MobileNo into J36Dupes FROM J36 INNER JOIN (SELECT distinct CycleInstanceId, AccountNo, MobileNo FROM J36 GROUP BY CycleInstanceId, AccountNo, MobileNo HAVING Count(*) > 1 ) AS Duplicates ON J36.CycleInstanceId = Duplicates.CycleInstanceId AND J36.AccountNo = Duplicates.AccountNo AND J36.MobileNo = Duplicates.MobileNoHearty head pats |
|
|
X002548
Not Just a Number
15586 Posts |
|
X002548
Not Just a Number
15586 Posts |
|
jen
Master Smack Fu Yak Hacker
4110 Posts |
Posted - 2004-12-20 : 05:10:04
|
quote: For tons of great SQL Information, also be sure to check out: SQLTeam.com!
--------------------keeping it simple... |
|
|
|
|
|
|
|