Wrox Programmer Forums
|
BOOK: Professional SQL Server 2000 Programming
This is the forum to discuss the Wrox book Professional SQL Server 2000 Programming by Robert Vieira; ISBN: 9780764543791
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional SQL Server 2000 Programming 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 29th, 2008, 08:34 AM
Authorized User
 
Join Date: Jan 2008
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default wrote remove duplicate query

I have been taking from this board for awhile now and thought I would try to give back. I am sure this is elementary to many of you that are well versed in SQL but I created a query yesterday that solved a big problem for me and I wanted to share it.

Now the issue was that I had a table with the columns: phone number, installed, disconnected. The phone numbers were listed multiple times mostly with null values but sometimes with legit data.

IE:
telnumber | Installed | Disconnected
555-1212 12-12-2005 05-06-2007
555-1212 NULL NULL
666-1212 NULL NULL
666-1212 NULL NULL

My goal was to eliminate duplicates from the table, keeping the phone number with legit data over NULL, but if the phone number was listed twice both times with NULL fields, then just keep one of them...

EXAMPLE CODE
Code:
SELECT DISTINCT * INTO temp_table
FROM original_table
WHERE column1 <> '' OR column2 <> ''
ORDER BY column1 ASC

SELECT * INTO FINAL_table
FROM temp_table 
UNION
SELECT DISTINCT * FROM original_table
WHERE (column1 NOT IN
        (SELECT column1 FROM temp_table)
       )

SELECT * FROM FINAL_table
ACTUAL CODE
Code:
SELECT DISTINCT * INTO phone_temp
FROM phone_original
WHERE installed <> '' OR disconnected <> ''
ORDER BY telnumber ASC

SELECT * INTO phone_FINAL
FROM phone_temp 
UNION
SELECT DISTINCT * FROM phone_original
WHERE (telnumber NOT IN
        (SELECT telnumber FROM phone_temp)
       )

SELECT * FROM phone_FINAL
__________________
keep your kneeze in the breeze





Similar Threads
Thread Thread Starter Forum Replies Last Post
remove duplicate text in an output html element mrame XSLT 1 June 20th, 2008 01:41 AM
How to remove duplicate row from sheet ketannsharma Excel VBA 1 February 18th, 2007 03:32 AM
Query producing duplicate results dba123 Reporting Services 0 February 16th, 2006 11:13 AM
wrote a Linked list , but failed why?? keepsmiling_sruti VB How-To 0 July 5th, 2005 04:37 AM
how to remove the query statement? gilgalbiblewheel Classic ASP Databases 8 September 11th, 2004 11:10 AM





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