V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
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 · Jul 20, 2023 · 1952 views
    This topic created in 1011 days ago, the information mentioned may be changed or developed.

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

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

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

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

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

    但我不会写,请教

    aw2350
        1
    aw2350  
       Jul 20, 2023
    建议用你的需求场景来描述,而不是上来就用你框定死的字段方案
    rraz0r0
        2
    rraz0r0  
       Jul 20, 2023
    ``
    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;
    ``
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1286 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 23:59 · PVG 07:59 · LAX 16:59 · JFK 19:59
    ♥ Do have faith in what you're doing.