Oracle数据库按时间进行分组统计数据的方法
作者:jack_Meng 发布时间:2023-07-14 13:52:56
标签:Oracle,统计
Oracle按不同时间分组统计的sql
如下表table1:
日期(exportDate) 数量(amount)
-------------- -----------
14-2月 -08 20
10-3月 -08 2
14-4月 -08 6
14-6月 -08 75
24-10月-09 23
14-11月-09 45
04-8月 -10 5
04-9月 -10 44
04-10月-10 88
注意:为了显示更直观,如下查询已皆按相应分组排序
1.按年份分组
select to_char(exportDate,'yyyy'),sum(amount) from table1 group by to_char(exportDate,'yyyy');
年份 数量
-----------------------------
2009 68
2010 137
2008 103
2.按月份分组
select to_char(exportDate,'yyyy-mm'),sum(amount) from table1 group by to_char(exportDate,'yyyy-mm')
order by to_char(exportDate,'yyyy-mm');
月份 数量
-----------------------------
2008-02 20
2008-03 2
2008-04 6
2008-06 75
2009-10 23
2009-11 45
2010-08 5
2010-09 44
2010-10 88
3.按季度分组
select to_char(exportDate,'yyyy-Q'),sum(amount) from table1 group by to_char(exportDate,'yyyy-Q')
order by to_char(exportDate,'yyyy-Q');
季度 数量
------------------------------
2008-1 22
2008-2 81
2009-4 68
2010-3 49
2010-4 88
4.按周分组
select to_char(exportDate,'yyyy-IW'),sum(amount) from table1 group by to_char(exportDate,'yyyy-IW')
order by to_char(exportDate,'yyyy-IW');
周 数量
------------------------------
2008-07 20
2008-11 2
2008-16 6
2008-24 75
2009-43 23
2009-46 45
2010-31 5
2010-35 44
2010-40 88
PS:Oracle按时间段分组统计
想要按时间段分组查询,首先要了解level,connect by,oracle时间的加减.
关于level这里不多说,我只写出一个查询语句:
----level 是一个伪例
select level from dual connect by level <=10
---结果:
1
2
3
4
5
6
7
8
9
10
oracle时间的加减看看试一下以下sql语句就会知道:
select sysdate -1 from dual
----结果减一天,也就24小时
select sysdate-(1/2) from dual
-----结果减去半天,也就12小时
select sysdate-(1/24) from dual
-----结果减去1 小时
select sysdate-((1/24)/12) from dual
----结果减去5分钟
select sydate-(level-1) from dual connect by level<=10
---结果是10间隔1天的时间
下面是本次例子:
select dt, count(satisfy_degree) as num from T_DEMO i ,
(select sysdate - (level-1) * 2 dt
from dual connect by level <= 10) d
where i.satisfy_degree='satisfy_1' and
i.insert_time<dt and i.insert_time> d.dt-2
group by d.dt
例子中的sysdate - (level-1) * 2得到的是一个间隔是2天的时间
group by d.dt 也就是两天的时间间隔分组查询
自己实现例子:
create table A_HY_LOCATE1
(
MOBILE_NO VARCHAR2(32),
LOCATE_TYPE NUMBER(4),
AREA_NO VARCHAR2(32),
CREATED_TIME DATE,
AREA_NAME VARCHAR2(512),
);
select (sysdate-13)-(level-1)/4 from dual connect by level<=34 --从第一条时间记录开始(sysdate-13)为表中的最早的日期,“34”出现的分组数(一天按每六个小时分组 就应该为4)
一下是按照每6个小时分组
select mobile_no,area_name,max(created_time ),dt, count(*) as num from a_hy_locate1 i ,
(select (sysdate-13)-(level-1)/4 dt
from dual connect by level <= 34) d
where i.locate_type = 1 and
i.created_time<dt and i.created_time> d.dt-1/4
group by mobile_no,area_name,d.dt
另外一个方法:
--按六小时分组
select trunc(to_number(to_char(created_time, 'hh24')) / 6),count(*)
from t_test
where created_time > trunc(sysdate - 40)
group by trunc(to_number(to_char(created_time, 'hh24')) / 6)
--按12小时分组
select trunc(to_number(to_char(created_time, 'hh24')) / 6),count(*)
from t_test
where created_time > trunc(sysdate - 40)
group by trunc(to_number(to_char(created_time, 'hh24')) / 6)


猜你喜欢
- 记录一次小白的tensorflow学习过程,也为有同样困扰的小白留下点经验。先说我出错和解决的过程。在做风格迁移实验时,使用预加载权重的VG
- Python pip安装lxml出错的问题解决办法1. 在使用pip安装lxml过程中出现了一下错误: &
- 前言最近公司服务器到期,需要进行数据迁移,而数据库属于多而繁琐,通过图形化界面一个一个备份所需时间成本很大,所以想着写一个sql脚本来执行。
- 在ubuntu下面发生的原因是:开了多个pycharm,关掉那个new project选项是灰色的,剩下的那个pycharm的new pro
- 本文实例讲述了PHP利用func_get_args和func_num_args函数实现函数重载的方法。分享给大家供大家参考。具体方法分析如下
- 如下所示:filename=None if request.method == 'POST
- 上次在blueidea上看到一个元素圆角的实现方法,但是那个太复杂了。于是就自己写了一个函数,可以将元素自动圆角,如div层,表格等。共有四
- 在 JavaScript 中对象和数组是引用类型,指向同一个内存空间,如果 prop 是一个对象或数组,在子组件内部改变它会影响父组件的状态
- 最近刚好有朋友遇到个global相关的问题,这里简单学习一下global关键字的用法。想要更好的了解global关键字,首先要熟悉pytho
- 1.sort.Sort介绍使用sort.Slice进行排序,因为slice把struct抽象化了,且slice封装过了,简单的基础类型可以使
- 1. 训练运行时候指定GPU运行时候加一行代码:CUDA_VISIBLE_DEVICES=1 python train.py2. 运行过程中
- 本文记录了PyCharm安装的图文教程,供大家参考,具体内容如下PyCharm的官网 1.在官网下载安装包2.选择Windows系
- 抽象工厂模式(Abstact Factory)是一种常见的软件设计模式。该模式为一个产品族提供了统一的创建接口。当需要这个产品族的某一系列的
- 实际项目中会涉及到需要对有些函数的响应时间做一些限制,如果超时就退出函数的执行,停止等待。可以利用python中的装饰器实现对函数执行时间的
- 概述在pytorch中有两种方式可以保存推理模型,第一种是只保存模型的参数,比如parameters和buffers;另外一种是保存整个模型
- 队列queue 多应用在多线程应用中,多线程访问共享变量。对于多线程而言,访问共享变量时,队列queue是线程安全的。从queue队列的具体
- 一、前言前几天在Python最强王者群有个叫【dcpeng】的粉丝问了一个关于Pandas中的问题,这里拿出来给大家分享下,一起学习。想问一
- 本次目标是将一副图像从rgb颜色空间转换到hsv颜色空间,颜色去除白色背景部分具体就调用了cv2的两个函数,一个是rgb转hsv的函数具体用
- html<ul class="header-list"> <li v-cl
- 一、前言程序访问MySQL数据库时,当查询出来的数据量特别大时,数据库驱动把加载到的数据全部加载到内存里,就有可能会导致内存溢出(OOM)。