SQL Server 2008.在结果中排序和插入自定义行
我有客户和操作员之间的通信表,其中包含电话、ID(1=客户,2=操作员)日期、消息...和一些其他数据。
我需要创建一个存储过程来对这样的表进行排序
number1 operaters message
number1 clients message
number1 clients message
number1 operaters message
number1 *no message*
.
.
.
所以,基本上,我需要找到操作员发送的第一条消息,然后找到下一个操作员消息之前的客户端消息。 如果在一条操作员消息后没有客户答复,我需要插入“没有客户答复”之类的行或类似的内容。
而且桌子很大,如果台阶少一点就好了。
我知道如何按多列排序,我知道如何找到第一个操作员消息(选择最短时间,其中 id...),但我不知道如何在需要的地方插入这些行。
预先感谢您的帮助。
是的,它就是 MSSQL。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有实际测试它,但这个总体想法应该有效:
用简单的英语来说:对于每个没有较年轻客户端消息的操作员消息,插入一个新的客户端消息。
(根据您的评论,
USER_ID = 1
表示客户端和USER_ID = 2
运算符。)另请注意,使用
GETDATE()假设所有其他日期都是正确的(即它们都不是将来的日期),这意味着新插入的消息必须比其“父”操作员消息更年轻。如果不能假设这一点,您可能需要在操作员消息的日期上使用
DATEADD
。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 = 1
signifies a client andUSER_ID = 2
operator.)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 useDATEADD
on operator message's DATE.