Querying Times Using Greater Than/Less Than Criteria

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.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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?
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.
makilaAuthor Commented:

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.
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
makilaAuthor Commented:
Perfect! That worked. Thanks!
Thanks. Glad to help.  
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.