need assistance with sql query

in my web application, I have a feature where users can message one another. In their "inbox", I currently have a list of all of their message records displayed in the following format:

FROM            SUBJECT                                             DATE
User A           re: subject for msg1                       10-9-2015
User B           re: subject for msg1                       10-8-2015
User A           re: subject for msg1                       10-7-2015
User B           re: subject for msg1                       10-6-2015
User A           re: subject for msg2                       10-9-2015
User C           re: subject for msg2                       10-7-2015
User A           re: subject for msg2                       10-4-2015
User C           re: subject for msg2                       10-1-2015


Instead of having each message displayed in my table, I want to just have one version of each message, similar to how gMail displays messages. so it would look like:

User B           re: subject for msg1                       10-9-2015
User A           re: subject for msg2                       10-9-2015

the latest version would be what displays. For my table structure, I have a table named tblMessages that contains a messageID column, which is unique to that message, as well as a conversationID column, which is how I group messages between two users together. Right now the following query gives me ALL messages for one user:

select id = messageID, 
         starred = case starred when 1 then 'starred' else 'not starred' end, 
         [from] = displayName, 
         subject = msgSubject,
         [date] = sentDate, 
         beenRead = case beenRead when 1 then '1' else '0' end 
from tblMessages m 
         inner join tblUsers u on m.msgFrom = u.userID and u.status = 1 
where msgTo = 100 and 
        to_deleteDate is null 
order by sentDate DESC

Open in new window


how can I tweak it to return the latest message for each conversation?
LVL 34
Big MontyWeb Ninja at largeAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
perhaps we can try:
Select a.*
from
(
	select id = messageID, 
		 starred = case starred when 1 then 'starred' else 'not starred' end, 
		 [from] = displayName, 
		 subject = msgSubject,
		 [date] = sentDate, 
		 beenRead = case beenRead when 1 then '1' else '0' end  ,

		row_number() over (partition by messageID order by sentDate desc) idx

	from tblMessages m 
		 inner join tblUsers u on m.msgFrom = u.userID and u.status = 1 
	where msgTo = 100 and 
		to_deleteDate is null 
) a
where a.idx = 1
order by [date] DESC

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Big MontyWeb Ninja at largeAuthor Commented:
thx for such a speedy response, much appreciated!

This mostly worked, I only had to change

row_number() over (partition by messageID order by sentDate desc) idx

to

row_number() over (partition by conversationID order by sentDate desc) idx

to get the data I wanted. Thanks again!
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
coool, glad that could make some helps here cheers
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.