Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7413435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:54:44+00:00 2026-05-29T06:54:44+00:00

I have a timeclock table with the following elements ( id(pk) , action ,

  • 0

I have a timeclock table with the following elements (id(pk), action, emp_id(fk), time).

How can I select the latest action if I pass the emp_id to the query?

id  emp_id  action  current_time
-----------------------------------------------
1   1   clockin     2012-01-29 21:52:38
2   1   clockout    2012-01-29 21:54:24
3   3   clock in    2012-01-30 21:10:51
4   4   clock in    2012-01-30 21:10:51
5   9   clock in    2012-01-30 21:11:05
6   10  clock in    2012-01-30 21:11:05
7   10  go to meal  2012-01-30 21:11:38
8   9   go to meal  2012-01-30 21:11:38
9   9   back        2012-01-30 21:12:53
10  3   back        2012-01-30 21:12:53
11  2   back        2012-01-30 21:12:53
12  1   back        2012-01-30 21:12:53

If I write my query like this:

SELECT MAX(ID) FROM timeclock WHERE emp_id = 1 

I get 12 which is right but if i

SELECT MAX(ID) , action FROM timeclock WHERE emp_id = 1

I get
id->12 and action -> 'clockin' instead of 'back' which is definitely not right

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T06:54:45+00:00Added an answer on May 29, 2026 at 6:54 am

    You’re missing a GROUP BY clause for your MAX() aggregate. The reason you got the correct answer 12 for your first query attempt was merely because that happens to be the greatest ID in the table while also coincidentally belonging to emp_id = 1. You would have gotten the same result for any of the emp_id values. A GROUP BY clause will sort this out.

    Here is an example retrieving the whole row for the associated record:

    SELECT * FROM timeclock 
    WHERE id = (SELECT MAX(id) AS id FROM timeclock WHERE emp_id = 1 GROUP BY emp_id);
    

    This can also be done with a HAVING clause, not needing the subquery:

    SELECT action 
    FROM timeclock 
    WHERE emp_id = 1
    GROUP BY emp_id
    HAVING id = MAX(id);
    

    For reference, the MySQL aggregate functions reference.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following questions about C1x mutexes (§7.25.4): In which situations can mtx_lock()
So I have a SQL table that contains time-clock information like this. The number
I have a timeclock system where i need to print a report for each
I have created a timeclock application in C# that connects to a web service
Have you managed to get Aptana Studio debugging to work? I tried following this,
i will have a c# application that will get data from a timeclock and
I have data defined like the ff.: import Data.Time.Clock data D = D {
My bike computer can show me various figures such as distance travelled, time elapsed,
I'm looking for a way to have multiple time clocks running along side the
I have a timeclock database setup on a Windows 2008 server, SQL Server. It

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.