Solved

Remove Duplicates based on Date Added

Posted on 2011-09-14
2
400 Views
Last Modified: 2012-08-14
I have an Access Table named tbl_TestAll  that I need to delete duplicates based on the Date Added date.

DtA is the Date Added field

The column names are liks such.
Cha, Allo, Tar, Cons, Opt, Des, Pct, Len, Cur, Com, DtA, For, Mar.

The rows are as such:

Row 1 Ca1, Allo1, Tar2, Cons1, Opt 1, Des1, Pct 3, Len1, Cur2, Com 2,  7/14//2001, For1, Mar1
Rpw 2 Ca1, Allo1, Tar3, Cons1, Opt 1, Des2, Pct 4, Len2, Cur3, Com 2,  9/14/2001,  For1, Mar1

So basically, Cha, Allo, Cons, Opt, For, MarI are duplicated in both rows.

I need a query that will delete Row 1, and keep Row 2 based on the latest date.
0
Comment
Question by:mato01
2 Comments
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 36539430
mato01,

In MS Access, it may be easiest to INSERT the rows you want into a new table, DELETE all rows, then put back the rows you want. You can try once you have done the insert into new table to DELETE where there is no match, so guess that is the key...

So, what I would try is to build a query that gets you the MAX DtA column that corresponds to Cha, Allo, Cons, Opt, For, MarI combinations.
SELECT Cha, Allo, Cons, Opt, For, MarI, MAX(DtA) MAX_DtA
FROM tbl_TestAll
GROUP BY Cha, Allo, Cons, Opt, For, MarI

Open in new window


If that looks like what you want, you can then JOIN that back to your original data, you may have to put it in an intermediate query first, but try:
SELECT t1.*
FROM tbl_TestAll AS t1
JOIN (
   SELECT Cha, Allo, Cons, Opt, For, MarI, MAX(DtA) MAX_DtA
   FROM tbl_TestAll
   GROUP BY Cha, Allo, Cons, Opt, For, MarI
) AS tm
   ON tm.Cha = t1.Cha
   AND tm.Allo = t1.Allo
   AND tm.Cons = t1.Cons
   AND tm.Opt = t1.Opt
   AND tm.For = t1.For
   AND tm.MarI = t1.MarI
   AND tm.MAX_Dta = t1.Dta
;

Open in new window


That should get you the records you want. Let me know if you have any questions from there as it is just a straight-forward, INSERT and DELETE operation.

And I can't remember if this works in MS Access, but you can try:
DELETE t1.*
FROM tbl_TestAll AS t1
JOIN (
   SELECT Cha, Allo, Cons, Opt, For, MarI, MAX(DtA) MAX_DtA
   FROM tbl_TestAll
   GROUP BY Cha, Allo, Cons, Opt, For, MarI
) AS tm
   ON tm.Cha = t1.Cha
   AND tm.Allo = t1.Allo
   AND tm.Cons = t1.Cons
   AND tm.Opt = t1.Opt
   AND tm.For = t1.For
   AND tm.MarI = t1.MarI
   AND tm.MAX_Dta <> t1.Dta
;

Open in new window


The change is not equal Dta.
0
 
LVL 1

Accepted Solution

by:
Milewskp earned 250 total points
ID: 36555312
Hi mato,
An aggregate query (call it Qry1) with MaxOfDtA will give you the rows with the latest dates. You could then base a delete qry on tbl_TestAll  and Qry1.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
HasData 9 39
Tags from access to excel 3 32
Track name AutoCorrect info 14 48
Is it possible to reset DSum? 12 44
The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

863 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now