mysql开窗函数over_oracle分析函数技术详解(配上开窗函数over())

一、Oracle分析函数入门 分析函数是什么? 分析函数是Oracle专门用于 解决复杂报表统计需求 的功能强大的函数, 它可以在数据中进行分组然后计算基于组的某种统计 ,并且每一组的每一行都可以返回一个统计。 分析函数和聚合函数的不同之处是什么? 普通的聚

一、Oracle分析函数入门

分析函数是什么?

分析函数是Oracle专门用于解决复杂报表统计需求的功能强大的函数,它可以在数据中进行分组然后计算基于组的某种统计值,并且每一组的每一行都可以返回一个统计值。

分析函数和聚合函数的不同之处是什么?

普通的聚合函数用group by分组,每个分组返回一个统计值,而分析函数采用partition by分组,并且每组每行都可以返回一个统计值。

分析函数的形式

分析函数带有一个开窗函数over(),包含三个分析子句:分组(partition by), 排序(order by), 窗口(rows) ,他们的使用形式如下:over(partition by xxx order by yyy rows between zzz)。

注:窗口子句在这里我只说rows方式的窗口,range方式和滑动窗口也不提

分析函数例子(在scott用户下模拟)

示例目的:显示各部门员工的工资,并附带显示该部分的最高工资。

test.jsp?url=http%3A%2F%2Fcommon.cnblogs.com%2Fimages%2Fcopycode.gif&refer=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F15336313

--显示各部门员工的工资,并附带显示该部分的最高工资。SELECT E.DEPTNO,

E.EMPNO,

E.ENAME,

E.SAL,

LAST_VALUE(E.SAL)

OVER(PARTITION BY E.DEPTNO

ORDER BY E.SAL ROWS

--unbounded preceding and unbouned following针对当前所有记录的前一条、后一条记录,也就是表中的所有记录 --unbounded:不受控制的,无限的 --preceding:在...之前 --following:在...之后 BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) MAX_SAL

FROM EMP E;

test.jsp?url=http%3A%2F%2Fcommon.cnblogs.com%2Fimages%2Fcopycode.gif&refer=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F15336313

运行结果:

test.jsp?url=http%3A%2F%2Fpic002.cnblogs.com%2Fimages%2F2012%2F270324%2F2012040415535157.png&refer=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F15336313

示例目的:按照deptno分组,然后计算每组值的总和

SELECT EMPNO,

ENAME,

DEPTNO,

SAL,

SUM(SAL) OVER(PARTITION BY DEPTNO ORDER BY ENAME) max_sal

FROM SCOTT.EMP;

运行结果:

test.jsp?url=http%3A%2F%2Fpic002.cnblogs.com%2Fimages%2F2012%2F270324%2F2012040415590388.png&refer=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F15336313

示例目的:对各部门进行分组,并附带显示第一行至当前行的汇总

test.jsp?url=http%3A%2F%2Fcommon.cnblogs.com%2Fimages%2Fcopycode.gif&refer=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F15336313

SELECT EMPNO,

ENAME,

DEPTNO,

SAL,

--注意ROWS BETWEEN unbounded preceding AND current row 是指第一行至当前行的汇总 SUM(SAL) OVER(PARTITION BY DEPTNO

ORDER BY ENAME

ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) max_sal

FROM SCOTT.EMP;

test.jsp?url=http%3A%2F%2Fcommon.cnblogs.com%2Fimages%2Fcopycode.gif&refer=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F15336313

运行结果:

test.jsp?url=http%3A%2F%2Fpic002.cnblogs.com%2Fimages%2F2012%2F270324%2F2012040416042411.png&refer=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F15336313

示例目标:当前行至最后一行的汇总

test.jsp?url=http%3A%2F%2Fcommon.cnblogs.com%2Fimages%2Fcopycode.gif&refer=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F15336313

SELECT EMPNO,

ENAME,

DEPTNO,

SAL,

--注意ROWS BETWEEN current row AND unbounded following 指当前行到最后一行的汇总 SUM(SAL) OVER(PARTITION BY DEPTNO

ORDER BY ENAME

ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) max_sal

FROM SCOTT.EMP;

test.jsp?url=http%3A%2F%2Fcommon.cnblogs.com%2Fimages%2Fcopycode.gif&refer=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F15336313

运行结果:

test.jsp?url=http%3A%2F%2Fpic002.cnblogs.com%2Fimages%2F2012%2F270324%2F2012040416132921.png&refer=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F15336313

示例目标:当前行的上一行(rownum-1)到当前行的汇总

test.jsp?url=http%3A%2F%2Fcommon.cnblogs.com%2Fimages%2Fcopycode.gif&refer=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F15336313

SELECT EMPNO,

ENAME,

DEPTNO,

SAL,

