Wrox Programmer Forums
|
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 August 26th, 2004, 04:32 AM
Friend of Wrox
 
Join Date: Sep 2003
Posts: 101
Thanks: 0
Thanked 0 Times in 0 Posts
Default duplicating rows

Hi

I am worling on sql server 2000.
I have 2 ques
1. I want to delete the duplicate rows from my table
key is suppid and empid means want to delete duplicate records of same suppid and empid.

2.I have one table with 10 fields.I dont have any primary key in this table.when i perform the delete operation (just delete from table no more where conditions) its taking long time to delete.
is there any way to speed up it can i create index on this table if can to which field i have to create because i dont have any where condition in my sql query.

Pl help me thanks


 
Old August 26th, 2004, 06:06 AM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 625
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to jemacc
Default

Post your sample table structure or select statement for others to offer help. Try this

DELETE FROM table1
WHERE
 EXISTS
 (SELECT yourId
 FROM
 table1 table2
 WHERE
  table2.columnname1 = table1.columename1
  AND table2.columnname2 = table1.columnname2
  AND table2.yourId < table1.yourId)



 
Old September 3rd, 2004, 06:02 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Quote:
quote:just delete from table no more where conditions...
because i dont have any where condition in my sql query.
Does this mean that you are trying to cleanup the table? If so why dont you try TRUNCATE command?

_________________________
- Vijay G
Strive for Perfection
 
Old September 30th, 2004, 07:41 AM
Authorized User
 
Join Date: Sep 2004
Posts: 55
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to Lalit_Pratihari
Default

Hi,

Alternatively you can also try this out in SQL server:

--To extract the duplicate rows
SELECT COL1, COL2, COUNT(*)
FROM DUP_ROWS
GROUP BY COL1, COL2
HAVING COUNT(*) > 1

--First, run the above GROUP BY query to determine how many sets of duplicate PK values exist, and the count of duplicates for each set.
--Select the duplicate key values into a holding table. For example:
SELECT COL1, COL2, COL3 = COUNT(*)
INTO HOLDKEY
FROM DUP_ROWS
GROUP BY COL1, COL2
HAVING COUNT(*) > 1


--Select the duplicate rows into a holding table, eliminating duplicates in the process. For example:
SELECT DISTINCT DUP_ROWS.*
INTO HOLDUPS
FROM DUP_ROWS, HOLDKEY
WHERE DUP_ROWS.COL1 = HOLDKEY.COL1
AND DUP_ROWS.COL2 = HOLDKEY.COL2

--At this point, the holddups table should have unique PKs, however, this will not be the case
--if t1 had duplicate PKs, yet unique rows.
--Verify that each key in holddups is unique, and that you do not have duplicate keys,
--yet unique rows. If so, you must stop here and reconcile which of the rows you wish to
--keep for a given duplicate key value. For example, the query:
SELECT COL1, COL2, COUNT(*)
FROM HOLDUPS
GROUP BY COL1, COL2

--should return a count of 1 for each row.
--If no, you have duplicate keys, yet unique rows, and need to decide which rows to save.
--This will usually entail either discarding a row, or creating a new unique key value for this row.
--Take one of these two steps for each such duplicate PK in the holddups table.
--Delete the duplicate rows from the original table. For example:
DELETE DUP_ROWS
FROM DUP_ROWS, HOLDKEY
WHERE DUP_ROWS.COL1 = HOLDKEY.COL1
AND DUP_ROWS.COL2 = HOLDKEY.COL2

--Put the unique rows back in the original table. For example:
INSERT DUP_ROWS SELECT * FROM HOLDUPS

Hope this helps,

Lalit
Life Means More...;)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Duplicating Labels in the Detail band MountainProud Access 4 June 11th, 2008 02:10 PM
Duplicating files in Prolog Ruman29 Other Programming Languages 0 March 8th, 2007 05:27 PM
Duplicating datagrid rows using dataview batlou Visual Studio 2005 0 October 25th, 2006 11:41 AM
Duplicating IDs in database pettrer ASP.NET 2.0 Basics 7 July 8th, 2006 04:03 PM
Duplicating Columns on same table yuvalk SQL Language 2 May 11th, 2004 06:22 AM





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