请教一段 SQL 语句,用于给会话分组

293 天前
 billgreen1

表格式: customerID, serviceID, sendTime, timelag, contentText

其中 sendTime 是消息发送时间 timelag 指的是当前消息发送时间 减去 上一条消息发送时间, 单位:秒

同一个客服和客户的对话,可能跨多天/多月。 现在想对这些会话进行切分,目前标准是 当 timelag > 600, (十分钟), 认为是一个新会话开始。

现在想新建一列 sessionID, 表示对话的 ID 。

我现在想到的方式是,设置一个变量,初始值为 1, 遇到 timelag >600 时, 该变量自增 1

但我不会写,请教

907 次点击
所在节点    MySQL
2 条回复
aw2350
293 天前
建议用你的需求场景来描述,而不是上来就用你框定死的字段方案
rraz0r0
292 天前
``
SELECT
( CASE customerId WHEN @lastCustomerId THEN @incrSeq := @incrSeq + 1 ELSE @incrSeq := 1 END ) sessionId,
( @lastCustomerId := customerId ) AS customerId,
timelag,
sendTime
FROM
`session`,
( SELECT @incrSeq := 1, @lastCustomerId := 0 ) AS t
WHERE
timelag > 600
ORDER BY
customerId,
sendTime;
``

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/958317

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX