V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
MySQL 5.5 Community Server
MySQL 5.6 Community Server
Percona Configuration Wizard
XtraBackup 搭建主从复制
Great Sites on MySQL
Percona
MySQL Performance Blog
Severalnines
推荐管理工具
Sequel Pro
phpMyAdmin
推荐书目
MySQL Cookbook
MySQL 相关项目
MariaDB
Drizzle
参考文档
http://mysql-python.sourceforge.net/MySQLdb.html
billgreen1
V2EX  ›  MySQL

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

  •  
  •   billgreen1 · 281 天前 · 882 次点击
    这是一个创建于 281 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

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

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

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

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

    但我不会写,请教

    aw2350
        1
    aw2350  
       281 天前
    建议用你的需求场景来描述,而不是上来就用你框定死的字段方案
    rraz0r0
        2
    rraz0r0  
       281 天前
    ``
    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;
    ``
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2816 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 05:57 · PVG 13:57 · LAX 22:57 · JFK 01:57
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.