一、 最简单的统计
select count(1)
from table_name t
where t.creation_time >=
to_date('2021-11-27 00:00:00', 'yyyy-mm-dd hh24:mi:ss')
and t.creation_time <=
to_date('2021-11-27 00:59:59', 'yyyy-mm-dd hh24:mi:ss');
二、 查询某个时间段内每天的数据量
select trunc(t.creation_time) date1,
--sum(num_col) num,
count(*) totalCount
from table_name t
where t.creation_time >= to_date('2021-11-01 00:00:00', 'yyyy-mm-dd hh24:mi:ss')
and t.creation_time <= to_date('2021-11-11 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
group by trunc(t.creation_time);
三、 查询某一天每个小时内的数据量
select trunc(t.creation_time,'hh') date1,
--sum(num_col) num,
count(*) totalCount
from table_name t
where t.creation_time >= to_date('2021-11-27 00:00:00', 'yyyy-mm-dd hh24:mi:ss')
and t.creation_time <= to_date('2021-11-27 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
group by trunc(t.creation_time,'hh');
四、 Oracle 函数trunc
TRUNC(number,num_digits)
number 需要截尾取整的数字。
num_digits 用于指定取整精度的数字。Num_digits 的默认值为 0。
针对日期的案例,如:
select trunc(sysdate) from dual --2017/6/13 返回当天的日期
select trunc(sysdate,'yyyy') from dual --2017/1/1 返回当年第一天.
select trunc(sysdate,'mm') from dual --2017/6/1 返回当月第一天.
select trunc(sysdate,'d') from dual --2017/6/11 返回当前星期的第一天(以周日为第一天).
select trunc(sysdate,'dd') from dual --2017/6/13 返回当前年月日
select trunc(sysdate,'hh') from dual --2017/6/13 13:00:00 返回当前小时
select trunc(sysdate,'mi') from dual --2017/6/13 13:06:00 返回当前分钟
五 、mysql查询
SELECT count(1) FROM table_name WHERE crt_time between '2022-05-31 20:00:00' and '2022-05-31 23:59:59';
参考: