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

  • Home
  • SEARCH
  • 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 8166701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:58:05+00:00 2026-06-06T19:58:05+00:00

Sorry for the ambiguous title; but I don’t now how to describe it different.

  • 0

Sorry for the ambiguous title; but I don’t now how to describe it different.

I have the following table:

imei | date       | time  | some more fields
345  | 2012-06-28 | 07:18 | .....
345  | 2012-06-28 | 07:20 | .....
345  | 2012-06-28 | 07:21 | .....
987  | 2012-06-28 | 07:19 | .....
etc etc

I want to get the latest row of ervery distinct imei, so:

345  | 2012-06-28 | 07:21
987  | 2012-06-28 | 07:19
  • Using SELECT * FROM t GROUP BY imei results in using the first line instead of the last one.
  • Ordering by time obviously orders the result relation instead of the sub.
  • Using having is only for stating a condition….

How can I write a query to do this?

  • 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-06-06T19:58:07+00:00Added an answer on June 6, 2026 at 7:58 pm

    As stated in the manual:

    The server is free to choose any value from each group, so unless they are the same, the values chosen are indeterminate. Furthermore, the selection of values from each group cannot be influenced by adding an ORDER BY clause. Sorting of the result set occurs after values have been chosen, and ORDER BY does not affect which values the server chooses.

    To obtain the groupwise maximum (as you want), you need to join the result of a grouped subquery with your table:

    SELECT * FROM t JOIN (
      SELECT imei, MAX(ADDTIME(date, time)) AS dt FROM t GROUP BY imei
    ) AS u ON t.imei = u.imei AND t.date = DATE(u.dt) AND t.time = TIME(u.dt)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry for the ambiguous title but I'm doing the following to write a simple
I have the following code (sorry for the large code chunk, but I could
Sorry for the ambiguous title, but this is a strange issue. I'm using the
Sorry for the ambiguous question but here I go. On each page a have
Sorry for the messy title, I just don't know how to describe the problem
I'm sorry for ambiguous title, but this question is hard for me to put
Sorry for the rather ambiguous title but I'm developing a Sencha Touch mobile app
Sorry, I know this question is easy, but I don't know how to get
All, Sorry in advance for the ambiguous title. Here's what I'm trying to do...
Sorry if the title is still ambiguous. I'm doing this assignment for school and

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.