Querying Times Using Greater Than/Less Than Criteria

Posted on 2006-03-29
Last Modified: 2008-01-09
I am trying to build a query that tells me if a user logged into the system late.

Table "Temp_Agent_Login" has their logins and logouts and their logid
Table "Reference_Agents" has their logid and emp_id
Table "Temp_Reference_Segments" that has their scheduled login and logout time and emp_id.

I built it in Access 97. Here is the SQL view:

SELECT Temp_Agent_Log.row_date, Reference_Agents.CMSName, Temp_Agent_Log.logid, Min(Temp_Agent_Log.login) AS MinOflogin, Temp_Reference_Segments.START_MOMENT
FROM Temp_Agent_Log INNER JOIN (Reference_Agents INNER JOIN Temp_Reference_Segments ON Reference_Agents.AWID = Temp_Reference_Segments.EMP_ID) ON Temp_Agent_Log.logid = Reference_Agents.CMS_ID
GROUP BY Temp_Agent_Log.row_date, Reference_Agents.CMSName, Temp_Agent_Log.logid, Temp_Reference_Segments.START_MOMENT, Temp_Reference_Segments.SEG_CODE
HAVING (((Temp_Reference_Segments.SEG_CODE)="shift"))
ORDER BY Reference_Agents.CMSName;

I want the query only to output the records where Min(Temp_Agent_Log.login) is greater than Temp_Reference_Segments.START_MOMENT by 5 or more minutes.

Min(Temp_Agent_Log.login) and Temp_Reference_Segments.START_MOMENT are both in Long Time format (e.g. 6:00:00 AM).

I tried to put this in the criteria for Min(Temp_Agent_Log.login) but got no results:

I also tried this with no results:
> [Temp_Reference_Segments]![START_MOMENT]

I'm stuck. I have no clue how to proceed.
Question by:makila
    LVL 28

    Expert Comment

    Try this (in design view)
    Add another column, using WHERE as the aggregation
    Datediff("n", temp_reference_segments.start_moment,min(temp_agent_log.login)) >4
    BTW-What results are you getting?
    LVL 18

    Expert Comment

    Use the DateDiff function to compare your dates.


    This would return the difference in minutes between two dates then you can test like you wanted.

      If x>5 then...

    Here's a reference online. Also look in your help file for more on the function.

    Author Comment


    I did what you said in design view and get this error message:
    Can't have aggregate function in WHERE clauses

    I created a new column and in the Field just let it autopop with Expr1: [Expr1], I left Table blank, selected Where for the Total, unchecked Show and put your formula in the Criteria.
    LVL 28

    Accepted Solution

    SELECT Temp_Agent_Log.row_date, Reference_Agents.cmsName, Temp_Agent_Log.logID, Min(Temp_Agent_Log.login) AS MinOflogin, Temp_Reference_Segments.start_moment
    FROM Temp_Agent_Log INNER JOIN (Reference_Agents INNER JOIN Temp_Reference_Segments ON Reference_Agents.awid = Temp_Reference_Segments.emp_id) ON Temp_Agent_Log.logID = Reference_Agents.cms_ID
    GROUP BY Temp_Agent_Log.row_date, Reference_Agents.cmsName, Temp_Agent_Log.logID, Temp_Reference_Segments.start_moment, Temp_Reference_Segments.seg_code
    HAVING (((Temp_Reference_Segments.seg_code)="shift") AND ((DateDiff("n",[start_moment],Min([login])))>4))
    ORDER BY Reference_Agents.cmsName;
    Instead of using WHERE aggregation, I merely used EXPRESSION as aggregation, and unchecked the VISIBLE box.

    Author Comment

    Perfect! That worked. Thanks!
    LVL 28

    Expert Comment

    Thanks. Glad to help.  

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Suggested Solutions

    I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
    Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
    Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
    What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now