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 November 21st, 2003, 04:16 PM
Authorized User
 
Join Date: Jun 2003
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to nlicata
Default eliminating all but 1 of duplicates

Thanks in advance for any assistance.

I have a table that is added to daily and due to some bad records coming from vendor, I end up with duplicate records that are not perfectly alike.

SAMPLE DATA:
customer_number field2 field3 field4
12345 abc 123 abc
12345 abc NULL NULL
6789 def 456 hij
6789 def NULL NULL
11111 wxyz NULL NULL

I need to eliminate records with duplicate customer_number and null in any field. But I want to leave non-duplicate records with null in any field. The database holds many years of monthly data so there will be duplicate customer numbers, but I only want to keep duplicates with no nulls. I can't delete records with nulls (if not duplicate) and I can't jus delete dups w/o nulls.

thanks again,
 
Old November 21st, 2003, 04:28 PM
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

do you have a pK on this table?

 
Old November 21st, 2003, 04:45 PM
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

If you have a primary key try this example below. If you don't add one. Also see the second example.

SELECT DISTINCT *
INTO duplicate_table
FROM original_table
GROUP BY key_value
HAVING COUNT(key_value) > 1

DELETE original_table
WHERE key_value
IN (SELECT key_value
FROM duplicate_table)

INSERT original_table
SELECT *
FROM duplicate_table

DROP TABLE duplicate_table

   SELECT DISTINCT *
      INTO duplicate_table
      FROM original_table
      GROUP BY key_value
      HAVING COUNT(key_value) > 1

   DELETE original_table
      WHERE key_value
      IN (SELECT key_value
             FROM duplicate_table)

   INSERT original_table
      SELECT *
         FROM duplicate_table

     DROP TABLE duplicate_table


When executed, this script moves one instance of any duplicate row in the original table to a duplicate table. It then deletes all rows from the original table that also reside in the duplicate table. Next, the rows in the duplicate table are moved back into the original table. Finally, the duplicate table is dropped.


Example 2

SELECT *
FROM Customer
WHERE Customer_number = (SELECT TOP 1 Customer_number
FROM Customer t1
WHERE t1.field2 = Customer.col2
ORDER BY Customer_number DESC )-- Or ASC

Your data
SAMPLE DATA:
customer_number field2 field3 field4
12345 abc 123 abc
12345 abc NULL NULL
6789 def 456 hij
6789 def NULL NULL
11111 wxyz NULL NULL



 
Old November 21st, 2003, 04:52 PM
Authorized User
 
Join Date: Jun 2003
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to nlicata
Default

excellent thank you,





Similar Threads
Thread Thread Starter Forum Replies Last Post
Eliminating Newlines muki XSLT 0 October 24th, 2005 09:49 PM
Eliminating spaces in a query - ??? snoopy92211 Access VBA 1 July 26th, 2004 11:47 PM
Eliminating Redundant Records spraveens MySQL 1 May 12th, 2004 04:21 AM
eliminating duplicate data erin SQL Language 2 April 22nd, 2004 11:49 AM
Eliminating HTML TAGs beyondforsaken VB How-To 12 June 12th, 2003 08:00 AM





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