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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:23:01+00:00 2026-05-16T18:23:01+00:00

I have a messages table which looks like this: +————+————-+———-+ | sender_id | created_at

  • 0

I have a messages table which looks like this:

+------------+-------------+----------+
| sender_id  |  created_at | message  |
+------------+-------------+----------+
|      1     | 2010-06-14  | the msg  |
|      1     | 2010-06-15  | the msg  |
|      2     | 2010-06-16  | the msg  |
|      3     | 2010-06-14  | the msg  |
+------------+-------------+----------|

I want to select the single most recent message for each sender.

This seems like a GROUP BY sender_id and ORDER BY created_at but I’m having trouble getting the most recent message selected.

I’m using postgres so need an aggregate function on the created_at field in the SELECT statement if I want to order by that field so I was looking at doing something like this as an initial test

SELECT messages.sender_id, MAX(messages.created_at) as the_date 
FROM messages 
GROUP BY sender_id 
ORDER BY the_date DESC 
LIMIT 10;

This seems to work but when I want to select ‘message’ as well I have no idea what aggregate function to use on it. I basically just want the message that corresponds to the MAX created_at.

Is there some way of getting at this or am I approaching it the wrong way?

  • 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-16T18:23:01+00:00Added an answer on May 16, 2026 at 6:23 pm

    This:

    SELECT  *
    FROM    (
            SELECT  DISTINCT ON (sender_id) *
            FROM    messages 
            ORDER BY
                    sender_id, created_at DESC 
            ) q
    ORDER BY
            created_at DESC
    LIMIT 5
    

    or this:

    SELECT  (mi).*
    FROM    (
            SELECT  (
                    SELECT  mi
                    FROM    messages mi
                    WHERE   mi.sender_id = m.sender_id
                    ORDER BY
                            created_at DESC
                    LIMIT 1
                    ) AS mi
            FROM    messages m
            GROUP BY
                    sender_id
            ) q
    ORDER BY
            (mi).created_at  DESC
    LIMIT 5
    

    Create an index on (sender_id, created_at) for this to work fast.

    You may find this article interesting:

    • PostgreSQL: Selecting records holding group-wise maximum
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables which looks something like this Table Queue int ID; string
well i have this messages table with sample values like these: msg_id recipient_id read
I have an Oracle table which contains event log messages for an application. We
I have a WPF listbox which displays messages. It contains an avatar on the
I have a table in SQL Server 2005 which has three columns: id (int),
I have some EDI messages (X12, HL7, etc ...) stored in an Oracle database.
Have you ever seen any of there error messages? -- SQL Server 2000 Could
I have an application that sends messages to an external web service. I build
I have a form showing progress messages as a fairly long process runs. It's
I have a scrolling LED sign that takes messages in either ASCII or (using

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.