有一个表billTable,结构如下:*********************************************************
FID       FNUMBER     FTYPE           FTIME
----------------------------------------------------
888 01012345678 1 2008-04-02 12:09:00
888 01012345678 1 2008-04-02 12:23:00
345 01012345678 1 2008-04-02 12:24:00
888 01012345678 1 2008-04-02 12:44:00
999 01012345678 2 2008-04-02 12:54:00
888 01012345678 1 2008-04-08 13:33:00
678 01012345678 1 2008-04-15 22:28:00
...
*********************************************************
我想统计在时间段内(FTIME)每小时有100条数据的记录按照FID分组显示出来。(这个100是输入的值)思路比较乱,主要的思路就是如果在时间段内每小时超过100条的话就显示出来。下面的SQL语句是错误的:
select t.fid, count(t.fid)cou from billtable t 
where t.ftime<to_date('2008-04-01 01:22:11','yyyy-MM-dd hh24:mi:ss') 
      and t.ftime > to_date('2008-04-15 12:22:11','yyyy-MM-dd hh24:mi:ss')
      group by t.fid

解决方案 »

  1.   

    select t.fid, count(t.fid)cou from billtable t ,
    (
    select a.fid
    from(
    select t.fid,to_char(ftime,'yyyymmdd') days, to_char(ftime,'hh24'),count(t.fid) hour_num
    from billtable t
    where t.ftime <to_date('2008-04-01 01:22:11','yyyy-MM-dd hh24:mi:ss')  
          and t.ftime > to_date('2008-04-15 12:22:11','yyyy-MM-dd hh24:mi:ss') 
          group by t.fid ,to_char(ftime,'yyyymmdd'),to_char(ftime,'hh24')) a
    where a.hour_num>100
    group by a.fid)  b
    where t.ftime <to_date('2008-04-01 01:22:11','yyyy-MM-dd hh24:mi:ss')  
          and t.ftime > to_date('2008-04-15 12:22:11','yyyy-MM-dd hh24:mi:ss') 
          and t.fid=b.fid
          group by t.fid 
      

  2.   

    ********************************************************* 
    FID       FNUMBER  FTYPE      FTIME 
    ---------------------------------------------------- 
    888 01012345678 1 2008-04-02 12:09:00 
    888 01012345678 1 2008-04-02 12:23:00 
    345 01012345678 1 2008-04-02 12:24:00 
    888 01012345678 1 2008-04-02 12:44:00 
    999 01012345678 2 2008-04-02 12:54:00 
    888 01012345678 1 2008-04-02 13:33:00
    678 01012345678 1 2008-04-15 22:28:00 
    ... 
    ********************************************************* 比如我查询时间段为2008-04-02~2008-04-02时发现12点钟这个时间段FID=888每小时超过2条数据(红色部分)。
    就把FID为888的统计记录打印出来。
      

  3.   

    查询时间段为2008-04-02~2008-04-02时
    ----------------------------------
    是不是写错了?2008-04-02 00:00:00~2008-04-02 14:00:00select t.fid, count(*) cou from billtable t 
    where t.ftime <to_date('2008-04-02 14:00:00','yyyy-MM-dd hh24:mi:ss') 
    and t.ftime > to_date('2008-04-02 00:00:00','yyyy-MM-dd hh24:mi:ss')
    group by t.fid 
    having count(*)>2   --这个数字你来写
      

  4.   

    select t.fid, count(*) cou from billtable t  
    where t.ftime  <to_date('2008-04-01 01:22:11','yyyy-MM-dd hh24:mi:ss')  
    and t.ftime > to_date('2008-04-15 12:22:11','yyyy-MM-dd hh24:mi:ss') 
    group by t.fid,trunc(t.ftime,'hh24') 
    having count(*)>2
      

  5.   

    主要是你想只显示FID超过每小时100条的的纪录部分
    还是想显示有超过每小时100条纪录的FID的统计时间段的所有纪录数?如果前一种楼上的用having就行了
    如果是后一种,就需要先找出有超过每小时100条纪录的fid
    然后再关联,把统计时段内的FID的纪录数统计出来
      

  6.   

    我的意思是显示FID在2008-04-01 ~ 2008-04-01时间段内,每小时超过100条的FID显示出来。
    比如在这个时间段内12点钟FID=888有200条,但是在1点钟FID=888有99条就不需要显示,我的显示格式为:
    *************
    FID   COUNT
    888    200
    *************

    但是在这个时间段内FID=888有多个小时点都超过100条(比如1点钟有120条,3点钟有200条...),
    我应该如何处理?
      

  7.   

    你的时间不对。2008-04-01 ~ 2008-04-01 怎么会是时间段???  前后日期是一样的你还要按照整点来分组,这个可以做到     4楼的就可以。
    这个还有问题,如果时间不是1天的话,你分组只按小时,还是天和小时来分组?但是在这个时间段内FID=888有多个小时点都超过100条(比如1点钟有120条,3点钟有200条...), ?
    你问我们怎么处理?你想要什么样的统计结果怎么处理
      

  8.   

    lz说得有点含糊,不过意思有点明白了
    楼主首先在以小时为单位的频度大于指定值,并且符合这些条件的再以分组显示,
    其中就是;
    group by to_char(ftime,'yyyymmddhh') 进行分组并且
    having count(*)>100;
    有组,再显示这些数据,按小组分列,
    只是这样除非做了函数索引,否则性能上是个问题,
    select fid, fnumber, ftype, ftime
    where to_char(ftime, 'yyyymmddhh') 
    in 
    ( select to_char(ftime,'yyyymddhh') from billtable
    group by to_char(ftime,'yyyymddhh') having count(*)>100 )
    order by fid;
    基本上应该符合LZ的要求了