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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:30:11+00:00 2026-06-05T08:30:11+00:00

I am trying to do a GROUP BY statement, with the grouped by column

  • 0

I am trying to do a GROUP BY statement, with the grouped by column showing the item with the newest timestamp. However, I don’t think it’s possible to order BEFORE a GROUP BY statement. Is the following subselect the only way to do what I’m trying to accomplish?

SELECT thread_id, content, timestamp FROM
(
  SELECT thread_id, content, timestamp FROM messaging_message 
  ORDER BY thread_id, timestamp desc
) combined 
GROUP BY thread_id    

Note that for a given thread_id, there may be multiple messages associated to it, and thus multiple content and timestamps for each thread_id.

  • 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-05T08:30:14+00:00Added an answer on June 5, 2026 at 8:30 am

    If I understand correctly and you want the most recent content per thread_id, use a MAX() aggregate to find the timestamp, and JOIN against it :

    SELECT thread_id, content, timestamp 
    FROM 
      messaging_message m
      JOIN (
         SELECT thread_id, MAX(timestamp) AS maxts
         FROM messaging_message 
         GROUP BY thread_id
      ) maxt ON m.thread_id = maxt.thread_id AND m.timestamp = maxt.maxts
    

    The ORDER BY doesn’t come into play at all by this method. It’s all done by grouping.

    MySQL, unlike other RDBMS doesn’t strictly require you to have every SELECT column accounted for in the GROUP BY, so you could probably just do

    SELECT thread_id, content, MAX(timestamp) AS maxts FROM messaging_message GROUP BY thread_id
    

    However, that isn’t portable and so I don’t recommend it. Instead, the JOIN subquery returns the pair of timestamp and thread_id. Those are used to match up against the related content and any other columns you may need from the row. If you had a unique id on each row, you could also make a subquery which returns only the id for each MAX(timestamp) and use it inside an IN(). But absent that unique id, a join against the thread_id, timestamp pair does the job.

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

Sidebar

Related Questions

In one statement I'm trying to group rows of one table by joining to
I'm trying to group time values in MS Access. I have a switch statement,
I have a database where I am trying to group together similar column values
I am trying to bind a grouped linq statement to a asp.net listview. Problem
Possible Duplicate: Running total by grouped records in table I am trying to put
I'm trying to make a group by statement using LINQ. I am getting an
I am trying to append a column created by using COUNT(*) and GROUP BY
I am trying to achieve the following: I have a single ORDER BY statement
I'm trying to group a set of data based on the range of an
I am trying to group a data collection and send it to the View

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.