--注意ROWS BETWEEN 1 preceding AND current row 是指当前行的上一行(rownum-1)到当前行的汇总 SUM(SAL) OVER(PARTITION BY DEPTNO

ORDER BY ENAME ROWS

BETWEEN 1 PRECEDING AND CURRENT ROW) max_sal

FROM SCOTT.EMP;

test.jsp?url=http%3A%2F%2Fcommon.cnblogs.com%2Fimages%2Fcopycode.gif&refer=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F15336313

运行结果:

test.jsp?url=http%3A%2F%2Fpic002.cnblogs.com%2Fimages%2F2012%2F270324%2F2012040416170288.png&refer=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F15336313

示例目标: 当前行的上一行(rownum-1)到当前行的下辆行(rownum+2)的汇总

test.jsp?url=http%3A%2F%2Fcommon.cnblogs.com%2Fimages%2Fcopycode.gif&refer=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F15336313

SELECT EMPNO,

ENAME,

DEPTNO,

SAL,

--注意ROWS BETWEEN 1 preceding AND 1 following 是指当前行的上一行(rownum-1)到当前行的下辆行(rownum+2)的汇总 SUM(SAL) OVER(PARTITION BY DEPTNO

ORDER BY ENAME

ROWS BETWEEN 1 PRECEDING AND 2 FOLLOWING) max_sal

FROM SCOTT.EMP;

test.jsp?url=http%3A%2F%2Fcommon.cnblogs.com%2Fimages%2Fcopycode.gif&refer=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F15336313

运行结果:

test.jsp?url=http%3A%2F%2Fpic002.cnblogs.com%2Fimages%2F2012%2F270324%2F2012040416191976.png&refer=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F15336313

二、理解over()函数

1.1、两个order by的执行时机

分析函数(以及与其配合的开窗函数over())是在整个sql查询结束后(sql语句中的order by的执行比较特殊)再进行的操作, 也就是说sql语句中的order by也会影响分析函数的执行结果:

a) 两者一致:如果sql语句中的order by满足与分析函数配合的开窗函数over()分析时要求的排序,即sql语句中的order by子句里的内容和开窗函数over()中的order

by子句里的内容一样,

那么sql语句中的排序将先执行,分析函数在分析时就不必再排序;

b) 两者不一致:如果sql语句中的order by不满足与分析函数配合的开窗函数over()分析时要求的排序,即sql语句中的order by子句里的内容和开窗函数over()中的order

by子句里的内容不一样,

那么sql语句中的排序将最后在分析函数分析结束后执行排序。

1.2、开窗函数over()分析函数中的分组/排序/窗口

开窗函数over()分析函数包含三个分析子句:分组子句(partition by), 排序子句(order by), 窗口子句(rows)

窗口就是分析函数分析时要处理的数据范围,就拿sum来说,它是sum窗口中的记录而不是整个分组中的记录,因此我们在想得到某个栏位的累计值时,我们需要把窗口指定到该分组中的第一行数据到当前行, 如果你指定该窗口从该分组中的第一行到最后一行,那么该组中的每一个sum值都会一样,即整个组的总和。

窗口子句在这里我只说rows方式的窗口,range方式和滑动窗口也不提。

窗口子句中我们经常用到指定第一行,当前行,最后一行这样的三个属性:

第一行是 unbounded preceding,

当前行是 current row,

最后一行是 unbounded following,

注释:

当开窗函数over()出现分组(partition by)子句时,

unbounded preceding即第一行是指表中一个分组里的第一行,unbounded following即最后一行是指表中一个分组里的最后一行;

当开窗函数over()省略了分组(partition

by)子句时,

unbounded preceding即第一行是指表中的第一行,unbounded following即最后一行是指表中的最后一行。

窗口子句不能单独出现,必须有order by子句时才能出现,

例如:

last_value(sal) over(partition by deptno

order by sal

rows between unbounded preceding and unbounded following)

以上示例指定窗口为整个分组。而出现order by子句的时候,不一定要有窗口子句,但效果会很不一样,此时的窗口默认是当前组的第一行到当前行!

如果省略分组,则把全部记录当成一个组。

a) 如果存在order by则默认窗口是unbounded preceding and current row --当前组的第一行到当前行

b) 如果这时省略order by则窗口默认为unbounded preceding and unbounded following --整个组

而无论是否省略分组子句,如下结论都是成立的:

1、窗口子句不能单独出现,必须有order by子句时才能出现。

2、当省略窗口子句时:a) 如果存在order by则默认的窗口是unbounded preceding and current row --当前组的第一行到当前行,即在当前组中,第一行到当前行

b) 如果同时省略order by则默认的窗口是unbounded preceding and unbounded following --整个组

所以,

lag(sal) over(order by sal) 解释

