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

The Archive Base Latest Questions

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

need a little help with some sql. I am trying to get distinct frompersonid,

  • 0

need a little help with some sql. I am trying to get distinct frompersonid, but i am wanting to still select all the columns. I am also wanting to end up ordering this by MessageDateTime.

SELECT  [MessageID]
  ,[ToPersonID]
  ,[FromPersonID]
  ,[MessageText]
  ,[MessageDateTime]
  ,[ViewedByPerson]
FROM dbo.Message

EDIT:

Let me try to clarify this a little better. say i have a table with these values.

messageid   toPerson   FromPerson   text
1           Andy       John         kdfjskjf
2           Andy       John         kdfjsdkj
3           Andy       Bob          kjfksdj

I am wanting to just display last message in the conversation between Andy and John so my results should be

messageid   toPerson   FromPerson
2           Andy       John
3           Andy       Bob
  • 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-27T13:10:04+00:00Added an answer on May 27, 2026 at 1:10 pm

    You’re obviously using MS SQL Server (or conceivably Sybase). You really don’t need the square brackets; at least to my eyes, they severely limit the readability of the SQL.

    Which values do you want to see for a given ‘FromPersonID’?

    You can do it this way:

    SELECT MAX(MessageID)       AS MessageID,
           MIN(ToPersonID)      AS ToPersonID,
           FromPersonID         AS FromPersonID,
           MIN(MessageText)     AS MessageText,
           MAX(MessageDateTime) AS MessageDateTime,
           MIN(ViewedByPerson)  AS ViewedByPerson
      FROM politicprofiles.dbo.Message
     GROUP BY FromPersonID
     ORDER BY MessageDateTime
    

    This somewhat arbitrarily picks one value to associate with the given unique ‘FromPersonID’; in general, the values will not come from a single row of data, but you didn’t specify that you wanted some representative row of data – just that you wanted some value from each of the columns.

    If you have some criterion in mind for which data should be shown for the cases where the FromPersonID has more than one record, then there are other ways to code the query. However, you may well end up doing a sub-query to identify one message for each FromPersonID (perhaps based on most recent MessageDateTime) and then selecting the row(s) from the main data table that match the FromPersonID and the most recent MessageDateTime for that PersonID:

    SELECT M.MessageID,
           M.ToPersonID,
           M.FromPersonID,
           M.MessageText,
           M.MessageDateTime,
           M.ViewedByPerson
      FROM politicprofiles.dbo.Message AS M
      JOIN (SELECT FromPersonID, MAX(MessageDateTime) AS MessageDateTime
              FROM politicprofiles.dbo.Message
             GROUP BY FromPersonID) AS R
        ON M.FromPersonID = R.FromPersonID AND M.MessageDateTime = R.MessageDateTime
     ORDER BY M.MessageDateTime, FromPersonID
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need some help with overlaying views using the prism framework.Its a little more
I am getting a little confused and need some help please. Take these two
Need a little help with a SQL / ActiveRecord query. Let's say I have
I need a little help creating a catch-all error handling page in my ICEfaces
I need a little help here: I get a file from an HTML upload
Hi I need a little help: How to get profile URL given an UID?
I need a little help converting some VB.NET code to C#. I have tried
i need a little bit of help with some jquery thing that i'm sure
I'm so close on this one, but I need a little help. I have
need a little help with the regexp i make... have some text where links

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.