sql - How to group all conversations in a database by date, conversation thread -
i've got sqllite database has log of received , sent messages.
id sender msg timestamp 1 2 9 1-1 201 2 9 2 1-2 202 3 1 9 2-1 203 4 9 2 1-3 204 5 9 1 2-2 205
i can order them timestamp, when conversations overlap example above (between 9, 2, , 1) kills readability.
how show them conversation?
ie:
id sender msg timestamp 1 2 9 1-1 201 2 9 2 1-2 202 4 9 2 1-3 204 3 1 9 2-1 203 5 9 1 2-2 205
in database conversations between 9 , number, example 2 , 1 never have conversation.
i've input data sqlfiddle here
to conversations in order started, you'll need self join table minimum timestamp per conversation, , order minimum id conversations in order , secondarily timestamp of message messages in order.
select a.id, a.sender, a."to", a.msg, a.timestamp table1 join table1 b on case when a.sender=9 a."to" else a.sender end = case when b.sender=9 b."to" else b.sender end group a.id, a.sender, a."to", a.msg, a.timestamp order min(b.timestamp), a.timestamp
Comments
Post a Comment