参考原文: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排序使用,很多中函数,不同的效果的更多相关文章

  1. oracle中函数和存储过程的区别和联系【转载竹沥半夏】

    oracle中函数和存储过程的区别和联系[转载竹沥半夏] 在oracle中,函数和存储过程是经常使用到的,他们的语法中有很多相似的地方,但也有自己的特点.刚学完函数和存储过程,下面来和大家分享一下自己 ...

  2. oracle中函数和存储过程的区别和联系

    oracle中函数和存储过程的区别和联系 在oracle中,函数和存储过程是经常使用到的,他们的语法中有很多相似的地方,但也有自己的特点.刚学完函数和存储过程,下面来和大家分享一下自己总结的关于函数和 ...

  3. Oracle中函数/过程返回结果集的几种方式

    原文 Oracle中函数/过程返回结果集的几种方式 Oracle中函数/过程返回结果集的几种方式:    以函数return为例,存储过程只需改为out参数即可,在oracle 10g测试通过.    ...

  4. Oracle中函数/过程返回多个值(结果集)

    Oracle中函数/过程返回结果集的几种方式: 以函数return为例,存储过程只需改为out参数即可,在oracle 10g测试通过. (1) 返回游标: return的类型为:SYS_REFCUR ...

  5. MySQL存储过程中的3种循环,存储过程的基本语法,ORACLE与MYSQL的存储过程/函数的使用区别,退出存储过程方法

    在MySQL存储过程的语句中有三个标准的循环方式:WHILE循环,LOOP循环以及REPEAT循环.还有一种非标准的循环方式:GOTO,不过这种循环方式最好别用,很容易引起程序的混乱,在这里就不错具体 ...

  6. Python中排序方法sort、函数sorted的key参数的作用分析

    从Python2.4开始,list.sort方法 和 sorted方法 都增加了一个 'key' 参数用来在进行比较之前指定每个列表元素上要调用的函数,将函数的返回值作为比较的依据. 那么怎么使用这个 ...

  7. 谈谈自己对C语言中函数指针的一些理解 (第一次写博客,有点小兴奋哈)

    1.函数指针声明的格式及简单的使用 (1)格式:(返回值)(*函数指针名)(参数列表)    例如:声明一个无参数无返回值的函数指针(void)(*p)(void). (2)将函数指针指向某个无参数无 ...

  8. 关于oracle数据库(10)函数

    分析函数,用于统计排名 语法:函数名() over(order by 排序字段 asc | desc) row_number() 无论值是否相等,生成连续的行号 -- 1,2,3,4, select ...

  9. STL 算法中函数对象和谓词

    STL 算法中函数对象和谓词 函数对象和谓词定义 函数对象: 重载函数调用操作符的类,其对象常称为函数对象(function object),即它们是行为类似函数的对象.一个类对象,表现出一个函数的特 ...

随机推荐

  1. WordPress 后台上传自定义网站Logo

    需求: 众所周知一般网站的logo都是固定的所以我在做网站时也是使用的静态logo文件,但最近用wp给一个客户做的网站时,因为网站现在的logo可能会需要重新设计,所以客户提出了需要在后台可以自己修改 ...

  2. Cognos业务洞察力:My First Business Insight

    Cognos Dashboard Cognos Dashboard 可以展示具有重要影响力的信息,以监视.衡量和管理企业绩效. IBM Cognos Dashboard(仪表盘)使任何用户能够以支持其 ...

  3. 【7】AccessDB快速数据访问

    阅读目录 C#和VB数据访问的比较 AccessDB的设计 数据库的连接 三种主要操作 错误输出及调试 小结 回到顶部 C#和VB数据访问的比较 C#中要进行一次普通的数据库查询,需要创建连接,再根据 ...

  4. [Node.js]26. Level 5 : Route rendering

    Instead of just writing out the quote to the response, instead render the quote.ejs template, passin ...

  5. 关于bootstrap-datetimepicker.js不支持IE8的解决办法

    if (!Array.prototype.indexOf) Array.prototype.indexOf = function (elt /*, from*/) { var len = this.l ...

  6. Struts2远程代码执行漏洞预警

    近期struts2 框架再现高危远程命令执行漏洞,漏洞编号S2-045,CVE编号CVE-2017-5638.利用此漏洞可对使用了struts2框架的网站进行远程命令执行,对服务器造成威胁.请相关单位 ...

  7. webpack打包过滤console.log

    在webpack.prod.conf.js里面的plugins里面加上 drop_debugger: true, drop_console: true new webpack.optimize.Ugl ...

  8. Java从零开始学六(运算符)

    运算符 一.赋值运算符号 No. 赋值运算符号 描述 1 = 赋值 int num=22; System.out.println("num= "+num); num=num-3; ...

  9. or1200中载入存储类指令说明

    下面内容摘自<步步惊芯--软核处理器内部设计分析>一书 OR1200中实现的载入存储类指令有8条,每条指令的作用与说明如表9.1所看到的. watermark/2/text/aHR0cDo ...

  10. map函数原理

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #map函数 #map函数会对一个序列对象中的每一个元素应用被传入的函数,并返回一个包含了所有函数调用结果的一 ...