Results 1 to 6 of 6
  1. #1
    ghnogueira is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2014
    Posts
    8

    Question Query that returns the first record of each client depending on a filter

    Hello!



    I have a database where I need to return only the first record of each key (named "process key") accordingly to a time frame I set.

    Consider the table below:

    Process Key Contact date Salesman Client Key Client
    999 04/28/2014 11:39:00 SALESMAN 1 111 CLIENT 1
    888 04/28/2014 10:13:00 SALESMAN 1 222 CLIENT 2
    555 04/22/2014 14:29:00 SALESMAN 2 333 CLIENT 3
    666 04/16/2014 12:14:00 SALESMAN 3 444 CLIENT 4
    555 04/16/2014 11:47:00 SALESMAN 2 333 CLIENT 3










    If I filter the date, e.g., from April 1st to 30th, I want to get as a result 4 records, one of each client and only the latest record from CLIENT 3 (04/22).

    If I filter the date, e.g., from April 1st to 20th, I want to get as a result only the records of clients 4 and 3 from 04/16 (as every other contact is from beyond 04/20).

    If I filter the date, e.g., from April 25th to 30th, the return must be only the top 2 records (the only ones after April 25th).

    I tried using a query grouping the totals on "process key" and the other cells set as "first", but if I filter from April 1st to 20th, for example, no records from CLIENT 3 gets shown, since Access processed the "first" record request before the date filter, and therefore only considered the contact from 04/22, and as it is later than 04/20, wasn't shown on the query.

    What should I do?

    Thanks!!!

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    lfpm062010 is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2013
    Location
    US
    Posts
    415
    Try this, let's call the table "CLIENTS". (Not tested)

    SELECT CLIENTS.[Process Key], First(CLIENTS.[Contact date]) AS [FirstOfContact date], First(CLIENTS.Salesman) AS FirstOfSalesman, First(CLIENTS.[Client Key]) AS [FirstOfClient Key]
    FROM CLIENTS
    HERE CLIENTS.[Contact date] BETWEEN [DATE BEGIN] AND [DATE END]
    GROUP BY CLIENTS.[Process Key]
    ORDER BY CLIENTS.[Process Key], First(CLIENTS.[Contact date]) DESC;

  4. #4
    ghnogueira is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2014
    Posts
    8
    Quote Originally Posted by lfpm062010 View Post
    Try this, let's call the table "CLIENTS". (Not tested)

    SELECT CLIENTS.[Process Key], First(CLIENTS.[Contact date]) AS [FirstOfContact date], First(CLIENTS.Salesman) AS FirstOfSalesman, First(CLIENTS.[Client Key]) AS [FirstOfClient Key]
    FROM CLIENTS
    HERE CLIENTS.[Contact date] BETWEEN [DATE BEGIN] AND [DATE END]
    GROUP BY CLIENTS.[Process Key]
    ORDER BY CLIENTS.[Process Key], First(CLIENTS.[Contact date]) DESC;
    Perfect mate!!!

    Thanks a lot!!!

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    ghnogueira is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2014
    Posts
    8
    Quote Originally Posted by pbaldy View Post
    Hey mate! So far all my results have been correct with the First function there...

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

Similar Threads

  1. Replies: 21
    Last Post: 04-29-2015, 11:57 PM
  2. Replies: 9
    Last Post: 10-29-2013, 02:13 PM
  3. Replies: 6
    Last Post: 07-21-2013, 10:43 PM
  4. Replies: 2
    Last Post: 09-04-2012, 03:26 PM
  5. Replies: 1
    Last Post: 04-12-2012, 11:34 AM

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