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中函数和存储过程的区别和联系【转载竹沥半夏】
oracle中函数和存储过程的区别和联系[转载竹沥半夏] 在oracle中,函数和存储过程是经常使用到的,他们的语法中有很多相似的地方,但也有自己的特点.刚学完函数和存储过程,下面来和大家分享一下自己 ...
- oracle中函数和存储过程的区别和联系
oracle中函数和存储过程的区别和联系 在oracle中,函数和存储过程是经常使用到的,他们的语法中有很多相似的地方,但也有自己的特点.刚学完函数和存储过程,下面来和大家分享一下自己总结的关于函数和 ...
- Oracle中函数/过程返回结果集的几种方式
原文 Oracle中函数/过程返回结果集的几种方式 Oracle中函数/过程返回结果集的几种方式: 以函数return为例,存储过程只需改为out参数即可,在oracle 10g测试通过. ...
- Oracle中函数/过程返回多个值(结果集)
Oracle中函数/过程返回结果集的几种方式: 以函数return为例,存储过程只需改为out参数即可,在oracle 10g测试通过. (1) 返回游标: return的类型为:SYS_REFCUR ...
- MySQL存储过程中的3种循环,存储过程的基本语法,ORACLE与MYSQL的存储过程/函数的使用区别,退出存储过程方法
在MySQL存储过程的语句中有三个标准的循环方式:WHILE循环,LOOP循环以及REPEAT循环.还有一种非标准的循环方式:GOTO,不过这种循环方式最好别用,很容易引起程序的混乱,在这里就不错具体 ...
- Python中排序方法sort、函数sorted的key参数的作用分析
从Python2.4开始,list.sort方法 和 sorted方法 都增加了一个 'key' 参数用来在进行比较之前指定每个列表元素上要调用的函数,将函数的返回值作为比较的依据. 那么怎么使用这个 ...
- 谈谈自己对C语言中函数指针的一些理解 (第一次写博客,有点小兴奋哈)
1.函数指针声明的格式及简单的使用 (1)格式:(返回值)(*函数指针名)(参数列表) 例如:声明一个无参数无返回值的函数指针(void)(*p)(void). (2)将函数指针指向某个无参数无 ...
- 关于oracle数据库(10)函数
分析函数,用于统计排名 语法:函数名() over(order by 排序字段 asc | desc) row_number() 无论值是否相等,生成连续的行号 -- 1,2,3,4, select ...
- STL 算法中函数对象和谓词
STL 算法中函数对象和谓词 函数对象和谓词定义 函数对象: 重载函数调用操作符的类,其对象常称为函数对象(function object),即它们是行为类似函数的对象.一个类对象,表现出一个函数的特 ...
随机推荐
- [NPM] Execute npx commands with $npm_ Environment Variables
We will incorporate npm specific environment variables when executing various npx commands. In our e ...
- easyui tree带checkbox实现单选
<ul id="regionTree"></ul> $('#regionTree').tree({ cascadeCheck: false, //onlyL ...
- 劣质代码评析——《写给大家看的C语言书(第2版)》附录B之21点程序(六)
. #include <stdio.h> . #include <time.h> . #include <ctype.h> . #include <stdli ...
- oracle client字符集设置 乱码问题
程序员经常要连接数据库 下面 就说一下 oracle数据库 客户端与服务器端 字符集一致性的问题 这可以解决中文乱码,其他字符乱码问题 主要是指在sqlplus中,其他类似toad/pls ...
- android实现六边形等不规则布局
在去年广告机项目中,UI设计出一个比较华丽的UI,但是对于我来说无从下手,我试过view的叠加并设置外边距实现,虽然勉强可以实现,但是获取单击焦点是有很多问题: 效果图如下: 最后只有另外想办法:我对 ...
- gradle 添加jar依赖,执行grade build时出现“程序包不存在”问题
引用的第三方依赖的包都找不到了 解决办法 group'com.suneony' version'1.0.0' apply plugin:'java' repositories { mavenLocal ...
- PyQt5教程——事件和信号(5)
PyQt5中的事件和信号 在这部分PyQt5编程教程中,我们探索应用中事件和信号的发生. 事件 所有的GUI应用都是事件驱动的.事件主要由应用的用户操作产生的.但是事件可能由其他条件触发,比如:一个网 ...
- Websocket——Websocket原理
偶然在知乎上看到一篇回帖,瞬间认为之前看的那么多资料都不及这一篇回帖让我对 websocket 的认识深刻有木有.所以转到我博客里,分享一下.比較喜欢看这样的博客,读起来非常轻松.不枯燥,没有布道师的 ...
- Linux网卡高级命令
网卡的高级命令 [root@gechong ~]# mii-tool No interface specified usage: mii-tool [-VvRrwl] [-A media,... | ...
- javascript 1.5s跳转
<script type="text/javascript"> var t = 1.5; window.onload=countDown; function count ...