Results 1 to 3 of 3
  1. #1
    JeroenMioch's Avatar
    JeroenMioch is offline Competent Performer
    Windows Vista Access 2003
    Join Date
    May 2012
    Location
    Den Haag, Netherlands
    Posts
    368

    SQL finding lowest date and oldest date from two duplicate records

    Right,



    I have a table that contains some duplicate records.
    These records are duplicate because they share the same personell number.

    These records contain more fields however. A name field and two date fields.
    the name field is not to be altered. But the date fields i need to range from the lowest to the higest.

    Lets say we have these records :

    Personell number Name Validfrom Validto

    997865 JeroenMioch 23-03-2012 30-05-2012
    997865 JeroenMioch 27-04-2012 02-07-2012

    So this is what i want the output to be :

    997865 JeroenMioch 23-03-2012 02-07-2012

    I figuered that the easiest way is to make an update SQL that spots the lowest date of the Validfrom field and updates the higest, and do the same with the validto field but then find the higest date.

    This way i end up with two identical records that contain data that i want, then run a delete duplicates query and im done.
    But im really really lost on how to code the SQL string.

    Anyone have any clue ?

    Thanks for thinking with me.

    Greetings : Jeroen

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    Do you really need to remove the records?

    A GROUP BY (Totals) aggregate query will give you the dates, unfortunately cannot be used in UPDATE action:
    SELECT [Personell Number], [Name], Min(Validfrom) As MinFrom, Max(Validto) As MaxTo GROUP BY [Personell Number], [Name];

    This UPDATE works:
    UPDATE Table1 SET Table1.Validfrom = DMin("Validfrom","Table1","[Personell Number]='" & [Personell Number] & "'"), Table1.Validto = DMax("Validto","Table1","[Personell Number]='" & [Personell Number] & "'");
    Last edited by June7; 12-08-2012 at 01:12 PM.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    JeroenMioch's Avatar
    JeroenMioch is offline Competent Performer
    Windows Vista Access 2003
    Join Date
    May 2012
    Location
    Den Haag, Netherlands
    Posts
    368
    That indeed works good.

    I read on another forum that running a totals query will work too.
    You need to run two querys, one for the Validto and one for the Validfrom field.
    You put two fields in the validto query and aggregate the Min for the one field and one for the max. Do likewise for the validfrom and it gives the desired results

    Thanks for the help June !

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 7
    Last Post: 11-29-2012, 11:36 AM
  2. Replies: 3
    Last Post: 07-18-2012, 10:13 PM
  3. Trouble Finding "Oldest" Data
    By bigdan5428 in forum Access
    Replies: 5
    Last Post: 04-26-2012, 05:25 PM
  4. Using expression builder to find lowest date
    By MMcKenna in forum Queries
    Replies: 1
    Last Post: 03-20-2012, 02:28 PM
  5. Replies: 3
    Last Post: 05-03-2011, 01:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums