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 7705827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:57:33+00:00 2026-05-31T23:57:33+00:00

I have a table without IDs. it has 3 columns: the name of a

  • 0

I have a table without IDs. it has 3 columns: the name of a computer, its status (on/off) at the moment of the poll, and the timestamp of the insertion.

if I run

select * from computers group by name;

I get a line for each computer (there are 200 different ones), but these lines don’t always hold the latest entry for it.
I then tried

select computers group by name order by timestamp asc;

But I get incoherent responses (some recent timestamps, some old ones… no idea why).

It’s basically the same problem as here : SQL: GROUP BY records and then get last record from each group?, but I don’t have ids to help 🙁

  • 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-31T23:57:34+00:00Added an answer on May 31, 2026 at 11:57 pm

    You can write:

    SELECT computers.name,
           computers.status,
           computers.timestamp
      FROM ( SELECT name,
                    MAX(timestamp) AS max_timestamp
               FROM computers
              GROUP
                 BY name
           ) AS t
      JOIN computers
        ON computers.name = t.name
       AND computers.timestamp = t.max_timestamp
    ;
    

    The above uses this subquery to finds the greatest timestamp for each name:

    SELECT name
           MAX(timestamp) AS max_timestamp
      FROM computers
     GROUP
        BY name
    ;
    

    and then it gathers fields from computers whose name and timestamp match something that the subquery returned.

    The reason that your order by clause has no effect is that it comes too “late”: it’s used to order records that are going to be returned, after it’s already determined that they will be returned. To quote from §11.16.3 “GROUP BY and HAVING with Hidden Columns” in the MySQL 5.6 Reference Manual on this subject:

    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.

    Another way is to write a correlated subquery, and dispense with the GROUP BY entirely. This:

    SELECT name, status, timestamp
      FROM computers AS c1
     WHERE NOT EXISTS
            ( SELECT 1
                FROM computers
               WHERE name = c1.name
                 AND timestamp > c1.timestamp
            )
    ;
    

    finds all rows in computers that haven’t been superseded by more-recent rows with the same name. The same approach can be done with a join:

    SELECT c1.name, c1.status, c1.timestamp
      FROM computers AS c1
      LEFT
     OUTER
      JOIN computers AS c2
        ON c2.name = c1.name
       AND c2.timestamp > c1.timestamp
     WHERE c2.name IS NULL
    ;
    

    which is less clear IMHO, but may perform better.

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

Sidebar

Related Questions

I have a table that has IDs and Strings and I need to be
I have table inside a div tab. The table has 40 rows in it
I have table with 50 entries (users with such details like Name Surname Location
I have table with 3 columns A B C. I want to select *
I have a table with some ids, and i want in default.aspx to open
I have a table that looks like this: ID |Name |Parent 1 |A |NULL
I have a table with about 1000 records and 2000 columns. What I want
I have an HTML page that has many nested tables, for example table for
I have a table like: Users (user_id, name, age, country_id) Now I want a
So imagine that you have a table of Products (ID int, Name nvarchar(200)) ,

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.