How to select Distinct records from SQL without a primary key

Posted by Satheesh on Stack Overflow See other posts from Stack Overflow or by Satheesh
Published on 2012-06-16T15:13:38Z Indexed on 2012/06/16 15:16 UTC
Read the original article Hit count: 136

Filed under:

I need to show a Notification on user login if there is any unread messages.So if multiple users send(5 messages each) while the user is in offline these messages should be shown on login.Means have to show the last messages from each user. I use joining to find records. In this scenario Message from User is not a primary key.

This is my query

SELECT UserMessageConversations.MessageFrom, UserMessageConversations.MessageFromUserName,UserMessages.MessageTo, UserMessageConversations.IsGroupChat, UserMessageConversations.IsLocationChat, 
    UserMessageConversations.Message, UserMessages.UserGroupID,UserMessages.LocationID FROM    UserMessageConversations LEFT OUTER JOIN
    UserMessages ON UserMessageConversations.UserMessageID = UserMessages.UserMessageID ![enter image description here][1]Where UserMessageConversations.MessageTo=743  AND UserMessageConversations.ReadFlag=0

enter image description here

This is the output obtained from above query. MessageFrom -582 appears twice. I need only one record of this User. How is it possible

© Stack Overflow or respective owner

Related posts about sql-server-2008