Connecting Tech Pros Worldwide Forums | Help | Site Map

This should be easy, shouldn't it? Not Min query

Newbie
 
Join Date: Dec 2008
Posts: 27
#1: Feb 27 '09
Hi,

I have a table which will have records appended to it weekly. In my append query I have a field to identify when it was appended using Now(). Now what I want to do is delete the identical records that were appended later so each record should only appear when they were appended for the first time.

So I basically am trying to select all the records that are not MIN on the appended date so I can then turn it into a delete query. I hope this is clear? How would I do this?

Your help is much appreciated.

FishVal's Avatar
Expert
 
Join Date: Jun 2007
Location: Israel
Posts: 2,584
#2: Feb 27 '09

re: This should be easy, shouldn't it? Not Min query


Make an aggregating query returning "min" records for each group.
Use "... WHERE ... Not In ..." syntax in your delete query.

Regards,
Fish.
Newbie
 
Join Date: Dec 2008
Posts: 27
#3: Feb 27 '09

re: This should be easy, shouldn't it? Not Min query


I am fumbling around SQL and it's not doing what it's told. If I gave a mini example of the situation, maybe you could help with the SQL.

Table appeded records go into = "DMH"
Field identifying when record each record was appended = "Updated Date"
Other fields include = "Name", "ADM_AOS_CODE", "Email"

Query selecting earliest Appended records, the records I want to keep = "DMEU"
FishVal's Avatar
Expert
 
Join Date: Jun 2007
Location: Israel
Posts: 2,584
#4: Feb 27 '09

re: This should be easy, shouldn't it? Not Min query


What field defines record groups from which earliest records are to be returned?
Newbie
 
Join Date: Dec 2008
Posts: 27
#5: Feb 27 '09

re: This should be easy, shouldn't it? Not Min query


That is the "Updated Date" field. I run the MIN function on this with all other fields in my table in the query. Did I understand you correctly?
FishVal's Avatar
Expert
 
Join Date: Jun 2007
Location: Israel
Posts: 2,584
#6: Feb 27 '09

re: This should be easy, shouldn't it? Not Min query


Does it mean there is only one record to keep?
Newbie
 
Join Date: Dec 2008
Posts: 27
#7: Feb 27 '09

re: This should be easy, shouldn't it? Not Min query


No, because the "Name" can be different etc.
FishVal's Avatar
Expert
 
Join Date: Jun 2007
Location: Israel
Posts: 2,584
#8: Feb 27 '09

re: This should be easy, shouldn't it? Not Min query


Ok. [Name] defines group from which earliest record is kept.
Is any from the rest fields a part of group definition?
Newbie
 
Join Date: Dec 2008
Posts: 27
#9: Feb 27 '09

re: This should be easy, shouldn't it? Not Min query


Sorry. To be specific: [ADM_ID] will be the field that will define the group.
FishVal's Avatar
Expert
 
Join Date: Jun 2007
Location: Israel
Posts: 2,584
#10: Feb 27 '09

re: This should be easy, shouldn't it? Not Min query


Something like the following

Query: qryRecordsToKeep

Expand|Select|Wrap|Line Numbers
  1. SELECT [ADM_ID], Min([Updated Date]) AS dteEarliestDate FROM [DMH] GROUP BY [ADM_ID];
  2.  
Expand|Select|Wrap|Line Numbers
  1. DELETE * FROM [DMH]
  2. WHERE [ADM_ID] Not In (SELECT dteEarliestDate FROM qryRecordsToKeep
  3. );
  4.  
Regards,
Fish
Newbie
 
Join Date: Dec 2008
Posts: 27
#11: Feb 27 '09

re: This should be easy, shouldn't it? Not Min query


Excellent. Thankyou very much!
FishVal's Avatar
Expert
 
Join Date: Jun 2007
Location: Israel
Posts: 2,584
#12: Feb 27 '09

re: This should be easy, shouldn't it? Not Min query


You are welcome.
Reply

Tags
append, delete, min, query, table