473,769 Members | 6,337 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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|s treetname|stree tsuffix|unitnum |
---------------------------------------------
12345|2069|Happ y St.|NULL|A-2|
ACGH23|2069|Hap py|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 4756
be*****@gmail.c om wrote:
key|streetnum|s treetname|stree tsuffix|unitnum |
---------------------------------------------
12345|2069|Happ y St.|NULL|A-2|
ACGH23|2069|Hap py|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.c om 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
7891
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 PM 13051:1002 136 9/24/2003 10:08:45 AM 13051:101 137 9/24/2003 10:08:47 AM 13051:101
4
1730
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 values (without duplicates) * count the number of records in both tables that has that value for the field.
1
1658
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 field. The data is coming from odbc and being inserted via ole into access. IF the LOTNUM_72 field is set as Prinary Key, then no records are inserted if any duplicates are seen. I'n not sure at all how to make the coding so if a duplicate does...
6
13764
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 t1 ON relevant_stuff WHERE (lots of conditions) After re-reading the relevant pat ofVol 1 of the SQL Reference I am unablee to see how this is possible.
6
3231
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 to do such a thing? Thanks,
22
12493
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=" & msDbFilename moConn.Properties("Persist Security Info") = False moConn.ConnectionString = msConnString moConn.CursorLocation = adUseClient moConn.Mode = adModeReadWrite' or using default...same result
9
4397
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 with only one of the duplicates issue, but I got to the point where an update query with a nested select subquery would work wonderfully, if only it would work! I have several fields in a master Access 2000 table, some of which are id, fname,...
5
8288
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 (don't ask me why) and I think that there are little chances of having it installed. Any wonder of how to overcome this situation without the aforementioned wizard? Thank you very much in advance Best regards from Barcelona
2
8743
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, and updated from the write table on a daily basis. I'm trying to get rid of duplicate entries for the queries, and I thought the most effective way to do this would be to run the query as INSERT INTO readtable (SELECT * FROM writetable GROUP...
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10222
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10050
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9866
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8876
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3967
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3570
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.