oracle排序使用,很多中函数,不同的效果
参考原文:http://blog.csdn.net/wanglipo/article/details/6954915
row_number() OVER (PARTITION BY COL1 ORDER BY COL2) 表示根据COL1分组,在分组内部根据 COL2排序,而此函数计算的值就表示每组内部排序后的顺序编号(组内连续的唯一的).
与rownum的区别在于:使用rownum进行排序的时候是先对结果集加入伪列rownum然后再进行排序,而此函数在包含排序从句后是先排序再计算行号码. row_number()和rownum差不多,功能更强一点(可以在各个分组内从1开时排序).
rank()是跳跃排序,有两个第二名时接下来就是第四名(同样是在各个分组内).
dense_rank()l是连续排序,有两个第二名时仍然跟着第三名。相比之下row_number是没有重复值的 .
lag(arg1,arg2,arg3): arg1是从其他行返回的表达式 arg2是希望检索的当前行分区的偏移量。是一个正的偏移量,时一个往回检索以前的行的数目。 arg3是在arg2表示的数目超出了分组的范围时返回的值。
create table a ( id number(8) not null, val number(8) ) /
insert into a(id,val) values(1,5);
insert into a(id,val) values(2,8);
insert into a(id,val) values(3,8);
insert into a(id,val) values(5,8);
insert into a(id,val) values(9,6);
insert into a(id,val) values(11,6);
insert into a(id,val) values(22,8);
insert into a(id,val) values(40,8);
insert into a(id,val) values(41,5);
commit;
select * from a order by id;
--[查询语句]--
select val,count(*) from ( select id,val,row_number() over (order by id) - row_number() over (partition by val order by id) x from a ) group by val,x order by min(id);
--[解析]--
select id,val,row_number() over (order by id) x from a; //按id进行排序
select id,val,row_number() over (partition by val order by id) x from a; //按val分组,分组内按id排序
select id,val,row_number() over (order by id) - row_number() over (partition by val order by id) x from a;//id排序值 减去 val分组内id排序值 = 连续相同值的排序值
原理:因为dense_rank() 和rownum都是连续的计数的,一个是全局计数,一个是局部分组计数,因此,两个递增频率都是1的连续相减,值应该是一样的,
比如 全局为 1,2,3,4,5
分组为 1,2 ;1;1,2 结果 1-1=0,2-2=0; 3-1=2; 4-1=3,5-2=3; 因此 1,2 ;4,5是连续的
--统计一个手机连续在一个地区停留时间 created_time 定位时间 area_no 地区
create table T_test
(
MOBILE_NO VARCHAR2(16),
AREA_NO VARCHAR2(10),
CREATED_TIME DATE not null
);
写法一:
select mobile_no, area_no, trunc(max_date - min_date), min_date, max_date
from (select distinct mobile_no,
area_no,
min(created_time) over(partition by mobile_no, area_no, gn) min_date,
max(created_time) over(partition by mobile_no, area_no, gn) max_date
from (select rownum - dense_rank() over(partition by mobile_no, area_no order by created_time) gn,
mobile_no,
area_no,
created_time
from (select a.mobile_no, a.area_no, a.created_time
from t_test a
order by mobile_no, created_time)));
写法二:
select mobile_no,
area_no,
count(*),
min(created_time),
max(created_time),
max(created_time) - min(created_time)
from (select a.mobile_no,
a.area_no,
a.created_time,
row_number() over(order by created_time) - row_number() over(partition by area_no order by created_time) x
from t_test a)
group by mobile_no, area_no, x
order by min(created_time)
我自己遇到的使用场景:
需求:查询5月15日之前每一天分数最高的所有信息,但是某一天分数最高的信息可能有多条。
以下为我的sql,可以按时间倒序排列,并且给每天分数相同的序号是一致的。
select dense_rank() over (partition by cake.gamble_date order by cake.score desc) num,cake.*
from t_cake cake
where cake.cake_date<'5月15日'
order by cake.cake_date desc;
oracle排序使用,很多中函数,不同的效果相关推荐
- oracle迁移mysql视图中函数问题,mysql中to_char自定义函数。
迁移到mysql中会遇到函数迁移问题,我们可以自定义函数 DELIMITER $$ CREATE FUNCTION new_data_report.to_char(Intnumber int) RET ...
- oracle 排序_Oracle数据库中SQL之过滤和排序数据
1 过滤 使用WHERE子句,将不满足条件的行过滤掉.WHERE子句紧随FROM子句 SELECT *|{[DISTINCT]column|expression[alias]} FROM table ...
- oracle中函数怎么使用方法,Oracle中函数的使用
1.decode () 例子:它的写法如下decode('a','b','c','d'),其中a,b,c,d可以是其他函数也可以是数值,依据我们自己的情况来使用,它的含义是如果a=b,那么结果显示c, ...
- Oracle 10g数据库基础之基本查询语句-中-函数
Oracle 10g数据库基础之基本查询语句-中-函数 --资料参考:张烈 张建中<数据库管理员培训讲义> 函数: 使用函数的目的是为了操作数据 将输入的变量处理,返回一个结果. 变量可以 ...
- 编写一个C程序,实现以下功能:定义一个学生结构体Student(含学号、姓名、年龄、身高)和一个函数sort(struct Student *p),该函数使用选择排序法按年龄由小到大排序。在主函数中
编写一个C程序,实现以下功能: 定义一个学生结构体Student(含学号.姓名.年龄.身高)和一个函数sort(struct Student *p),该函数使用选择排序法按年龄由小到大排序.在主函数中 ...
- python中sorted函数的作用_Python中排序方法sort、函数sorted的key参数的作用分析
从Python2.4开始,list.sort方法 和 sorted方法 都增加了一个 'key' 参数用来在进行比较之前指定每个列表元素上要调用的函数,将函数的返回值作为比较的依据. 那么怎么使用这个 ...
- Oracle中函数/过程返回结果集的几种方式
原文 Oracle中函数/过程返回结果集的几种方式 Oracle中函数/过程返回结果集的几种方式: 以函数return为例,存储过程只需改为out参数即可,在oracle 10g测试通过. ...
- 计算机中用函数排序,编写一个sort()函数,实现选择法排序,在主函数中输入10个整数,主函数调用sort()函数实现排序后将数据输出。...
编写一个sort()函数,实现选择法排序,在主函数中输入10个整数,主函数调用sort()函数实现排序后将数据输出. 重赏重赏 更新时间:2019-05-06 04:06 最满意答案 #include ...
- oracle只查询0点数据,Oracle 10g数据库基础之基本查询语句-中-函数
Oracle 10g数据库基础之基本查询语句-中-函数 --资料参考:张烈 张建中<数据库管理员培训讲义> 函数: 使用函数的目的是为了操作数据 将输入的变量处理,返回一个结果. 变量可以 ...
最新文章
- C++/C++11中头文件numeric的使用
- 番外:Spring MVC环境搭建和Mybatis配置避坑篇
- P1198 [JSOI2008]最大数
- 简单php不用mysql_简单的PHP / MySQL不工作
- 课时106.边框练习(理解)
- 前端学习(892):bom概述
- leecode11 盛水最多的容器
- Canvas 画占比图 解决canvas锯齿 bug
- 一行代码添加P值的可视化技巧分享~~
- 自动驾驶使用贝塞尔曲线进行静态障碍物避障测试
- android httppost
- Julia :元编程、宏
- I need to follow my heart
- 三角形箭头向右的_向右三角形特殊符号
- 宝塔面板nginx跨域配置(跳坑)
- 第二周:神经网络的编程基础(Basics of Neural Network programming)
- Freebase调研(持续更新中)
- 新手入门3D游戏建模一定要搞懂的流程!
- 乱七八糟之处理器天梯图
- pma连接,报错10061