Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 7th, 2006, 07:47 AM
Registered User
 
Join Date: Apr 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to subhasischakraborty
Default How can you delete duplicate rows without using te

How can you delete duplicate rows without using temporary tables ?

Like I have a table stucture

A B C
---------------
1 1 1
1 1 1
1 1 1
1 1 1
2 2 2
2 2 2
2 2 2
2 2 3

I need It like this

A B C
---------------
1 1 1
2 2 2
2 2 3

Sujjest any views you know ...... and thanx in advance


 
Old April 7th, 2006, 03:16 PM
Friend of Wrox
 
Join Date: Aug 2004
Posts: 385
Thanks: 0
Thanked 0 Times in 0 Posts
Default

use a cursor

 
Old April 7th, 2006, 03:50 PM
Registered User
 
Join Date: Apr 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

This works for me

Select distinct * into <newtable> from <oldtable>

works like magic??

 
Old April 10th, 2006, 06:05 AM
Registered User
 
Join Date: Apr 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to subhasischakraborty
Default

Look I am not using a temporary table and then to use a new table is all the same . Can u sujjest any other ways ..

Using a cursor is also a very good idea .... But are there any simple solutions like the ans from SPere

And thanx in advance

 
Old April 10th, 2006, 10:31 AM
Registered User
 
Join Date: Apr 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry about the previous answer:
This is the way it should be done( can be optimized)

declare @lp tinyint -- need for loop
declare @col1 tinyint,@col2 tinyint,@col3 tinyint --3 test columns
set @lp=1 -- like false
while @lp>0
begin
 if not exists (select top 1 * from abc group by col1,col2,col3 having count(col1)>1)
  set @lp=0 --like true, to end loop
 else
 begin --real brain work here
   select top 1 @col1 = col1,@col2 = col2,@col3 = col3 from abc group by col1,col2,col3 having count(col1)>1
  --delete since no primary key,or unique way to identify row
   delete from abc where col1=@col1 and col2=@col2 and col3=@col3
   insert into abc values(@col1,@col2,@col3)
 end
end

If you find another answer would like to hear from you.
Have fun.

 
Old April 20th, 2006, 02:49 AM
Registered User
 
Join Date: Apr 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to subhasischakraborty
Default

Hey this a troubling ans , if the number of columns are more than 40 then .....


:(

 
Old April 20th, 2006, 03:11 AM
Registered User
 
Join Date: Apr 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to subhasischakraborty
Default

Actualy what I meant was if the table structure had columns more than 40 . Are there any general ways to tackle

http://www.humshayarhai.blogspot.com

http://www.seowebtools.blogspot.com
 
Old October 17th, 2007, 02:45 PM
Registered User
 
Join Date: Oct 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

in SQL 2005 you can use the new row_number() function.
this will allow you to identify each row and delete duplicates.
:)

row_number


Joe
http://www.joesdirectory.co.uk

 
Old October 17th, 2007, 05:01 PM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
Default

Or, you could just design your table correctly to begin with... Primary keys work wonders ;)

--Jeff Moden
 
Old October 17th, 2007, 05:12 PM
Registered User
 
Join Date: Oct 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by Jeff Moden
 Or, you could just design your table correctly to begin with... Primary keys work wonders ;)

--Jeff Moden
LOL, true, but back to the real world it can still happen...
i had it happen tonight when loading in data from an external CSV data source.
Although the data had unique rows in the CSV file, by the time i had ran my clean up script to clean up the data i was left with some duplicate rows - not all fields were duplicate but enough fields to cause me problems.


Joe
www.joesdirectory.co.uk
















Similar Threads
Thread Thread Starter Forum Replies Last Post
How to delete unique rows but with duplicate colum winder SQL Server 2005 3 January 3rd, 2007 04:57 PM
How to delete one of two duplicate records aarkaycee SQL Server 2000 0 August 20th, 2006 07:27 PM
duplicate rows msrnivas Classic ASP Components 1 August 26th, 2004 02:07 AM
Detecting duplicate rows. Daniel Walker PHP Databases 6 June 21st, 2004 02:39 AM
Checking Duplicate values and delete rows ppenn Excel VBA 2 February 3rd, 2004 06:57 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.