over(order by salary)表示意义如下:

首先,我们要知道由于省略分组子句,所以当前组的范围为整个表的数据行,

然后,在当前组(此时为整个表的数据行)这个范围里执行排序(即order by salary),

最后,我们知道分析函数lag(sal)在当前组(此时为整个表的数据行)这个范围里的窗口范围为当前组的第一行到当前行,即分析函数lag(sal)在这个窗口范围执行。

参见:

Oracle的LAG和LEAD分析函数

Oracle分析函数ROW_NUMBER()|RANK()|LAG()使用详解

1.3、帮助理解over()的实例

例1:关注点:sql无排序,over()排序子句省略

SELECT DEPTNO, EMPNO, ENAME, SAL,

LAST_VALUE(SAL) OVER(PARTITION BY DEPTNO)

FROM EMP;

运行结果:

test.jsp?url=http%3A%2F%2Fpic002.cnblogs.com%2Fimages%2F2012%2F270324%2F2012040519432562.png&refer=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F15336313

例2:关注点:sql无排序,over()排序子句有,窗口省略

test.jsp?url=http%3A%2F%2Fcommon.cnblogs.com%2Fimages%2Fcopycode.gif&refer=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F15336313

SELECT DEPTNO,

EMPNO,

ENAME,

SAL,

LAST_VALUE(SAL) OVER(PARTITION BY DEPTNO

ORDER BY SAL DESC)

FROM EMP;

test.jsp?url=http%3A%2F%2Fcommon.cnblogs.com%2Fimages%2Fcopycode.gif&refer=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F15336313

运行结果:

test.jsp?url=http%3A%2F%2Fpic002.cnblogs.com%2Fimages%2F2012%2F270324%2F2012040519445151.png&refer=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F15336313

例3:关注点:sql无排序,over()排序子句有,窗口也有,窗口特意强调全组数据

test.jsp?url=http%3A%2F%2Fcommon.cnblogs.com%2Fimages%2Fcopycode.gif&refer=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F15336313

SELECT DEPTNO,

EMPNO,

ENAME,

SAL,

LAST_VALUE(SAL)

OVER(PARTITION BY DEPTNO

ORDER BY SAL

ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) MAX_SAL

FROM EMP;

test.jsp?url=http%3A%2F%2Fcommon.cnblogs.com%2Fimages%2Fcopycode.gif&refer=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F15336313

运行结果:

test.jsp?url=http%3A%2F%2Fpic002.cnblogs.com%2Fimages%2F2012%2F270324%2F2012040519462678.png&refer=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F15336313

例4:关注点:sql有排序(正序),over()排序子句无,先做sql排序再进行分析函数运算

test.jsp?url=http%3A%2F%2Fcommon.cnblogs.com%2Fimages%2Fcopycode.gif&refer=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F15336313

SELECT DEPTNO,

MGR,

ENAME,

SAL,

HIREDATE,

LAST_VALUE(SAL) OVER(PARTITION BY DEPTNO) LAST_VALUE

FROM EMP

WHERE DEPTNO = 30

ORDER BY DEPTNO, MGR;

test.jsp?url=http%3A%2F%2Fcommon.cnblogs.com%2Fimages%2Fcopycode.gif&refer=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F15336313

运行结果:

test.jsp?url=http%3A%2F%2Fpic002.cnblogs.com%2Fimages%2F2012%2F270324%2F2012040519474655.png&refer=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F15336313

例5:关注点:sql有排序(倒序),over()排序子句无,先做sql排序再进行分析函数运算

test.jsp?url=http%3A%2F%2Fcommon.cnblogs.com%2Fimages%2Fcopycode.gif&refer=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F15336313

SELECT DEPTNO,

MGR,

ENAME,

SAL,

HIREDATE,

LAST_VALUE(SAL) OVER(PARTITION BY DEPTNO) LAST_VALUE

FROM EMP

WHERE DEPTNO = 30

ORDER BY DEPTNO, MGR DESC;

test.jsp?url=http%3A%2F%2Fcommon.cnblogs.com%2Fimages%2Fcopycode.gif&refer=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F15336313

运行结果:

test.jsp?url=http%3A%2F%2Fpic002.cnblogs.com%2Fimages%2F2012%2F270324%2F2012040519485794.png&refer=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F15336313

例6:关注点:sql有排序(倒序),over()排序子句有,窗口子句无,此时的运算是:sql先选数据但是不排序,而后排序子句先排序并进行分析函数处理(窗口默认为第一行到当前行),最后再进行sql排序

test.jsp?url=http%3A%2F%2Fcommon.cnblogs.com%2Fimages%2Fcopycode.gif&refer=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F15336313

SELECT DEPTNO,

MGR,

ENAME,

SAL,

HIREDATE,

