I have table for comunication between clients and operaters, which contains phone, id (1=client, 2=operater) date, message… and some other data.
I need to make stored procedure which will sort table like this
number1 operaters message
number1 clients message
number1 clients message
number1 operaters message
number1 *no message*
.
.
.
So, bassically, I need to find first message sent by operater and then client messages before next operaters message.
If after one operaters message there is no clients answer, I need to insert row like “no clients anwer” or something like that.
And that is large table, so will be good if it is less steps.
I know how to order by multiple columns, I know how to find first operaters message (select min time where id…) but I don’t know how to insert those rows where needed.
Thanks in advance for help.
And yeah, it’s MSSQL.
Didn’t actually test it, but this general idea should work:
In plain English: for each operator message that does not have a younger client message, insert a new client message.
(Based on your comment,
USER_ID = 1signifies a client andUSER_ID = 2operator.)Also note that using
GETDATE()assumes all other dates are correct (i.e. none of them are in the future), implying that newly inserted message must be younger than its “parent” operator message. If that cannot be assumed, you’ll probably need to useDATEADDon operator message’s DATE.