Solved

MySQL: Finding Oldest Douplicate

Posted on 2014-02-11
3
254 Views
Last Modified: 2014-02-11
The returns the bookkey and updated time of duplicate items:
SELECT elqBooks.isbn, elqBooks.updated
 FROM elqBooks 
 GROUP BY elqBooks.ISBN HAVING count(elqBooks.isbn) > 1

Open in new window

I want the OLDEST item to be selected.  How can I get the OLDEST matching duplicated item?
0
Comment
Question by:hankknight
3 Comments
 
LVL 34

Accepted Solution

by:
Dan Craciun earned 500 total points
ID: 39850012
SELECT elqBooks.isbn, MIN(elqBooks.updated)
 FROM elqBooks
 GROUP BY elqBooks.ISBN HAVING count(elqBooks.isbn) > 1
0
 
LVL 16

Author Comment

by:hankknight
ID: 39850068
Thanks.  Now how can I do this with the bookkey only?  I only want the bookkey of the oldest douplicate record to be included.
SELECT elqBooks.bookkey
 FROM elqBooks 
 GROUP BY elqBooks.ISBN HAVING count(elqBooks.isbn) > 1

Open in new window

0
 
LVL 11

Expert Comment

by:John_Vidmar
ID: 39850188
Without knowing your schema, would you be able to take the results of the group-by/min and join back to your table to get the key?
SELECT	a.bookkey
FROM	elqBooks	a
JOIN	(	SELECT	isbn
		,	updated = MIN(updated)
		FROM	elqBooks 
		GROUP
		BY	isbn
		HAVING	count(*) > 1 
	)	b	ON	a.isbn = b.isbn
			AND	a.updated = b.updated

Open in new window

0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question