MIN(SAL) OVER(PARTITION BY DEPTNO ORDER BY SAL ASC) LAST_VALUE

FROM EMP

WHERE DEPTNO = 30

ORDER BY DEPTNO, MGR DESC;

test.jsp?url=http%3A%2F%2Fcommon.cnblogs.com%2Fimages%2Fcopycode.gif&refer=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F15336313

运行结果:

test.jsp?url=http%3A%2F%2Fpic002.cnblogs.com%2Fimages%2F2012%2F270324%2F2012040519501614.png&refer=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F15336313

test.jsp?url=http%3A%2F%2Fcommon.cnblogs.com%2Fimages%2Fcopycode.gif&refer=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F15336313

SELECT DEPTNO,

MGR,

ENAME,

SAL,

HIREDATE,

MIN(SAL) OVER(PARTITION BY DEPTNO ORDER BY SAL DESC) LAST_VALUE

FROM EMP

WHERE DEPTNO = 30

ORDER BY DEPTNO, MGR DESC;

test.jsp?url=http%3A%2F%2Fcommon.cnblogs.com%2Fimages%2Fcopycode.gif&refer=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F15336313

运行结果:

test.jsp?url=http%3A%2F%2Fpic002.cnblogs.com%2Fimages%2F2012%2F270324%2F2012040519511846.png&refer=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F15336313

三、常见分析函数详解

为了方便进行实践,特将演示表和数据罗列如下:

一、创建表

create table t(

bill_month varchar2(12) ,

area_code number,

net_type varchar(2),

local_fare number

);

二、插入数据

test.jsp?url=http%3A%2F%2Fcommon.cnblogs.com%2Fimages%2Fcopycode.gif&refer=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F15336313

insert into t values('200405',5761,'G', 7393344.04);

insert into t values('200405',5761,'J', 5667089.85);

insert into t values('200405',5762,'G', 6315075.96);

insert into t values('200405',5762,'J', 6328716.15);

insert into t values('200405',5763,'G', 8861742.59);

insert into t values('200405',5763,'J', 7788036.32);

insert into t values('200405',5764,'G', 6028670.45);

insert into t values('200405',5764,'J', 6459121.49);

insert into t values('200405',5765,'G', 13156065.77);

insert into t values('200405',5765,'J', 11901671.70);

insert into t values('200406',5761,'G', 7614587.96);

insert into t values('200406',5761,'J', 5704343.05);

insert into t values('200406',5762,'G', 6556992.60);

insert into t values('200406',5762,'J', 6238068.05);

insert into t values('200406',5763,'G', 9130055.46);

insert into t values('200406',5763,'J', 7990460.25);

insert into t values('200406',5764,'G', 6387706.01);

insert into t values('200406',5764,'J', 6907481.66);

insert into t values('200406',5765,'G', 13562968.81);

insert into t values('200406',5765,'J', 12495492.50);

insert into t values('200407',5761,'G', 7987050.65);

insert into t values('200407',5761,'J', 5723215.28);

insert into t values('200407',5762,'G', 6833096.68);

insert into t values('200407',5762,'J', 6391201.44);

insert into t values('200407',5763,'G', 9410815.91);

insert into t values('200407',5763,'J', 8076677.41);

insert into t values('200407',5764,'G', 6456433.23);

insert into t values('200407',5764,'J', 6987660.53);

insert into t values('200407',5765,'G', 14000101.20);

insert into t values('200407',5765,'J', 12301780.20);

insert into t values('200408',5761,'G', 8085170.84);

insert into t values('200408',5761,'J', 6050611.37);

insert into t values('200408',5762,'G', 6854584.22);

insert into t values('200408',5762,'J', 6521884.50);

insert into t values('200408',5763,'G', 9468707.65);

insert into t values('200408',5763,'J', 8460049.43);

insert into t values('200408',5764,'G', 6587559.23);

insert into t values('200408',5764,'J', 7342135.86);

insert into t values('200408',5765,'G', 14450586.63);

insert into t values('200408',5765,'J', 12680052.38);

commit;

test.jsp?url=http%3A%2F%2Fcommon.cnblogs.com%2Fimages%2Fcopycode.gif&refer=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F15336313

三、first_value()与last_value():求最值对应的其他属性

问题、取出每月通话费最高和最低的两个地区。

test.jsp?url=http%3A%2F%2Fcommon.cnblogs.com%2Fimages%2Fcopycode.gif&refer=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F15336313

SELECT BILL_MONTH,

AREA_CODE,

SUM(LOCAL_FARE) LOCAL_FARE,

FIRST_VALUE(AREA_CODE)

OVER(PARTITION BY BILL_MONTH

ORDER BY SUM(LOCAL_FARE) DESC

ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) FIRSTVAL,

LAST_VALUE(AREA_CODE)

