参考原文: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. file not found app文件

    昨天svn迁移.然后又一次check out之后编译遇到这个错误. Ld Build/Products/Debug-iphonesimulator/wiseCloudCrmTests.xctest/w ...

  2. Android -- uses-sdk:minSdkVersion 10 cannot be smaller than version L declared in library com.android.support:appcompat-v7:21.0.0-rc1

    这是一个报错,是我在Android Studio上添加完Support-v4和v7包之后爆出的错误,百度了好久也没有百度到.当时我的项目有minSdkVersion 19. 设置版本最小为L的时候也会 ...

  3. 应用程序在状态栏展示时间(C#)

    private DispatcherTimer _timer; private void SetTimeElaspInStatusBar() { try { _timer = new Dispatch ...

  4. 微信小程序:字体保持大小

    小程序和网页差不多,前台用wxml把内容摆好,然后用css调整样式.所以和web一样,必须要能够精确控制每一个元素的大小.在Web中,通过CSS基本达到了像素级的控制.但在小程序中,情况有所不同.下面 ...

  5. php实现支付宝授权登录

    第一步: 登录到蚂蚁金服开放平台https://open.alipay.com/platform/home.htm,前提是有商户号.创建应用之后,然后到开发者中心开通对应功能.如图: 第二步: 到应用 ...

  6. (数据挖掘-入门-3)基于用户的协同过滤之k近邻

    主要内容: 1.k近邻 2.python实现 1.什么是k近邻(KNN) 在入门-1中,简单地实现了基于用户协同过滤的最近邻算法,所谓最近邻,就是找到距离最近或最相似的用户,将他的物品推荐出来. 而这 ...

  7. Win7没有防火墙:0x80070422

    在"MSDN我告诉你"下载的官方原版Window7 64位,安装后防火墙是这样的: 点击"使用推荐设置",报错:0x80070422,估计是因为使用PE中的工具 ...

  8. 【IPC进程间通讯之二】管道Pipe

    IPC进程间通信+管道Pipe                IPC(Inter-Process Communication.进程间通信).         管道用于进程间共享数据,事实上质是共享内存 ...

  9. MISRA-C++ 2008

  10. CSS nth-child、first-child、last-child、nth-of-type、first-of-type和last-of-type选择器使用

    以下示例主要讲解nth-child.first-child.last-child.nth-of-type.first-of-type和last-of-type使用. 示例代码: <!DOCTYP ...