dbTalk Databases Forums  

Re: How to delete doubled records in one query?

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss Re: How to delete doubled records in one query? in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Dan Guzman
 
Posts: n/a

Default Re: How to delete doubled records in one query? - 02-13-2008 , 07:56 AM






Quote:
Is it possible to remowe those doubled records (and keep safe the good
one ) in one SQL query?
A unique row identifier is required to perform a set-based delete of the
duplicates in a single query. Without one, you will need either a cursor or
some other sort of iteration technique (e.g. WHILE).

--
Hope this helps.

Dan Guzman
SQL Server MVP
http://weblogs.sqlteam.com/dang/

"Macio" <othellot (AT) example (DOT) com> wrote

Quote:
Hello,
I have records doubled in my table. Like:

order amount date
10001 145,90 2008-02-02
10001 145,90 2008-02-02
10002 305,95 2008-02-03
10002 305,95 2008-02-03
...

Is it possible to remowe those doubled records (and keep safe the good
one ) in one SQL query? I would like not to use any additional table nor
cursor. I have heard about delet caluse based on select query, but I
cannot write someting like that.
BTW, the table does not have any unique key (e.g. the field 'order' is not
unique). The table is quite big (millions of records).
Please help.
Maciek


Reply With Quote
  #2  
Old   
Plamen Ratchev
 
Posts: n/a

Default Re: How to delete doubled records in one query? - 02-13-2008 , 08:27 AM






Here is one method for SQL Server 2005. Adding an UNIQUE constraint after
the delete will make life easier.

;WITH DupOrders (rn)
AS
(SELECT ROW_NUMBER() OVER(
PARTITION BY order_nbr
ORDER BY date)
FROM Orders)
DELETE FROM DupOrders
WHERE rn > 1;

HTH,

Plamen Ratchev
http://www.SQLStudio.com


Reply With Quote
  #3  
Old   
Tom van Stiphout
 
Posts: n/a

Default Re: How to delete doubled records in one query? - 02-13-2008 , 08:28 AM



On Wed, 13 Feb 2008 08:30:16 +0100, Macio <othellot (AT) example (DOT) com>
wrote:

That sounds like a terrible database design.
You could use:
select distinct order, amount, [date]
to get the unique records, and insert them into a new table, then
delete the existing table and rename the new one to the current name.

-Tom.


Quote:
Hello,
I have records doubled in my table. Like:

order amount date
10001 145,90 2008-02-02
10001 145,90 2008-02-02
10002 305,95 2008-02-03
10002 305,95 2008-02-03
...

Is it possible to remowe those doubled records (and keep safe the good
one ) in one SQL query? I would like not to use any additional table
nor cursor. I have heard about delet caluse based on select query, but I
cannot write someting like that.
BTW, the table does not have any unique key (e.g. the field 'order' is
not unique). The table is quite big (millions of records).
Please help.
Maciek

Reply With Quote
  #4  
Old   
jhofmeyr@googlemail.com
 
Posts: n/a

Default Re: How to delete doubled records in one query? - 02-14-2008 , 09:55 AM



The WHILE loop suggested by Dan would work like this:

WHILE EXISTS (
SELECT 1
FROM table
GROUP BY order, amount, date
HAVING COUNT(*) > 1
)
BEGIN
DELETE TOP(1) t
FROM table
WHERE EXISTS (
SELECT 1
FROM table
WHERE order = t.order
AND amount = t.amount
AND date = t.date
GROUP BY order, amount, date
HAVING COUNT(*) > 1
)
END

This also only works on SQL Server 2005. On SQL Server 2000 I'd
recommend using a temp table and SELECT DISTINCT as Tom suggested.

Good luck!
J

Reply With Quote
  #5  
Old   
jhofmeyr@googlemail.com
 
Posts: n/a

Default Re: How to delete doubled records in one query? - 02-14-2008 , 09:57 AM



I missed the alias assignment

On Feb 14, 3:55*pm, jhofm... (AT) googlemail (DOT) com wrote:
Quote:
* DELETE TOP(1) t
* FROM table t -- oops
* WHERE EXISTS (
J


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.