OVER(PARTITION BY BILL_MONTH

ORDER BY SUM(LOCAL_FARE) DESC

ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) LASTVAL

FROM T

GROUP BY BILL_MONTH, AREA_CODE

ORDER BY BILL_MONTH

test.jsp?url=http%3A%2F%2Fcommon.cnblogs.com%2Fimages%2Fcopycode.gif&refer=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F15336313

运行结果:

test.jsp?url=http%3A%2F%2Fpic002.cnblogs.com%2Fimages%2F2012%2F270324%2F2012040615282316.png&refer=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F15336313

四、rank(),dense_rank()与row_number():求排序

rank,dense_rank,row_number函数为每条记录产生一个从1开始至n的自然数,n的值可能小于等于记录的总数。这3个函数的唯一区别在于当碰到相同数据时的排名策略。

①row_number:

row_number函数返回一个唯一的值,当碰到相同数据时,排名按照记录集中记录的顺序依次递增。

②dense_rank:

dense_rank函数返回一个唯一的值,当碰到相同数据时,此时所有相同数据的排名都是一样的。

③rank:

rank函数返回一个唯一的值,当碰到相同的数据时,此时所有相同数据的排名是一样的,同时会在最后一条相同记录和下一条不同记录的排名之间空出排名。

演示数据在Oracle自带的scott用户下:

1、rank()值相同时排名相同,其后排名跳跃不连续

test.jsp?url=http%3A%2F%2Fcommon.cnblogs.com%2Fimages%2Fcopycode.gif&refer=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F15336313

SELECT *

FROM (SELECT DEPTNO,

RANK() OVER(PARTITION BY DEPTNO ORDER BY SAL DESC) RW,

ENAME,

SAL

FROM SCOTT.EMP)

WHERE RW <= 4;

test.jsp?url=http%3A%2F%2Fcommon.cnblogs.com%2Fimages%2Fcopycode.gif&refer=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F15336313

运行结果:

test.jsp?url=http%3A%2F%2Fpic002.cnblogs.com%2Fimages%2F2012%2F270324%2F2012040615331530.png&refer=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F15336313

2、dense_rank()值相同时排名相同,其后排名连续不跳跃

test.jsp?url=http%3A%2F%2Fcommon.cnblogs.com%2Fimages%2Fcopycode.gif&refer=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F15336313

SELECT *

FROM (SELECT DEPTNO,

DENSE_RANK() OVER(PARTITION BY DEPTNO ORDER BY SAL DESC) RW,

ENAME,

SAL

FROM SCOTT.EMP)

WHERE RW <= 4;

test.jsp?url=http%3A%2F%2Fcommon.cnblogs.com%2Fimages%2Fcopycode.gif&refer=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F15336313

运行结果:

test.jsp?url=http%3A%2F%2Fpic002.cnblogs.com%2Fimages%2F2012%2F270324%2F2012040615355970.png&refer=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F15336313

3、row_number()值相同时排名不相等,其后排名连续不跳跃

test.jsp?url=http%3A%2F%2Fcommon.cnblogs.com%2Fimages%2Fcopycode.gif&refer=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F15336313

SELECT *

FROM (SELECT DEPTNO,

ROW_NUMBER() OVER(PARTITION BY DEPTNO ORDER BY SAL DESC) RW,

ENAME,

SAL

FROM SCOTT.EMP)

WHERE RW <= 4;

test.jsp?url=http%3A%2F%2Fcommon.cnblogs.com%2Fimages%2Fcopycode.gif&refer=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F15336313

运行结果:

test.jsp?url=http%3A%2F%2Fpic002.cnblogs.com%2Fimages%2F2012%2F270324%2F2012040615372594.png&refer=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F15336313

五、lag()与lead():求之前或之后的第N行

lag和lead函数可以在一次查询中取出同一字段的前n行的数据和后n行的值。这种操作可以使用对相同表的表连接来实现,不过使用lag和lead有更高的效率。

lag(arg1,arg2,arg3)

第一个参数是列名,

第二个参数是偏移的offset,

第三个参数是超出记录窗口时的默认值。

举例如下:

SQL> select * from kkk;

ID NAME

---------- --------------------

1 1name

2 2name

3 3name

4 4name

5 5name

SQL> select id,name,lag(name,1,0) over(order by id) from kkk;

ID NAME LAG(NAME,1,0)OVER(ORDERBYID)

---------- -------------------- ----------------------------

1 1name 0

2 2name 1name

3 3name 2name

4 4name 3name

5 5name 4name

SQL> select id,name,lead(name,1,0) over(order by id) from kkk;

ID NAME LEAD(NAME,1,0)OVER(ORDERBYID)

---------- -------------------- -----------------------------

1 1name 2name

2 2name 3name

3 3name 4name

4 4name 5name

5 5name 0

SQL> select id,name,lead(name,2,0) over(order by id) from kkk;

ID NAME LEAD(NAME,2,0)OVER(ORDERBYID)

---------- -------------------- -----------------------------

1 1name 3name

2 2name 4name

3 3name 5name

4 4name 0

5 5name 0

SQL> select id,name,lead(name,1,'linjiqin') over(order by id) from kkk;

ID NAME LEAD(NAME,1,'ALSDFJLASDJFSAF')

---------- -------------------- ------------------------------

1 1name 2name

2 2name 3name

3 3name 4name

4 4name 5name

5 5name linjiqin

---------------------------------------------------------------------------------------

六、rollup()与cube():排列组合分组

1)、group by rollup(a, b, c):

首先会对(a、b、c)进行group by,

然后再对(a、b)进行group by,

其后再对(a)进行group by,

最后对全表进行汇总操作。

2)、group by cube(a, b, c):

则首先会对(a、b、c)进行group by,

然后依次是(a、b),(a、c),(a),(b、c),(b),(c),

最后对全表进行汇总操作。

1、生成演示数据:

Connected to Oracle Database 10g Enterprise Edition Release 10.2.0.1.0

Connected as ds_trade

SQL> conn system/oracle as sysdba

Connected to Oracle Database 10g Enterprise Edition Release 10.2.0.3.0

Connected as SYS

SQL> create table scott.t as select * from dba_indexes;

Table created

SQL> connect scott/oracle

Connected to Oracle Database 10g Enterprise Edition Release 10.2.0.3.0

Connected as scott

SQL>

2、普通group by体验

sql> select owner, index_type, status, count(*) from t where owner like 'SY%' group by owner, index_type, status;

test.jsp?url=http%3A%2F%2Fpic002.cnblogs.com%2Fimages%2F2012%2F270324%2F2012040617130779.png&refer=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F15336313

3、group by rollup(A,B,C)

GROUP BY ROLLUP(A, B, C):

首先会对(A、B、C)进行GROUP BY,

然后再对(A、B)进行GROUP BY,

其后再对(A)进行GROUP BY,

最后对全表进行汇总操作。

sql> select owner, index_type, status, count(*) from t where owner like 'SY%' group by ROLLUP(owner, index_type, status);

test.jsp?url=http%3A%2F%2Fpic002.cnblogs.com%2Fimages%2F2012%2F270324%2F2012040617132888.png&refer=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F15336313

4、group by cube(A,B,C)

GROUP BY CUBE(A, B, C):

则首先会对(A、B、C)进行GROUP BY,

然后依次是(A、B),(A、C),(A),(B、C),(B),(C),

最后对全表进行汇总操作。

sql> select owner, index_type, status, count(*) from t where owner like 'SY%' group by cube(owner, index_type, status);

test.jsp?url=http%3A%2F%2Fpic002.cnblogs.com%2Fimages%2F2012%2F270324%2F2012040617134765.png&refer=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F15336313

七、max(),min(),sun()与avg():求移动的最值总和与平均值

问题:计算出各个地区连续3个月的通话费用的平均数(移动平均值)

test.jsp?url=http%3A%2F%2Fcommon.cnblogs.com%2Fimages%2Fcopycode.gif&refer=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F15336313

SELECT AREA_CODE,

BILL_MONTH,

LOCAL_FARE,

SUM(LOCAL_FARE) OVER(PARTITION BY AREA_CODE

ORDER BY TO_NUMBER(BILL_MONTH)

RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING) "3month_sum",

AVG(LOCAL_FARE) OVER(PARTITION BY AREA_CODE

ORDER BY TO_NUMBER(BILL_MONTH)

RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING) "3month_avg",

MAX(LOCAL_FARE) OVER(PARTITION BY AREA_CODE

ORDER BY TO_NUMBER(BILL_MONTH)

RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING) "3month_max",

MIN(LOCAL_FARE) OVER(PARTITION BY AREA_CODE

ORDER BY TO_NUMBER(BILL_MONTH)

RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING) "3month_min"

FROM (SELECT T.AREA_CODE, T.BILL_MONTH, SUM(T.LOCAL_FARE) LOCAL_FARE

FROM T

GROUP BY T.AREA_CODE, T.BILL_MONTH)

test.jsp?url=http%3A%2F%2Fcommon.cnblogs.com%2Fimages%2Fcopycode.gif&refer=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F15336313

运行结果:

test.jsp?url=http%3A%2F%2Fpic002.cnblogs.com%2Fimages%2F2012%2F270324%2F2012040711242793.png&refer=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F15336313

问题:求各地区按月份累加的通话费

test.jsp?url=http%3A%2F%2Fcommon.cnblogs.com%2Fimages%2Fcopycode.gif&refer=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F15336313

SELECT AREA_CODE,

BILL_MONTH,

LOCAL_FARE,

SUM(LOCAL_FARE) OVER(PARTITION BY AREA_CODE

ORDER BY BILL_MONTH ASC) "last_sum_value"

FROM (SELECT T.AREA_CODE, T.BILL_MONTH, SUM(T.LOCAL_FARE) LOCAL_FARE

FROM T

GROUP BY T.AREA_CODE, T.BILL_MONTH)

ORDER BY AREA_CODE, BILL_MONTH

test.jsp?url=http%3A%2F%2Fcommon.cnblogs.com%2Fimages%2Fcopycode.gif&refer=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F15336313

运行结果:

test.jsp?url=http%3A%2F%2Fpic002.cnblogs.com%2Fimages%2F2012%2F270324%2F2012040711285263.png&refer=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F15336313

--------------------------------------------------------------------------

Blog:http://www.cnblogs.com/linjiqin/

J2EE、Android、Linux、Oracle QQ交流群:142463980、158560018(满)

另见:《Oracle分析函数ROW_NUMBER()|RANK()|LAG()使用详解》

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/502256.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

传递给系统调用的数据区域太小怎么解决_一口气说出“分布式追踪系统”原理!...

“ 在微服务架构中&#xff0c;一次请求往往涉及到多个模块&#xff0c;多个中间件&#xff0c;多台机器的相互协作才能完成。图片来自 Pexels这一系列调用请求中&#xff0c;有些是串行的&#xff0c;有些是并行的&#xff0c;那么如何确定这个请求背后调用了哪些应用&#xf…

combinations java_[LeetCode][Java] Combinations

题目&#xff1a;Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.For example, If n 4 and k 2, a solution is:[[2,4],[3,4],[2,3],[1,2],[1,3],[1,4],]题意:给定两个整数 n 和 k&#xff0c;返回1 ...n中k个数字的所有的组合。…

java将对象 缓存_ehcache java 对象缓存怎么实现

展开全部1.技术背景&#xff1a;系统缓存是32313133353236313431303231363533e4b893e5b19e31333337396236位于应用程序与物理数据源之间&#xff0c;用于临时存放复制数据的内存区域&#xff0c;目的是为减少应用程序对物理数据源访问的次数&#xff0c;从而提高应用程序的运行…

语义分割和实例分割_一文读懂语义分割与实例分割

以人工智能为导向的现代计算机视觉技术&#xff0c;在过去的十年中发生了巨大的变化。今天&#xff0c;它被广泛用于图像分类、人脸识别、物体检测、视频分析以及机器人及自动驾驶汽车中的图像处理等领域。图像分割技术是目前预测图像领域最热门的一项技术&#xff0c;原因在于…

游戏自审自查报告_开发的射箭小游戏上线了,分享一下我在开发过程中遇到的问题...

利用业余时间开发的微信小游戏-射箭救人质上线了&#xff0c;主要玩法就是操作弓箭射断绳子把人救下来就可以了。图片资源是我找一个朋友做的。开发过程不算太顺利。磕磕绊绊做了12关。希望大家支持下。谢谢。分享一下我在开发中遇到的问题和部分解决方案、希望对大家有所帮助。…

java float 存储方式_java-解析以字符串形式存储的float会抛出异...

在相关的源代码中可以看到,该值将被修剪&#xff1a;static FloatingDecimal.ASCIIToBinaryConverter readJavaFormatString(String arg) throws NumberFormatException {boolean arg0 false;boolean arg1 false;try {arg arg.trim();....因此,在转换为floatValue之前将删除…

java 抛出异常的目的_Java实验八,异常

一.实验目的1. 掌握自定义异常类的编写&#xff1b;2. 掌握使用try-catch语句来处理异常。二.实验内容及要求车站检查危险品的设备&#xff0c;如果发现危险品会发出警告。编程模拟设备发现危险品&#xff1a;1. 编写一个Exception的子类DangerException&#xff0c;该子类可以…

pythongui显示图片_opencv2.4.13+python2.7学习笔记--opencv中的Gui特性--图片:读图像,显示图像,保存图像...

#-*- coding: utf-8 -*-"""Created on Tue Mar 14 19:39:11 2017author: Thinkpad"""2.opencv中的Gui特性2.1图片&#xff1a;读图像&#xff0c;显示图像&#xff0c;保存图像读图像cv2.imread(a,b)a:图像所在的路径b:如何读取图片cv2.IMREAD_C…

postmapping注解_Swagger常用注解

在使用swagger时候如果掌握一些注解的使用&#xff0c;则在开发过程中测试的时候可以事半功倍&#xff0c;尤其在与前端技术进行联调&#xff0c;前端技术在访问swagger中的每个api时&#xff0c;可以很清楚的知道每个url对应的请求类型、参数类型、参数是否非必输、参数个数等…

