Results 1 to 3 of 3
  1. #1
    zx3 is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Aug 2011
    Posts
    47

    Question Max day apart between 2 consecutive records by employee

    Hi all,



    I have a project i need to get records that have the maximum days interval by employee

    Say i start with the following
    dt emp amt
    1-Dec-14 aa 11
    1-Dec-14 bb 2
    28-Nov-14 aa 32
    27-Nov-14 bb 14
    26-Nov-14 bb 32
    10-Nov-14 aa 18

    dt is date, emp is employee name, amt is sales amount dapart is days apart

    the query has to get the following result
    dt emp amt dapart
    28-Nov-14 aa 32 18
    1-Dec-14 bb 2 4


    28 nov14 is for aa because it's 18 days apart from prev aa's record and 3 days apart from next aa's record.

    Please help.
    Last edited by zx3; 12-09-2014 at 03:38 AM.

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,959
    One way is with nested subquery, review http://allenbrowne.com/subquery-01.html#AnotherRecord

    Another way might be with domain aggregate DMax(), however domain aggregates can be slower performers.
    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
    zx3 is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Aug 2011
    Posts
    47
    Thank June7 for the link, based on the link, i made a workable query as following

    Code:
    SELECT f.dt, f.emp, f.amt, f.apart as dapart FROM(
    SELECT dt, dt-pd AS [apart], emp, amt FROM (
    SELECT dt, emp, amt, 
    (SELECT MAX(dt) FROM TA T1 WHERE T1.emp = T.emp AND T1.dt < T.dt) AS pd 
    FROM TA AS T)) AS f INNER JOIN (
    SELECT emp, Max(apart) as dapart FROM(
    SELECT dt, dt-pd AS [apart], emp, amt FROM (
    SELECT dt, emp, amt, 
    (SELECT MAX(dt) FROM TA T1 WHERE T1.emp = T.emp AND T1.dt < T.dt) AS pd 
    FROM TA AS T))
    GROUP BY emp
    ) AS l ON (f.emp=l.emp) and (f.apart=l.dapart);
    It sounds too complicated although it works, hope some expert here can refine it bit to make it simple, or else after a day i will close this thread as solved.
    Thank you guys

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

Similar Threads

  1. Multiple records assigned to one Employee
    By Bosakie in forum Forms
    Replies: 3
    Last Post: 10-08-2014, 09:29 AM
  2. Replies: 6
    Last Post: 12-06-2013, 11:00 AM
  3. Replies: 2
    Last Post: 07-29-2012, 05:52 PM
  4. multiple records with consecutive dates
    By sotssax in forum Forms
    Replies: 2
    Last Post: 07-26-2011, 04:23 AM
  5. Employee Training Records
    By Penn State Girl in forum Database Design
    Replies: 8
    Last Post: 06-06-2011, 08:09 AM

Tags for this Thread

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