各位大虾好 小弟想请教一句SQL语句
就是我有一张表 表里面有两个字段
ID,TIME
time字段的格式是 2008/05/22 11:54:25
我想得到当天0点 到早上9点以前 这个时间段内一共有多少条记录的总数
然后9点以后的 都分开统计每小时的记录的总数也就是想得到一个这样的时间 总数
8(这里显示的8是0点-8点的总数) 100
9 50
10 53
11 40
12 105
13 100
14 110
.......就是一个这样的结果这个SQL 应该怎样写
谢谢
就是我有一张表 表里面有两个字段
ID,TIME
time字段的格式是 2008/05/22 11:54:25
我想得到当天0点 到早上9点以前 这个时间段内一共有多少条记录的总数
然后9点以后的 都分开统计每小时的记录的总数也就是想得到一个这样的时间 总数
8(这里显示的8是0点-8点的总数) 100
9 50
10 53
11 40
12 105
13 100
14 110
.......就是一个这样的结果这个SQL 应该怎样写
谢谢
WHERE TO_CHAR(TIME,'YYYY/MM/DD HH24:MI:SS') BETEEN '2008/05/22 00:00:00' AND '2008/05/22 08:59:59'
union all
SELECT COUNT(*) "8点以后" FROM TABLENAME
WHERE TO_CHAR(TIME,'YYYY/MM/DD HH24:MI:SS') >='2008/05/22 09:00:00'
where to_char(time,'yyyymmdd')=to_char(sysdate,'yyyymmdd)
group by decode(trunc(to_char(time,'hh24')/8),0,8,to_char(time,'hh24'))
group by to_char(times,'hh24')--结果
时间 总数
01 1
02 2
03 1
05 1
06 1
09 1
10 2
11 1
12 2
select '09',count(*) as 总数 from table where to_char(time,'hh24')>'00' and to_char(time,'hh24')<='08' group by '09'
union all
select to_char(TIME,'hh24') as 时间,count(*) as 总数 from table where to_char(time,'hh24')>'08' group by to_char(TIME,'hh24')
union all
select to_char(TIME,'hh24') as 时间,count(*) as 总数 from table where to_char(time,'hh24')>'08' group by to_char(TIME,'hh24')
and to_char(time,'hh24')<='08' and to_char(time,'yyyy-mm-dd')='2008-05-22' group by '09'
union all
select to_char(TIME,'hh24') as 时间,count(*) as 总数 from table
where to_char(time,'hh24')>'08' and to_char(time,'yyyy-mm-dd')='2008-05-22'
group by to_char(TIME,'hh24')
UNION SELECT COUNT(*),TO_CHAR(TIME,'HH24') TIM FROM TABLENAME WHERE TO_CHAR(TIME,'HH24') BETWEEN '09' AND '24' GROUP BY TO_CHAR(TIME,'HH24')
(select decode(a.a-1,8,0,a.a-1) b,a.a c from
(select rownum a from dual connect by rownum<=24) a where a.a>8) x
where to_char(time,'hh24')>=x.b and to_char(time,'hh24')<x.c and
to_char(time,'yyyy-mm-dd')=to_char(sysdate,'yyyy-mm-dd')
试试这个吧!公司的机子都不让装,所以没测试。