Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
DB Engine
SQL ServerMSDESQL Server CE
Services
Analysis (Data Mining)Analysis (OLAP)DTSIntegration ServicesNotification ServicesReporting Services
Programming
CLRConnectivitySQLXML
Other Technologies
ClusteringEnglish QueryFull-Text SearchReplicationService Broker
General
Data WarehousingPerformanceSecuritySetupSQL Server ToolsOther SQL Server Topics
DirectoryUser Groups
Related Topics
MS AccessOther DB ProductsMS Server Products.NET DevelopmentVB DevelopmentJava DevelopmentMore Topics ...

SQL Server Forum / DB Engine / SQL Server / March 2008

Tip: Looking for answers? Try searching our database.

Dupes

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
morphius - 25 Mar 2008 01:44 GMT
ID  CID     Date
1     1      3/10/2008
2     2      3/10/2008
3     3      6/20/2007
4     3      6/20/2007
5     4      3/10/2008
6     3      3/30/2007
7     5      6/20/2007
8     3      6/20/2007

IDs 3,4, 8 are duplicates. What would the delete statement look like to
delete IDs 4 & 8 and leave ID 3.
Tom Moreau - 25 Mar 2008 01:53 GMT
Try:

delete MyTable
where exists
(
   select
       *
   from
       MyTable m
   where
       m.CID = MyTable.CID
   and
       m.[Date] = MyTable.CID
   and
       m.ID > MyTable.CID
)

Signature

  Tom

----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
SQL Server MVP
Toronto, ON   Canada
https://mvp.support.microsoft.com/profile/Tom.Moreau

ID  CID     Date
1     1      3/10/2008
2     2      3/10/2008
3     3      6/20/2007
4     3      6/20/2007
5     4      3/10/2008
6     3      3/30/2007
7     5      6/20/2007
8     3      6/20/2007

IDs 3,4, 8 are duplicates. What would the delete statement look like to
delete IDs 4 & 8 and leave ID 3.
morphius - 25 Mar 2008 03:59 GMT
Tom,
I modified to:
and
>         m.[Date] = MyTable.[Date]
>     and
>         m.ID > MyTable.ID

This works. Thanks..

> Try:
>
[quoted text clipped - 25 lines]
> IDs 3,4, 8 are duplicates. What would the delete statement look like to
> delete IDs 4 & 8 and leave ID 3.
Tom Moreau - 25 Mar 2008 12:07 GMT
Oops!  God catch.

Signature

  Tom

----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
SQL Server MVP
Toronto, ON   Canada
https://mvp.support.microsoft.com/profile/Tom.Moreau

Tom,
I modified to:
and
>         m.[Date] = MyTable.[Date]
>     and
>         m.ID > MyTable.ID

This works. Thanks..

"Tom Moreau" wrote:

> Try:
>
[quoted text clipped - 25 lines]
> IDs 3,4, 8 are duplicates. What would the delete statement look like to
> delete IDs 4 & 8 and leave ID 3.
Plamen Ratchev - 25 Mar 2008 05:55 GMT
On SQL Server 2005 you can do:

;WITH Dups (seq)
AS
(SELECT ROW_NUMBER() OVER(
                  PARTITION BY cid, date
                  ORDER BY id)
FROM Foo)
DELETE Dups
WHERE seq > 1;

HTH,

Plamen Ratchev
http://www.SQLStudio.com
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.