一个表有3个字段
userid   start_time            end_time
1      2011-08-05 03:00:00   2011-08-05 05:11:00
1      2011-08-05 13:00:00   2011-08-05 15:11:00
2      2011-08-05 03:00:00   2011-08-05 04:11:00
3      2011-08-05 04:00:00   2011-08-05 04:11:00
等等我想知道 一天24小时内 每15分钟间隔 的在线人数统计  (比如00:15,00:30,00:45这种间隔)
实在找不到 比较效率的解决方案了

解决方案 »

  1.   

    select  ceiling(sec_to_time(start_time)/900),count(*)
    from tb
    gropu by ceiling(sec_to_time(start_time)/900)
      

  2.   

     (不要高估你的汉语表达能力或者我的汉语理解能力)
       建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
       参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html
       
       1. 你的 create table xxx .. 语句
       2. 你的 insert into xxx ... 语句
       3. 结果是什么样,(并给以简单的算法描述)
       4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
       
       这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。   
      

  3.   

    生成24小时分成15分钟的临时表(btime),
    比如
    ATIME
    00:15
    00:30
    00:45

    24:00与工作表连接
    select count(*) from tt a left join btime b on b.atime between a.start_time and a.end_time