有一个表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
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
(
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
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的统计记录打印出来。
----------------------------------
是不是写错了?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 --这个数字你来写
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
还是想显示有超过每小时100条纪录的FID的统计时间段的所有纪录数?如果前一种楼上的用having就行了
如果是后一种,就需要先找出有超过每小时100条纪录的fid
然后再关联,把统计时段内的FID的纪录数统计出来
比如在这个时间段内12点钟FID=888有200条,但是在1点钟FID=888有99条就不需要显示,我的显示格式为:
*************
FID COUNT
888 200
*************
但是在这个时间段内FID=888有多个小时点都超过100条(比如1点钟有120条,3点钟有200条...),
我应该如何处理?
这个还有问题,如果时间不是1天的话,你分组只按小时,还是天和小时来分组?但是在这个时间段内FID=888有多个小时点都超过100条(比如1点钟有120条,3点钟有200条...), ?
你问我们怎么处理?你想要什么样的统计结果怎么处理
楼主首先在以小时为单位的频度大于指定值,并且符合这些条件的再以分组显示,
其中就是;
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的要求了