473,403 Members | 2,323 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,403 software developers and data experts.

Howto: Delete every second record if duplicates

Hi.

I have a "union" table which results of a union of two tables.
Occasionally I could have duplicates, when the same PIN has been added
to both tables, albeit at different Datees/Times, such as:

PIN Name Added Date
100411 A 7/11/2007 10:12:58 AM
100411 A 7/17/2007 10:54:23 AM
100413 B 7/11/2007 10:13:28 AM
100413 B 7/17/2007 10:54:39 AM
104229 C 7/6/2007 2:34:13 PM
104231 D 7/6/2007 2:34:25 PM
104869 E 6/10/2007 11:59:12 AM
104869 E 6/22/2007 2:40:18 PM

The question is - how can I delete by queries the first occurence
(time-wise) of these duplicates - i.e. I would want to delete the
first occurence of 100411 (A), the first occurence of 100413 (B), and
the first occurence of 104869 (E) in the example above - records C and
D show only once, so they are fine.

Is there a MsAccess solution ? Is there a SQL-server solution ?

Thank you very much !
Alex

Jul 19 '07 #1
2 5405
Radu (cu*************@yahoo.com) writes:
I have a "union" table which results of a union of two tables.
Occasionally I could have duplicates, when the same PIN has been added
to both tables, albeit at different Datees/Times, such as:

PIN Name Added Date
100411 A 7/11/2007 10:12:58 AM
100411 A 7/17/2007 10:54:23 AM
100413 B 7/11/2007 10:13:28 AM
100413 B 7/17/2007 10:54:39 AM
104229 C 7/6/2007 2:34:13 PM
104231 D 7/6/2007 2:34:25 PM
104869 E 6/10/2007 11:59:12 AM
104869 E 6/22/2007 2:40:18 PM

The question is - how can I delete by queries the first occurence
(time-wise) of these duplicates - i.e. I would want to delete the
first occurence of 100411 (A), the first occurence of 100413 (B), and
the first occurence of 104869 (E) in the example above - records C and
D show only once, so they are fine.

Is there a MsAccess solution ? Is there a SQL-server solution ?
What about a primary key on (PIN, Name) to prevent this from happening
in the first place?

It's unclear what you with first occurrance, but I take to mean that
you mean the one with the earliest value of Added Date:

DELETE tbl
FROM tbl a
JOIN (SELECT PIN, Name, AddedDate = MIN(AddedDate)
FROM tbl
GROUP BY PIN, Name) AS b ON a.PIN = b.PIN
AND a.Name = b.Name
AND a.AddedDate b.AddedDate

This solution is for SQL Server. For Access solutions, try another
newsgroup.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Jul 19 '07 #2
On Thu, 19 Jul 2007 20:28:34 -0000, Radu wrote:
>Hi.

I have a "union" table which results of a union of two tables.
Occasionally I could have duplicates, when the same PIN has been added
to both tables, albeit at different Datees/Times, such as:

PIN Name Added Date
100411 A 7/11/2007 10:12:58 AM
100411 A 7/17/2007 10:54:23 AM
100413 B 7/11/2007 10:13:28 AM
100413 B 7/17/2007 10:54:39 AM
104229 C 7/6/2007 2:34:13 PM
104231 D 7/6/2007 2:34:25 PM
104869 E 6/10/2007 11:59:12 AM
104869 E 6/22/2007 2:40:18 PM

The question is - how can I delete by queries the first occurence
(time-wise) of these duplicates - i.e. I would want to delete the
first occurence of 100411 (A), the first occurence of 100413 (B), and
the first occurence of 104869 (E) in the example above - records C and
D show only once, so they are fine.

Is there a MsAccess solution ? Is there a SQL-server solution ?

Thank you very much !
Alex
Hi Alex,

Your mention of a union makes me suspect that you don't want to remove
duplicate rows from a base table, but rather not include duplicate rows
in a view or query, without changing base data. If this suspicion is
correct, then try

SELECT PIN, Name, MIN("Added Date")
FROM YourView;

If I'm incorrect and you want to actually remove duplicated rows from a
base table, then see Erland's reply.

--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Jul 20 '07 #3

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

Similar topics

1
by: Patrizio | last post by:
Hi All, I've the following table with a PK defined on an IDENTITY column (INSERT_SEQ): CREATE TABLE MYDATA ( MID NUMERIC(19,0) NOT NULL, MYVALUE FLOAT NOT NULL, TIMEKEY ...
3
by: Alexander Anderson | last post by:
I have a DELETE statement that deletes duplicate data from a table. It takes a long time to execute, so I thought I'd seek advice here. The structure of the table is little funny. The following is...
16
by: Philip Boonzaaier | last post by:
I want to be able to generate SQL statements that will go through a list of data, effectively row by row, enquire on the database if this exists in the selected table- If it exists, then the colums...
1
by: Smythe32 | last post by:
If anyone could help, I would appreciate it. I have a table as listed below. I need to check for duplicates by the OrderItem field and if there are duplicates, it then needs to keep the...
8
by: Steve | last post by:
I have several pairs of synchronized subforms in an application. I have a Delete button for each pair that uses the following code or similar to delete a record in the second subform: ...
4
by: DEWright_CA | last post by:
Hi Everyone! I am working on a project in C# and have a table the contains state info, plus a variety of other info that I will reference from another dropdown list. How can I do this...
0
by: Radu | last post by:
Hi. I have a "union" table which results of a union of two tables. Occasionally I could have duplicates, when the same PIN has been added to both tables, albeit at different Datees/Times, such...
7
by: AccessHunter | last post by:
I have a access query that has more than 400000 records including duplicates.First I need help in finding a way to filter the whole 400000 records and list all duplicates. Lets say ID, Job &...
3
allingame
by: allingame | last post by:
Need help with append and delete duplicates I have tables namely 1)emp, 2)time and 3)payroll TABLE emp ssn text U]PK name text
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
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...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...

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.