473,394 Members | 1,889 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

SELECT duplicates without looping

Hi All,

I have a fairly large table with approx 30K rows that updates every
night via a cron script that automatically downloads the 2 new csv's.

The problem is the files are downloaded from two completely different
systems and I've recently found duplicates!

To make matters worse, the two feeds do not share a common unique key.

The following snippet is an example of the data where row 1 is from
feed 1 and row 2 is from feed 2

key|streetnum|streetname|streetsuffix|unitnum|
---------------------------------------------
12345|2069|Happy St.|NULL|A-2|
ACGH23|2069|Happy|St.|A-2|
So my question is: How can I identify duplicate records using multiple
columns without looping through the entire recordset in PHP?

I have successfully found other duplicates by COUNT(key) ...GROUP BY
key HAVING COUNT(key) >= 2.

I tried using CONCAT to combine the columns into one string but didn't
work because CONCAT fails if any column is NULL.

Any help would be greatly appreciated.

Tim

Oct 11 '05 #1
3 4711
be*****@gmail.com wrote:
key|streetnum|streetname|streetsuffix|unitnum|
---------------------------------------------
12345|2069|Happy St.|NULL|A-2|
ACGH23|2069|Happy|St.|A-2|
So my question is: How can I identify duplicate records using multiple
columns without looping through the entire recordset in PHP?
You can do a self-join:

SELECT t1.key, t2.key
FROM tbl AS t1 INNER JOIN tbl AS t2 ON
CONCAT_WS(' ', t1.streetnum, t1.streetname, t1.streetsuffix, t1.unitnum) =
CONCAT_WS(' ', t2.streetnum, t2.streetname, t2.streetsuffix, t2.unitnum)

That catches the specific example you give above. Unfortunately,
finding duplicates in such free-form data is one of those limitless
problems. There are so many ways that the data can be "the same" that
it's hard to automate.

For instance, what if `streetname` contains the string such as "Happy
St., suite A-2"? Is that equal to "Happy St #A2"? How can you make an
expression to compare these?

Database analysts are often assigned with multi-week projects to de-dupe
vast sets of data. It's unlikely that you'll ever catch 100% of the
duplicates through a single query, but you can catch some reasonable set
of them. Then it becomes a question of how much work is it worth to
catch the last few cases?

Sometimes the most successful method is to sort the dataset by
streetname (or other likely key) and then use your eyeballs to spot
duplicates. A very tedious task, but much more likely to spot all the
uncommon cases of duplicates.
I tried using CONCAT to combine the columns into one string but didn't
work because CONCAT fails if any column is NULL.


Read the description of CONCAT_WS() here:
http://dev.mysql.com/doc/mysql/en/string-functions.html

That function skips NULL arguments, instead of returning NULL for the
whole expression.

Regards,
Bill K.
Oct 11 '05 #2
Wow, that's awesome!

I appreciate your help.

While I've got your attention, what are some good resources for
advanced query writing?

I feel like I have grasped the basics of MySQL and am looking to go to
the next level.

Again, I thank you for you help.

Tim

Oct 11 '05 #3
be*****@gmail.com wrote:
While I've got your attention, what are some good resources for
advanced query writing?


I learned some from Joe Celko's books "SQL for Smarties" and "SQL
Programming Style". He also has a book "SQL Puzzles & Answers" but I
haven't read that one.

And also reading the questions and answers on comp.databases and various
MySQL newsgroups is very worthwhile.

There's no substitute for experience. Try exercises of complex queries.

But don't feel like you have to write the most complex query when you're
actually committing it in code for a project! The simplest code that
gets the job done is often the best, to help improve the code's
readability, extensibility, and maintenance.

Also, keep in mind that not every problem is best solved with a single
huge query that does everything. SQL was designed to be combined with
application code, and often that's the best way to massage query results
into an application data structure. Also, don't assume that a single
query always works faster than a multi-query solution.

I think these points are just as important for advanced usage of SQL as
learning every complex syntax you can use in a query. You're advanced
if you can recognize when it's appropriate and necessary to use a
complex query, and when you can do the job with a simpler one (or more
than one).

Regards,
Bill K.
Oct 11 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Vincent Jones | last post by:
my data is like so. Id Date Transaction 545 9/24/2003 3:01:08 PM 13051:10 546 9/24/2003 3:03:30 PM 13051:10 538 9/24/2003 2:53:31 PM 13051:1002 539 9/24/2003 2:54:57...
4
by: Justin Koivisto | last post by:
OK, out of my element here once again... I want a query (or something I can use as a record source for a report) to do the following: * look in 2 tables (one field each) to find all entered...
1
by: rickn | last post by:
Being new to VB and programming, I'm not sure how to modify the following or if required have a Select statement to NOT allow any duplicates. I'm trying not to have any duplicates in the LOTNUM_72...
6
by: Bob Stearns | last post by:
I am getting duplicate rows back from a select distinct statement of the form: SELECT DISTINCT 'jhough', '000111', t0.bhid FROM (SELECT lots of good stuff) t0 LEFT OUTER JOIN another_table ...
6
by: TCook | last post by:
Hello, I was wondering if anyone has a code snippet for looping through a 'select' control's 'option' elements? Do I have to use an ASP.Net web control such as an asp list control or dropdown...
22
by: MP | last post by:
vb6,ado,mdb,win2k i pass the sql string to the .Execute method on the open connection to Table_Name(const) db table fwiw (the connection opened via class wrapper:) msConnString = "Data Source="...
9
by: P3Eddie | last post by:
Hello all! I don't know if this can even be done, but I'm sure you will either help or suggest another avenue to accomplish the same. My problem may be a simple find duplicates / do something...
5
by: limperger | last post by:
Hello everyone! Is out there any way to search for duplicate entries without using the "Find duplicates" option? In the Access 97 installed in my workplace, the Find duplicates option is disabled...
2
by: pedalpete | last post by:
I seem to have a few duplicates entries in a very large database. My database has two identical tables. One is written to on a regular basis (every few seconds) and the other is used for queries,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.