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)
分享到:
相关推荐
Oracle 数据库开发培训中根据某一列的值修改另一列的值 Oracle 数据库是一种关系型数据库管理系统,广泛应用于企业级应用程序中。在 Oracle 数据库中,我们可以使用 update 语句来修改某一列的值根据另一列的值。...
oracle 某列存儲的值是有逗號的字符串,希望通過逗號分隔開來顯示多行。sql,有預期結果,有項目實戰。
oracle 查询列最大值,例如有n例值,但是要找出这些列中的最大值。
ORACLE 数据库中如何统计表记录数的过程说明
NULL 博文链接:https://zhuleishitou.iteye.com/blog/998637
首先,创建一个简单的测试表,这里过程就略过了,直接上表(真的是以简单为主,哈哈…) 第一种写法row_number(): SELECT val,COUNT(*) FROM (SELECT ID,val, row_number() OVER(ORDER BY ID)-row_number() OVER...
在公司实际的项目开发中,我遇到这样一个需求,需要将oracle数据库某张表的某两列相加形成新的一列。
ORACLE存储过程中定义数组并且判断某值是否在数组中。有例子可執行、 、有例子可執行、
统计oracle的一个库所拥有的记录数,思路是先得到数据库里所有的表,然后再统计每个表里面有多少条记录,累加最后得到总记录数。
oracle拼接字符串查询语句。 普通拼接字符串和拼接某一列的所有值。
oracle中记录用户登录信息的触发器的例子,有具体操作过程,包括错误信息及解释,这个例子是自己为某企业的oracle安全管理的具体例子
在oracle查询删除一个表编码中相同的记录
删除重复记录的方法原理:在Oracle中,每一条记录都有一个rowid,rowid在整个数据库中是唯一的,rowid确定了每条记录是在Oracle中的哪一个数据文件、块、行上。在重复的记录中,可能所有列的内容都相同,但rowid不会...
SQL和Oracle中统计数据库中表个数及表名
oracle行转列
Oracle左连接返回多条记录中一条记录的查询语句,更具指定条件分组排序,返回各组中第一条记录
oracle的按月统计sql..............................
oracle中查询两年之间某一个月的数据
oracle中取中间记录的方法,用到了minus来去中间记录
oracle知识列点 一个简单的行列转换例子,需要的可以看下