Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Delete Duplicates


Delete Duplicates

Author
Message
Q -631159
Q -631159
SSC Veteran
SSC Veteran (241 reputation)SSC Veteran (241 reputation)SSC Veteran (241 reputation)SSC Veteran (241 reputation)SSC Veteran (241 reputation)SSC Veteran (241 reputation)SSC Veteran (241 reputation)SSC Veteran (241 reputation)

Group: General Forum Members
Points: 241 Visits: 165
I agree with the others, great question!

Q

Please take a number. Now serving emergency 1,203,894
redrazorback
redrazorback
SSC-Enthusiastic
SSC-Enthusiastic (124 reputation)SSC-Enthusiastic (124 reputation)SSC-Enthusiastic (124 reputation)SSC-Enthusiastic (124 reputation)SSC-Enthusiastic (124 reputation)SSC-Enthusiastic (124 reputation)SSC-Enthusiastic (124 reputation)SSC-Enthusiastic (124 reputation)

Group: General Forum Members
Points: 124 Visits: 132
This anwer works for the given data but wouldn't work if we inserted a row like:

insert into #new(id,keyvalue) values (1,'bb')

The stated answer assumes a duplicate is defined by the ID column and would delete the above row even thought it's unique in the table. Since no PK is defined on the table I think we must assume a duplicate is defined by multiple rows with identical values for every column. So if we include both ID and keyvalue columns in the over() clause we will delete only non-unique rows:

with numbered as
(
SELECT rowno=row_number() over (partition by ID, keyvalue order by ID, keyvalue),ID,keyvalue
from #new
)

delete from numbered where rowno>1
Yelena Varshal
Yelena Varshal
Hall of Fame
Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)

Group: General Forum Members
Points: 3480 Visits: 593
J is correct.
As for SQL, the old one will work too for this particular case:

declare @I int
select @I = (select count(convert(varchar(2),ID)+Keyvalue) from #new
group by (convert(varchar(2),ID)+Keyvalue)
having count(convert(varchar(2),ID)+Keyvalue) >1)
set @I = @I-1
set rowcount @I
delete #New where convert(varchar(2),ID)+Keyvalue
in
(select convert(varchar(2),ID)+Keyvalue from #new
group by (convert(varchar(2),ID)+Keyvalue)
having count(convert(varchar(2),ID)+Keyvalue) >1)


Regards,
Yelena Varshal

Jamie-2229
Jamie-2229
SSCrazy
SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)

Group: General Forum Members
Points: 2955 Visits: 831
So if we include both ID and keyvalue columns in the over() clause we will delete only non-unique rows:


The angle you derive with the "Partition by" clause is literally a "partition" of a particular cluster of fields defined uniquely. If you prefer, then also, use only the "order by" portion of the clause and create a numeric cluster("PK" ID ) that is dynamic [rather than static].


Select rowno=Row_Number() over(Order by mychoiceofID,2ndchoice,etcchoice), * from mytable.

Jamie
Olga B
Olga B
Mr or Mrs. 500
Mr or Mrs. 500 (596 reputation)Mr or Mrs. 500 (596 reputation)Mr or Mrs. 500 (596 reputation)Mr or Mrs. 500 (596 reputation)Mr or Mrs. 500 (596 reputation)Mr or Mrs. 500 (596 reputation)Mr or Mrs. 500 (596 reputation)Mr or Mrs. 500 (596 reputation)

Group: General Forum Members
Points: 596 Visits: 455
Excellent question. I learned something useful.

One application of OVER that seems to have potential (note hedging Wink ) is that it allows applying aggregate function to more than one different grouping in the same query. For instance,


select customerID, productID, orderDate, orderAmount,
CustomerTotal = sum(orderAmount) over (partition by customerID),
ProductTotal = sum(orderAmount) over (partition by productID)
from Orders




Any thoughts on performance?

Edited for clarity.
Jamie-2229
Jamie-2229
SSCrazy
SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)

Group: General Forum Members
Points: 2955 Visits: 831
Any thoughts on performance?


If I recall correctly I came across this syntax with help from Erland Sommarskog. I was looking for a way to remove duplicates from a table with over 100 million rows. As it turned out, this was the only method I could use that would perform well enough given the amount of cpu and ram, that would even do the job.

Jamie
Anipaul
Anipaul
SSCertifiable
SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)

Group: General Forum Members
Points: 6275 Visits: 1407
Excellent questions..............



Jamie-2229
Jamie-2229
SSCrazy
SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)

Group: General Forum Members
Points: 2955 Visits: 831
Of note, I learned something today as well regarding the semi-colons as my background is vb where semi-colons make our life more difficult. I am focussing on getting those semi-colons into my queries. Thanks for all the nice comments... appreciated!

Jamie
Cliff Jones
Cliff Jones
SSCarpal Tunnel
SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)

Group: General Forum Members
Points: 4069 Visits: 3648
Yes, this is useful. In fact I ran into a case yesterday where I was able to employ this method to remove duplicates from a table. And as performance goes it was much more efficient then the method I would commonly have used which was copy all the data out into a temp table, truncate and then select distinct back into the table.
DB_Andrew
DB_Andrew
Valued Member
Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)

Group: General Forum Members
Points: 59 Visits: 271
If you have code like this or need to use code like this, you have other greater issues going on and need to put the pipe down.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search