dtj2ww9500 2013-12-12 15:43
浏览 116
已采纳

MySQL在允许的限制内搜索并发时间戳,然后计算SUM?

Been racking my brain for weeks on how I could correctly achieve this, been playing around with PHP & mySQL but every formula or function I write seems to be missing something. Here's exactly what I'm trying to do..

-mySQL database filled with records of logins.

-Fields are RECORD,START_TIME,DURATION,CUSTOMERID

-RECORD is unique key

-START_TIME is in the "2013-12-12 10:32:59" type format

-DURATION is in seconds

-CUSTOMERID is the unique account number of user

Each user is allowed 3 simultaneous logins, any logins above 3 at a time I need to tally up a total of seconds of DURATION for that user of all sessions during the "overage".

So an example would be..

RECORD  |  START_TIME        |  DURATION  |  CUSTOMERID
   1       2013-1-1 12:00:00      10          BILLYBOB
   2       2013-1-1 12:01:00      600         BILLYBOB
   3       2013-1-1 12:04:00      1200        BILLYBOB
   4       2013-1-1 12:05:20      500         BILLYBOB
   5       2013-1-1 12:06:30      600         BILLYBOB
   6       2013-1-1 16:00:00      100         BILLYBOB
   7       2013-1-1 18:00:00      300         BILLYBOB

In this case, it would return records 2,3,4,5 since there was over 3 simultaneous sessions, then return the total duration of those records which would be 2900.

Is this asking too much of MySQL?

  • 写回答

2条回答 默认 最新

  • dongyuan2388 2013-12-13 11:22
    关注

    Okay, I got it now...

    First you must find the time slices per user. Say he/she worked from 10:00:00 to 10:09:59 and from 10:10:00 to 10:19:59 and from 10:00:00 to 10:29:59. That gives you three time slices: 10:00:00 to 10:09:59 (sessions 1 and 3), 10:10:00 to 10:19:59 (session 2 and 3) and 10:20:00 to 10:29:59 (session 3).

    Then you count the sessions that were active in each time slice and remove slices were only three or less sessions were active. With those violated time slices found, you select the sessions that were active then.

    Here is the complete statement:

    select *
    from sessions
    where exists
    (
      select *
      from
      (
        select start_times.customerid, start_times.start_time, min(end_times.end_time) as end_time
        from
        (
          select customerid, start_time from sessions
          union
          select customerid, date_add(start_time, interval duration second) from sessions
        ) start_times
        join
        (
          select customerid, date_add(start_time, interval duration - 1 second) as end_time from sessions
          union
          select customerid, date_add(start_time, interval -1 second) from sessions
        ) end_times
        on (start_times.customerid = end_times.customerid and start_times.start_time < end_times.end_time)
        group by start_times.customerid, start_times.start_time
      ) time_slices
      where 
      (
        select count(*) 
        from sessions
        where time_slices.customerid = sessions.customerid
        and
        (
          time_slices.start_time between sessions.start_time and date_add(sessions.start_time, interval duration - 1 second)
          and
          time_slices.end_time between sessions.start_time and date_add(sessions.start_time, interval duration - 1 second)
        )
      ) > 3
      and time_slices.customerid = sessions.customerid
      and
      (
        time_slices.start_time between sessions.start_time and date_add(sessions.start_time, interval duration - 1 second)
        and
        time_slices.end_time between sessions.start_time and date_add(sessions.start_time, interval duration - 1 second)
      )
    )
    ;
    

    And here is the SQL fiddle: http://sqlfiddle.com/#!2/12c47/1 .

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码