Overall Scenario: I am storing conversations in a table, I need to retrieve the messages for a particular location, such that they're grouped into conversations, and the groups of conversations are sorted by the most recent message received in that group. This is analogous to how text messages are organized on a phone or facebook's newsfeed ordering. I'm storing the messages in the following schema:
Location_id | SentByUser | Customer | Messsage | Time
1 | Yes | 555-123-1234 | Hello world | 2013-12-01 10:00:00
1 | No | 555-123-1234 | Thank you | 2013-12-01 12:00:00
1 | Yes | 999-999-9999 | Winter is coming | 2013-12-03 11:00:20
1 | Yes | 555-123-1234 | Foo Bar | 2013-12-02 11:00:00
1 | No | 999-999-9999 | Thank you | 2013-12-04 13:00:00
1 | Yes | 111-111-1111 | Foo Foo Bar | 2013-12-05 01:00:00
In this case, if I was building the conversation tree for location id, I'd want the following output:
Location_id | SentByUser | Customer | Messsage | Time
1 | Yes | 111-111-1111 | Foo Foo Bar | 2013-12-05 01:00:00
1 | Yes | 999-999-9999 | Winter is coming | 2013-12-03 11:00:20
1 | No | 999-999-9999 | Thank you | 2013-12-04 13:00:00
1 | Yes | 555-123-1234 | Hello world | 2013-12-01 10:00:00
1 | No | 555-123-1234 | Thank you | 2013-12-01 12:00:00
1 | Yes | 555-123-1234 | Foo Bar | 2013-12-02 11:00:00
So what I'd like to do is group all the conversations by the Customer field, and then order the groups by Time, and lastly order the messages within each group also. This is because I'm building out an interface that's similar to text messages. For each location there may be hundreds of conversations, and I'm only going to show a handful at a time. If I ensure that my query output is ordered, I dont have to worry about server maintaining any state. The client can simply say give me the next 100 messages etc.
My question is two fold: 1. Is there a simple way to sub order results? Is there an easy way without doing a complex join back on the table itself or creating a new table to maintain some order. 2. Is the way I'm approaching this a good practice? As in, is there a better way to store and retrieve messages such that the server doesn't have to maintain state? As in, is there a better pattern that I should consider?
I looked at various questions and answers, and the best one I could find was What is the most efficient/elegant way to parse a flat table into a tree?, but it doesnt seem fully applicable to my case because the author is talking about multi branch trees.