I would like to query back a list of threads, with each of the conversation showing the latest message max(date)
, and a count of how many messages in the conversation count(*), group by threadID
.
Here's the SQL table
Message
-------------
messageID (int)
subject (varchar)
...
date (date)
threadID (guid)
How do I produce the following in one single query? in a thread view like in Gmail?
Or is the data structure wrong? Maybe I should denormalize the required data to a Thread
table? Make use of trigger to update the last message ID and count?
Thanks!
update I also need the subject of the 'max([date])', and order the thread by the date of the last message, thank you!