Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old April 29th, 2008, 09:34 AM
Authorized User
 
Join Date: Jan 2008
Location: kokomo, IN, USA.
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
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
remove duplicate text in an output html element mrame XSLT 1 June 20th, 2008 02: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 05:37 AM
how to remove the query statement? gilgalbiblewheel Classic ASP Databases 8 September 11th, 2004 12:10 PM



All times are GMT -4. The time now is 08:11 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.