Oracle按天、按月统计数据

-----按天统计
select to_char(t.hiredate, 'yyyy/mm/dd') 日期, count(1) 数量
	from EMP t
 where t.hiredate >= to_date('1980/1/1', 'yyyy/mm/dd')
	 and t.hiredate <= to_date('2017/1/31', 'yyyy/mm/dd')
 group by to_char(t.hiredate, 'yyyy/mm/dd')
 order by 日期;

---按月统计
select to_char(t.hiredate, 'yyyy/mm') 日期, count(1) 数量
	from EMP t
 where t.hiredate >= to_date('1980/1/1', 'yyyy/mm/dd')
	 and t.hiredate <= to_date('2017/12/31', 'yyyy/mm/dd')
 group by to_char(t.hiredate, 'yyyy/mm')
 order by 日期;

-----按年统计
select to_char(t.hiredate, 'yyyy') 日期, count(1) 数量
	from EMP t
 where t.hiredate >= to_date('1980/1/1', 'yyyy/mm/dd')
	 and t.hiredate <= to_date('2017/12/31', 'yyyy/mm/dd')
 group by to_char(t.hiredate, 'yyyy')
 order by 日期;
 
 -----按季度统计
select to_char(t.hiredate, 'q') 日期, count(1) 数量
	from EMP t
 where t.hiredate >= to_date('1980/1/1', 'yyyy/mm/dd')
	 and t.hiredate <= to_date('2017/12/31', 'yyyy/mm/dd')
 group by to_char(t.hiredate, 'q')
 order by 日期;
 
  -----按周统计
select to_char(t.hiredate, 'iw') 日期, count(1) 数量
	from EMP t
 where t.hiredate >= to_date('1980/1/1', 'yyyy/mm/dd')
	 and t.hiredate <= to_date('2017/12/31', 'yyyy/mm/dd')
 group by to_char(t.hiredate, 'iw')
 order by 日期;

Oracle 数据库中按统计数据量,有以下几种常见情况及对应的 SQL 实现: ### 统计指定份每的数据量 若要统计指定份(如 2013 年 5 )每的数据量,可使用如下 SQL 语句: ```sql select trunc(datecol) 日期,sum(数量) 数量和,count(1) 数据量 from tablex where to_char(datecol,'yyyymm') = '201305' group by trunc(datecol); ``` 此 SQL 语句通过 `to_char(datecol,'yyyymm')` 筛选出指定份的数据,再用 `trunc(datecol)` 按分组,最后使用 `sum(数量)` 统计数量总和,`count(1)` 统计数据量 [^1]。 ### 统计一段时间内每的数据量 若要统计一段时间(如 2018 年 7 20 日到 2018 年 8 20 日)内每的数据量,可使用如下 SQL 语句: ```sql select a.time,nvl(b.num,0) as num from ( select to_char(to_date('2018/07/20', 'yyyy-mm-dd') + rownum - 1, 'yyyy-mm-dd') as time from dual connect by rownum <= trunc(to_date('2018/08/20', 'yyyy/mm/dd') - to_date('2018/07/20', 'yyyy/mm/dd')) + 1 ) a left join ( select time,count(time) as num from ( SELECT to_char(INPUT_DATE,'yyyy-mm-dd') as time FROM ORDER_REGISTRATION_ where INPUT_DATE between to_date('2018/07/20', 'yyyy/mm/dd') and to_date('2018/08/20', 'yyyy/mm/dd') + 1 ) group by time order by time ) b on a.time = b.time order by a.time; ``` 该 SQL 语句先通过 `connect by` 生成指定时间段内的所有日期,再使用左连接将这些日期与实际数据进行关联,使用 `nvl` 函数确保无数据的日期数据量显示为 0 [^3]。 ### 统计归档日志每的数据量 若要统计 Oracle 数据库每及每小时产生归档日志的数据量,可使用如下 SQL 语句: ```sql alter session set nls_date_format='yyyy.mm.dd hh24:mi:ss'; select trunc(completion_time) as ARC_DATE, count(*) as COUNT, round((sum(blocks * block_size) / 1024 / 1024), 2) as ARC_MB from v$archived_log group by trunc(completion_time) order by trunc(completion_time); ``` 此 SQL 语句先设置日期格式,再通过 `trunc(completion_time)` 按分组,使用 `count(*)` 统计记录数,`round((sum(blocks * block_size) / 1024 / 1024), 2)` 计算归档日志的大小(MB) [^2]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值