I fully expect to have a duh moment when I read replies to this....BUT....
I have a table with duplicates in the [LN] field (could be 2 records with same value, might be 7 records). I need to delete all but the the most recent based on the [eDate] field.
I hope this is enough info.
Thanks.
This should handle the nesting of the subqueries and the duplications when [eDate]s match : - DELETE tDo.*
-
FROM [tblDuplicates] AS tDo
-
WHERE tDo.ID Not In(
-
SELECT TOP 1
-
tDi.ID
-
FROM [tblDuplicates] AS tDi
-
WHERE tDi.LN=tDo.LN
-
ORDER BY tDi.[eDate] DESC
-
, tDi.ID DESC
-
)
12 9686 @bard777
Do you have a unique key set up in your table?
If so, you could do something like this: - DELETE Table.* FROM Table
-
WHERE [ID] NOT IN (SELECT Last(ID) FROM
-
(SELECT ID, LN FROM Table ORDER BY eDate)
-
GROUP BY LN);
That's a lot more sub-queries then I like to put into a criteria expression, but it gives you the idea. Also - do you ever have two records with the same LN and the same eDate? Would you want to keep them all, or does it matter which is deleted?
Things are a little trickier if there's no ID field. Another thing to consider is you could select the data you want with a GROUP BY clause and make a new table from it. I wouldn't recommend it if this is something you will be repeating, but it might be an option depending on your setup.
@gershwyn
Yes, I have an ID field, so this is a good lead, thanks.
It is possible that [eDate] could be the same as well, but in this case either will do. The real issue is there is no data validation on the front end.
@bard777
The following function will open a recordset examining two columns LN and edate in a table named Table1 and stack records in Ascending order of LN and descending descending order of edate. The procedure loops records skipping over the first LN number (ie the latest date for that LN value) and deletes any other records having the same LN value in the stack. It repeats this process for each individual LN number found until the end of the table. The end result is an individual LN value in the first column and in the second column the latest date for that LN number. Obviously test this out on a copy table to see if it fits your needs - Function Get_Rid_Of_LNRecords_Except_Latest_EDate()
-
On Error GoTo Err_Get_Rid_Of_LNRecords_Except_Latest_EDate
-
Dim db As DAO.Database
-
Dim rst As DAO.Recordset
-
Set db = CurrentDb
-
mysql = "SELECT Table1.LN, Table1.eDate "
-
mysql = mysql & "FROM Table1 "
-
mysql = mysql & "ORDER BY Table1.LN, Table1.eDate DESC;"
-
-
' open a recordset of two columns
-
' sorting records by LN first then the edate descending
-
Set rst = db.OpenRecordset(mysql, dbOpenDynaset)
-
' if a dataset is available loop through records
-
' skipping the first LN record encountered and deleting the rest
-
' the first LN record encountered will be first LN record for any given date
-
-
mypointer = rst!LN
-
-
Do While Not rst.BOF And Not rst.EOF
-
'skip the first one
-
rst.MoveNext
-
If mypointer = rst!LN Then
-
rst.Delete
-
Else
-
'ressign the pointer because we have encountered
-
' a new LN
-
mypointer = rst!LN
-
End If
-
Loop
-
-
rst.Close
-
mysql = ""
-
Set rst = Nothing
-
Set db = Nothing
-
MsgBox "Process Complete", vbInformation, "System Message"
-
Exit_Get_Rid_Of_LNRecords_Except_Latest_EDate:
-
Exit Function
-
-
Err_Get_Rid_Of_LNRecords_Except_Latest_EDate:
-
If Err = 3021 Then
-
'just ignore because encountered last row
-
MsgBox "Process Complete", vbInformation, "System Message"
-
Else
-
MsgBox "Error " & Err.Number & " " & Err.Description
-
End If
-
Resume Exit_Get_Rid_Of_LNRecords_Except_Latest_EDate
-
-
End Function
You can run the above if you paste it as is into a new module and then place a command button on a form (without using the wizard) then in the property box for the command button at the 'On click' event just type this =Get_Rid_Of_LNRecords_Except_Latest_EDate() @Jim Doherty
Thanks Jim...I had already started donig something similar so I went with that (for now anyways).
Here is what I have, but it is not working: - Set db = CurrentDb
-
Dim rsDEDUPE As Recordset
-
Dim varLN As String
-
Dim varTic As Long
-
Dim varToc As Long
-
-
varTic = 0 ' count total deleted duplicate records
-
varToc = 0 ' count total for duplicate recordset
-
-
Set rsDEDUPE = db.OpenRecordset("_1duplicates for tbl_exceptions_CSV") ' rename query
-
rsDEDUPE.MoveFirst
-
-
Do While Not rsDEDUPE.EOF
-
-
If Not rsDEDUPE.EOF Then
-
varLN = rsDEDUPE(0)
-
rsDEDUPE.MoveNext
-
varToc = varToc + 1
-
End If
-
-
If Not rsDEDUPE.EOF Then
-
If Trim(varLN) = Trim(rsDEDUPE(0)) Then
-
-
DoCmd.RunSQL ("DELETE * FROM tbl_exceptions_CSV WHERE [AutoKey] = " & rsDEDUPE(2))
-
-
varTic = varTic + 1
-
End If
-
End If
-
-
Loop
It is giving me a "Record Deleted" error on the live that is:
I have gotten this error in the past when I have a query that has a linked table that is linked to a linked table. The table that the query uses to create the recordset is local, so I don't know why this would be happening.
Have you looked at the logic of the one I sent you. I did take the time out to present you with something that aligns itself to your original post!
I am not so sure I understand what you are experiencing with the #deletion# thing. If you have an openrecordset and delete records in SQL (Delete From X Where etc etc) at the same time then obviously the recordset will reflect that
NeoPa 32,556
Expert Mod 16PB
This should handle the nesting of the subqueries and the duplications when [eDate]s match : - DELETE tDo.*
-
FROM [tblDuplicates] AS tDo
-
WHERE tDo.ID Not In(
-
SELECT TOP 1
-
tDi.ID
-
FROM [tblDuplicates] AS tDi
-
WHERE tDi.LN=tDo.LN
-
ORDER BY tDi.[eDate] DESC
-
, tDi.ID DESC
-
)
@NeoPa
Just a small point Neopa' the correlated inner loop needs descending order edate sorting otherwise the poster will be left with the 'earliest date' as opposed to the latest date having the benefit of two insights SQL and VBA. What more can one need?
NeoPa 32,556
Expert Mod 16PB
You're absolutely right of course Jim :)
Strangely I've done a couple of similar ones today and thought I'd included that in this one, as I did in the other. Thanks for catching.
Thanks NeoPa, that did the trick! I thought it could all be done in a query, I just couldn't wrap my mind around the logic. I also found out something interesting about using TOP 1....it will return multiple records if they meet the criteria (I didn't include the ID field in the ORDER first time around), didn't know it would do that.
Jim, thanks for the time you spent on your method. I had already started working on some VBA along the same lines before I saw your code. I didn't ignore your effort, I was trying to work with what I had instead of re-writing at that point. I did mean to seem ungratelful for your help.
NeoPa 32,556
Expert Mod 16PB
No worries Bard. The TOP predicate catches many of us out believe me ;)
@bard777
Don't worry about it Bard I am not the 'sensitive soul' :) I much rather prefer that multiple methods came piling into threads the browsing individual not just yourself then has choice according to their particular flavour of working.
SQL,s TOP predicate will return more than 'one' record as you experienced because it returns TIES by default in Access (whereas in SQL Server it does not you explicitly request it) You simply code that out by using the DISTINCT predicate
NeoPa 32,556
Expert Mod 16PB
Interesting Jim (DISTINCT predicate of the SELECT clause for handling ties). I normally include a unique reference field in the sorting to avoid them (as in current thread).
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Nova's Taylor |
last post by:
Hi folks,
I am a newbie to Python and am hoping that someone can get me started
on a log parser that I am trying to write.
The log is an ASCII file that contains a process identifier (PID),...
|
by: Rubinho |
last post by:
I've a list with duplicate members and I need to make each entry
unique.
I've come up with two ways of doing it and I'd like some input on what
would be considered more pythonic (or at least...
|
by: Shaiguy |
last post by:
I have a table containing the following fields:
ProjectUpdateID (PrimaryKey)
ProjectID
UpdateDate
I would like to create a Query in Ms Access 2000 which will return me
the most recent 2...
|
by: William Wisnieski |
last post by:
Hello Everyone,
I'm helping out a non-profit school with their database. They would like to
know the last gift made by each donor, the donor name, and the gift amount.
I built a query based...
|
by: Lee |
last post by:
I have an application where I need to find the most recent file placed in a
folder. For example, my users periodically place an image file with the
format "ImageXXXX.jpg" (where XXXX is some...
|
by: psuaudi |
last post by:
i have a table with dates that events occured. it looks something like this:
11/18/2006 1:00PM Open
11/18/2006 1:25PM Close
I created a select query that selects the last (most recent)...
|
by: Jason H |
last post by:
Hi,
I am sure I am just overlooking the obvious, but I am having a little trouble with this one...
I am setting up an inventory database that tracks company tools and their location. This...
|
by: Sector 7G |
last post by:
I'm working with a SQL query for a Human Resources database. Its intended purpose is to find all the paycheck records with a check date (prckhist.chkdate ) more recent than eleven days past the...
|
by: robert.waters |
last post by:
I need to perform the following:
- select the most recent X number of records in a table (there is a
timestamp field)
- select the Nth occurrence of X number of records
ex:
- most recent 10...
|
by: zacks |
last post by:
Most applications whose purpose is to work with various types of files
implement a "Most Recent Files" list, where the last, say, four files
accessed by the application can quickly be re-opened by...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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: 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,...
|
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...
| |