select count(*),to_date(t.sent_bg_time,'yyyy-mm-dd hh24') as 时间段
from table_history_201407 t 
where t.sent_bg_time between to_date('20140723','yyyymmdd') and to_date('20140723','yyyymmdd')+0.99999 
group by to_date(t.sent_bg_time,'yyyy-mm-dd hh24')

解决方案 »

  1.   

    select count(*),to_char(t.sent_bg_time,'hh24') as 时间段
    from table_history_201407 t
    where trunc(t.sent_bg_time)=to_date('20140723','yyyymmdd')
    group by to_char(t.sent_bg_time,'hh24')
      

  2.   


    谢谢  但有提示文字与格式字符串不匹配              sent_bg_time字段格式2014/7/23 00:00:00
      

  3.   

    1楼语句有问题
    sent_bg_time不是date类型的吗?再对他to_date没有什么意义
    这么写应该是把sent_bg_time当文本类型处理了
      

  4.   

    执行了你的语句没有问题 但是我不是很清楚to_date和to_char;      为什么字段sent_bg_time要to_char转成字符串不能直接用本身date类型进行查找或分组吗 to_char不是多了转换的步骤耽搁了时间吗
      

  5.   

    date类型本身含小时分秒的,
    如12:10 和12:11分组的时候会分组成两条,达不到统计的效果
    to_char(sent_bg_time,‘HH24’)之后,两个时间 就都转化为‘12’,分组统计就统计到一起了
      

  6.   

    嗯,写错了,应该是to_char.你也可以用trunc(sent_bg_time,'HH24')来代替to_char,但是如果你还需要在页面上打印结果的话,那还得转换一次
      

  7.   

    select count(*),to_char(t.sent_bg_time,'hh24')||'-'||lpad(to_number(to_char(t.sent_bg_time,'hh24'))+1,2,'0') as 时间段
    from table_history_201407 t
    where trunc(t.sent_bg_time)=to_date('20140723','yyyymmdd')
    group by to_char(t.sent_bg_time,'hh24')