日期:2014-05-17  浏览次数:20752 次

按时间分段查询
坐等  求指导
数据源

id      name        end_time
--------------------------------
1      张三            2014-1-07 08:53:11
2      李四            2014-1-07 08:58:22
3      张三            2014-1-07 09:13:11
4      张三            2014-1-07 09:31:11
5      张三            2014-1-07 10:53:11
...


查出结果为

张三 09:00前  有  1条 记录
     9点到10点有  2条 记录
     10点后   有  1条  记录

------解决方案--------------------
这不是典型的count \ group by语法嘛。。。
select 姓名,函数(时间),count(1)
from 表名
group by 姓名,函数(时间)


其中,函数(时间)主要是对时间进行归类转换,需要你自己去解决,举个例子:
        函数(2014-1-07 08:53:11) = 2013-1-07 09:00点前
具体是怎么实现转换的就上百度查API吧。
------解决方案--------------------
select  t.name, ( case when to_char(t.end_time,'hh24')<9 then '9点前'
                        when to_char(t.end_time,'hh24')>= 9 and to_char(t.end_time,'hh24')<10 then ‘10点前’
else '10点后'
   end),count(*)
from tab t
group by t.name, ( case when to_char(t.end_time,'hh24')<9 then '9点前'
                        when to_char(t.end_time,'hh24')>= 9 and to_char(t.end_time,'hh24')<10 then ‘10点前’
else '10点后'
   end)