How to query sql in MS Access 2007 Find Min & Max with DateTime?

  • i query sql in MS Access 2007.i want find mix and max function with datetime.

    This Code:

    SELECT inf.SSN AS EmpNo, ck.CHECKTIME AS CHKDATE,Min(ck.CHECKTIME) AS TIMEIN, Max(ck.CHECKTIME) AS TIMEOUT

    FROM CHECKINOUT ck INNER JOIN USERINFO inf ON ck.Badgenumber = inf.Badgenumber

    WHERE (((ck.CHECKTIME)>=#20/03/2013#)

    Group By inf.SSN, ck.CHECKTIME

    ORDER BY inf.SSN, ck.CHECKTIME

    Result:

    EmpNo | CHKDATE | TIMEIN | TIMEOUT

    1290005 | 20/3/2556 7:24:52 |20/3/2556 7:24:52 | 20/3/2556 7:24:52

    1290005 | 20/3/2556 19:07:54 |20/3/2556 19:07:54 | 20/3/2556 19:07:54

    1290005 | 21/3/2556 7:14:29 |21/3/2556 7:14:29 | 21/3/2556 7:14:29

    1320004 | 20/3/2556 7:28:57 |20/3/2556 7:28:57 | 20/3/2556 7:28:57

    1320004 | 20/3/2556 17:05:23 |20/3/2556 17:05:23 | 20/3/2556 17:05:23

    1320004 | 21/3/2556 7:15:30 |21/3/2556 7:15:30 | 21/3/2556 7:15:30

    But I want Result:

    EmpNo | CHKDATE | TIMEIN | TIMEOUT

    1290005 | 20/3/2556 00:00:00 |20/3/2556 7:24:52 | 20/3/2556 19:07:54

    1290005 | 21/3/2556 00:00:00 |21/3/2556 7:14:29 | 21/3/2556 7:14:29

    1320004 | 20/3/2556 00:00:00 |20/3/2556 7:28:57 | 20/3/2556 17:05:23

    1320004 | 21/3/2556 00:00:00 |21/3/2556 7:15:30 | 21/3/2556 7:15:30

    How query this result in Access 2007. Thanks For your Time. 😉

  • In order to solve this sort of problem using Group By queries, you need to use multiple queries. The Max function is simply taking the Max function of the record where the Min was found, and that's because the Min only has one record, the minimum (which is the CheckIn record). The problem is often solved by using ADO or DAO record processing from VBA, and simply stepping through the records. One complication that has to be dealt with in either case is where there is a checkin record but no checkout record. It shouldn't happen, but it invariably will at some point.

    Wendell

    Wendell
    Colorful Colorado
    You can't see the view if you don't climb the mountain!

  • You need to group by just the date portion of CHECKTIME - try this or similar

    SELECT inf.SSN AS EmpNo, Format(ck.CHECKTIME, "dd/mm/yyyy") AS CHKDATE,Min(ck.CHECKTIME) AS TIMEIN, Max(ck.CHECKTIME) AS TIMEOUT

    FROM CHECKINOUT ck INNER JOIN USERINFO inf ON ck.Badgenumber = inf.Badgenumber

    WHERE (((ck.CHECKTIME)>=#20/03/2013#)

    Group By inf.SSN, format(ck.CHECKTIME, "yyyymmdd")

    ORDER BY inf.SSN, format(ck.CHECKTIME, "yyyymmdd")

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply