oracle 统计SQL

一、 最简单的统计

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';

参考:

oracle之TRUNC函数 - F风 - 博客园

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值