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条)

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值