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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:44:03+00:00 2026-05-27T20:44:03+00:00

I am trying to select the most recent record between 2 users based on

  • 0

I am trying to select the most recent record between 2 users based on a to_number and a from_number and created date/time.

Once the record is found, display the message and time stamp. As long as either the to_number or from_number have the same pairing, then that is the message I want to display.

I’m really getting stuck on finding unique to/from OR from/to records with the same number combinations AND that haven’t been listed before.

My data:

Messages table:
"id","to_number","from_number","message","created_at","dm_user_id"
"1","7325551212","5705551234","new update","2011-12-17T11:26:33-05:00","1"
"2","5705551234","3015551212","next update","2011-12-17T11:26:53-05:00","1"
"3","6095559876","4695551212","trying messages.","2011-12-19T19:20:47-05:00","2"
"4","5705551234","4155551212","did i get this?","2011-12-19T20:04:40-05:00","1"
"5","9075551212","5705551234","Where did this go?","2011-12-19T20:05:51-05:00","1"
"6","9075551212","5705551234","testing","2011-12-19T20:12:53-05:00","1"
"7","3015551212","5705551234","Are you here ","2011-12-19T20:13:34-05:00","1"
"8","6175554567","4695551212","test from app","2011-12-19T22:51:32-05:00","2"

From the above data, I only want the following records, listed newest to oldest.

NOTE: Not all records will be returned because there are duplicate to/from combinations. For example, id 2 and id 7 are messages between the same 2 numbers. Only the most recent will be returned, id 7.

Another example is id 5 and id 6 – they are both to/from the same numbers so only the most recent is returned, id 6.:

for dm_user_id=1

"3015551212", "Hello",           "2011-12-19T20:13:34-05:00"  # id 7
"9075551212", "testing",         "2011-12-19T20:12:53-05:00"  # id 6
"4155551212", "did i get this?", "2011-12-19T20:04:40-05:00"  # id 4
"7325551212", "new update",      "2011-12-17T11:26:33-05:00"  # id 1

for dm_user_id=2

"6175554567", "test from app",    "2011-12-19T22:51:32-05:00"  # id 8
"6095559876", "trying messages.", "2011-12-19T19:20:47-05:00"  # id 3

I’m trying different combinations of GROUP BY and DISTINCT, but not getting the results I’m looking for.

select * from messages where dm_user_id = 1
group by to_number, from_number

select * from (
select DISTINCT to_number, from_number dm_user_id
from messages) where dm_user_id = 1
  • 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-27T20:44:03+00:00Added an answer on May 27, 2026 at 8:44 pm

    This is a common question, basically you want the most recent message from each number, either TO or FROM, but you don’t want duplicates. You may find something useful in this category greatest-n-per-group

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

Sidebar

Related Questions

I'm trying to select a record with the most effective votes. Each record has
I am trying to select a record out of the table1 by joining table2
I'm trying to select more than 80,000 record in SQL Server in a table
I am trying to allow users to sort mysql queries based on name, price,
I am trying to find the most efficient way to get the most recent
I'm trying to write a query that returns the most recent GPS positions from
I'm trying to select the 5 most viewed articles out of a list of
I'm trying to select the most frequently occurring key words on a table. I
I am trying to write a query that will return only the most recent
Am trying to create a NETEZZA table which has only the most recent records

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.