java map 多个值_java 一个函数EnumMap返回多个值

在开发过程中&#xff0c;经常会有这种情况&#xff0c;就是一个函数需要返回多个值&#xff0c;这是一个问题&#xff01;&#xff01;网上这个问题的解决方法&#xff1a;1、使用map返回值&#xff1b;这个方法问题是&#xff0c;你并不知道如何返回值的key是什么&#xff0c…

python pandas 日期_python+pandas+时间、日期以及时间序列处理方法

pythonpandas时间、日期以及时间序列处理方法先简单的了解下日期和时间数据类型及工具 python标准库包含于日期(date)和时间(time)数据的数据类型&#xff0c;datetime、time以及calendar模块会被经常用到。 datetime以毫秒形式存储日期和时间&#xff0c;datetime.timedelta表…

java程序怎么都不是一个_java运行的流程-怎么运行java程序编了一个程序不知道怎么运行郁闷啊后缀文件名是 爱问知识人...

在初学java编程语言时&#xff0c;痛苦的事莫过于跟着示例一步步做&#xff0c;总是得不到想要的结果&#xff0c;这是很多初学者都会碰到的问题。下面详细教你运行第一个java应用程序(环境windows xp jdk 6。0)&#xff1a;第一步&#xff1a;下载并安装JDK 6。0&#xff0c;…

调用别的方法的返回值_Spring boot如何实现异步调用

Spring boot如何实现异步调用异步调用:一个可以无需等待被调用函数的返回值就让操作继续进行的方法举个例子异步调用就是你 喊 你朋友吃饭 &#xff0c;你朋友说知道了 &#xff0c;待会忙完去找你 &#xff0c;你就去做别的了。同步调用就是你 喊 你朋友吃饭 &#xff0c;你朋…

java excel 创建按钮_通过单击Excel按钮在Catia中设计零件

我已经在用宏编辑器构建的Catia V5中编写了一些宏&#xff0c;但我无法弄清楚如何从Excel访问Catia命令 .我想知道如何通过仅在excel文件圆柱体的半径和长度中创建一个简单的直圆柱体 .我想在Catia中输入不同的压力容器&#xff0c;在Excel中输入它们的直径和高度&#xff0c;然…

pythonspot_python-Spotipy-列表索引超出范围

编写Spotipy脚本以从给定专辑返回专辑曲目,我偶尔会遇到错误&#xff1a;album_id results["albums"]["items"][0]["uri"]IndexError: list index out of range这种错误往往发生在更多的流行歌手遍历所有专辑的时候.我猜结果列表已达到极限或以…

国防科大JAVA工程师笔试题_国防科大人工智能考博题答案

【实例简介】国防科大计算机学院考博试题 人工智能11-15年试题答案A-20年33(注主:图21的日2该是想反3):61(35),610)①到日1①bs15②①9-519海:S→B>D>印k0b4763到句2(013-13e(as|0⊙(升早巧一③Cm52B877③1315④①a3s93(62833106舞决:5>B→>D>2或5A>C>D→…

python四舍五入保留小数点后三位_Python中的“正确”四舍五入到小数点后3位

我可能遗漏了一些重要的东西&#xff0c;但我无法找到一种方法来在Python(2.7)中将浮点/小数“适当”舍入&#xff0c;至少舍入到小数点后三位。我的意思是1.2225应该四舍五入到1.223&#xff0c;而1.2224应该四舍五入到1.222。在我知道在Python中&#xff0c;round不适用于flo…

java 指代对象_java-This的理解

都知道this是一个指代作用吧&#xff0c;但指代的是什么&#xff1f;一般网上搜索的this 都说是“当前对象”&#xff0c;但这样说和没说区别一个样&#xff0c;什么是“当前对象”都说不清楚。但其实&#xff0c;this指代得分下面4种情况来说。有的是场景展示的(如一、三)&…

python 日历查询系统_python 日历

上章总结了python中time模块的使用&#xff0c;这次总结日历模块 calendar>>> import calendar>>> cal calendar.month(2016,1)>>> calJanuary 2016\nMo Tu We Th Fr Sa Su\n 1 2 3\n 4 5 6 7 8 9 10\n11 12 13 14 15 16 17\n1…

在java语法中继承_java中的继承

一、继承&#xff1a;1.如果不使用继承&#xff0c;会出现以下一些问题&#xff1a;(1)代码重复(2)如果要修改的话&#xff0c;两个类都需要修改。2.继承体现的是一种is a 的关系。eg:Dog is a Pet;apple is a Fruit;Student is a Person;二、子类可以继承到父类的哪些财产呢&a…