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
关于connect by可以看
http://www.cnblogs.com/johnnyking39/articles/1155497.html
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)
出处:http://blog.csdn.net/wanglipo/article/details/6556665
Oracle按时间段分组统计的更多相关文章
- Oracle按不同时间分组统计
Oracle按不同时间分组统计 Oracle按不同时间分组统计的sql 如下表table1: 日期(exportDate) 数量(amount) -------------- ----------- ...
- ORACLE的分组统计之ROLLUP(一)
Oracle 9i以后,扩展了group by 的功能,能够满足大部分多维数据的分析统计功能,主要表现: 1. rollup,cube,grouping sets 扩展group by字句提供了丰富的 ...
- Oracle数据库从入门到精通-分组统计查询
视频课程:李兴华 Oracle从入门到精通 视频课程学习者:阳光罗诺 视频来源:51CTO学院 整体内容: 统计函数的使用 分组统计查询的实现 对分组的数据过滤 统计函数 在之前我们就学习过一个COU ...
- MySQL实现按天分组统计,提供完整日期列表,无数据自动补0
业务需求最近要在系统中加个统计功能,要求是按指定日期范围里按天分组统计数据量,并且要能够查看该时间段内每天的数据量. 解决思路直接按数据表日期字段group by统计,发现如果某天没数据,该日期是不出 ...
- Mysql按时间段分组查询
Mysql按时间段分组查询来统计会员的个数,mysql个数 Mysql按时间段分组查询来统计会员的个数,mysql个数 1.使用case when方法(不建议使用)- 代码如下 复制代码SELECT ...
- Oracle和MySQL分组查询GROUP BY
Oracle和MySQL分组查询GROUP BY 真题1.Oracle和MySQL中的分组(GROUP BY)有什么区别? 答案:Oracle对于GROUP BY是严格的,所有要SELECT出来的字段 ...
- mysql自定义时间段分组
说明:一下是自定义7天为一个时间段的分组统计 select datediff(ms_time, '开始时间') div 7 as time , count(*) count,sum(money) mo ...
- SQL Fundamentals: 分组统计查询(FROM-WHERE-GROUPBY-HAVING-SELECT-ORDER BY)
SQL Fundamentals || Oracle SQL语言 统计函数 单字段分组统计(GROUP BY) 多字段分组统计 HAVING子句 控制操作的显示列:基本的SELECT语句 控制行:限定 ...
- 010.简单查询、分组统计查询、多表连接查询(sql实例)
-------------------------------------day3------------ --添加多行数据:------INSERT [INTO] 表名 [(列的列表)] --SEL ...
随机推荐
- PYPI 国内源
搬砖自http://www.cnblogs.com/sunnydou/p/5801760.html 阿里云 http://mirrors.aliyun.com/pypi/simple/ 中国科技大学 ...
- Android 自定义View - 饼图
最近有看到一个自定义等分圆的View,自己尝试做了一个类似的,效果图如下图(1)所示: 图(1) 实现方法:自定义View-ColorCircle,需要的知道的值有圆的半径,等分个数以及扇形颜色. / ...
- Flume环境安装
源码包下载: http://archive.apache.org/dist/flume/1.8.0/ 集群环境: master 192.168.1.99 slave1 192.168.1.100 sl ...
- mongo数据库连接工具类(C#)
Framework版本:.Net Framework 4 using System; using System.Collections.Generic; using System.Linq; usin ...
- 微信小程序中公用内容
微信小程序中各个页面调用公用的js 在util.js文件中 // 跳转哪里 function go(where) { wx.reLaunch({ url: where, }) } // 将方法暴露出去 ...
- RC 522模块在LINUX平台调试笔记
硬件平台: 1 主控:SMDK Exynos4412 POP S5M8767A 2 RFID模块:君盾集团提供的RC522模块 3 通信接口:SPI 软件平台:Android ICS & ke ...
- What's the difference between SDK and Runtime in .NET Core?
What's the difference between SDK and Runtime in .NET Core? Answer1 According to the .Net Core Guide ...
- luogu p1101 单词方阵
https://www.luogu.org/problem/show?pid=1101 很恶心的代码 就是八个方向都搜索 #include<bits/stdc++.h> using na ...
- Java,vue.js,jsp for循环的写法
vue.js <li v-for="student in studentList">{{student.name}}</li> jsp el表达式 < ...
- Codeforces Round #390 (Div. 2) C. Vladik and chat(dp)
http://codeforces.com/contest/754/problem/C C. Vladik and chat time limit per test 2 seconds memory ...