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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:42:42+00:00 2026-05-25T06:42:42+00:00

I have a table with data like this: +———–+——-+——+———-+ | timestamp | event |

  • 0

I have a table with data like this:

+-----------+-------+------+----------+
| timestamp | event | data | moreData |
+-----------+-------+------+----------+
| 100000000 |     1 |   10 |       20 |
| 100000001 |     1 |   15 |       10 |
| 100000002 |     1 |   30 |       30 |
| 100000003 |     1 |    5 |       50 |
| 100000004 |     2 |  110 |      120 |
| 100000005 |     2 |  115 |      110 |
| 100000006 |     2 |  130 |      130 |
| 100000007 |     2 |   15 |      150 |
+-----------+-------+------+----------+

Now I want to select only the newest rows for each event. So in the end I want to have this result set:

+-----------+-------+------+----------+
| timestamp | event | data | moreData |
+-----------+-------+------+----------+
| 100000003 |     1 |    5 |       50 |
| 100000007 |     2 |   15 |      150 |
+-----------+-------+------+----------+

So far I was not able to do this. In MySQL I can use “GROUP BY event” but then I get some random row from the database, not the newest. ORDER BY doesn’t help because the grouping is done before ordering. Using an aggregation like MAX(timestamp) while grouping by event also doesn’t help because then the timestamp is the newest but “data” and “moreData” is still from some other random row.

I guess I have to do a sub select so I have to first get the latest timestamp like this:

SELECT MAX(timestamp), event FROM mytable GROUP BY event

and then use the result set to filter a second SELECT, but how? And maybe there is a clever way to do it without a sub select?

  • 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-25T06:42:43+00:00Added an answer on May 25, 2026 at 6:42 am

    AFAIK, sub select is your best option, as follows:

    SELECT * 
    FROM mytable mt 
        JOIN ( SELECT MAX(timestamp) as max, event 
               FROM mytable 
               GROUP BY event) m_mt 
        ON (mt.timestamp = m_mt.max AND mt.event = m_mt.event);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table like this: Application,Program,UsedObject It can have data like this: A,P1,ZZ
I'm a bit confused on this. I have a data table structured like this:
I have a table like this... CustomerID DBColumnName Data 1 FirstName Joe 1 MiddleName
i have a table data like this (int) (time) [mySQL datatype] Id Duration ------------------
I have UserScores Table with data like this: Id userId Score 1 1 10
I have table one which looks like this. And I want to get data
I have table data which looks like this id | keyword | count |
I have a table with data that looks like this: create table demo_patient_info (
If I have a table of data like this tableid author book pubdate 1
I have my table schema and data something like this CompanyCategoryId SubCategoryId CategoryId CompanyId

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.