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 用verilog实现tanh函数和softplus函数
  • ¥15 Hadoop集群部署启动Hadoop时碰到问题
  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 QTableWidget重绘程序崩溃
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站