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…

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

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

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

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

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

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

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…

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

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

照片识别出错_AI跨年龄人脸识别技术在跨年龄寻亲的应用简析

9月3日&#xff0c;央视财经《经济半小时》栏目播出了一段有关失踪儿童找回的视频新闻。在这则新闻中&#xff0c;跨年龄人脸识别技术是最为核心的功臣&#xff0c;深圳警方利用跨年龄人脸识别技术&#xff0c;根据一张3岁孩童的儿童照片找回了失踪了十几年的孩子&#xff0c;让…

分段线性判别法 java_线性判别分析(Linear Discriminant Analysis)(二)

4. 实例将3维空间上的球体样本点投影到二维上&#xff0c;W1相比W2能够获得更好的分离效果。PCA与LDA的降维对比&#xff1a;PCA选择样本点投影具有最大方差的方向&#xff0c;LDA选择分类性能最好的方向。LDA既然叫做线性判别分析&#xff0c;应该具有一定的预测功能&#xff…

软件测试用例_大话软件测试用例要素

我们经常都知道一个测试用例里面包含以下几个要素&#xff1a;1&#xff0c;用例编号2&#xff0c;模块3&#xff0c;场景4&#xff0c;用例名称5&#xff0c;前置条件6&#xff0c;测试等级7&#xff0c;操作步骤8&#xff0c;预期结果(需求要求的结果)9&#xff0c;实际结果1…

java中this图解_JAVA-初步认识-第七章-this关键字的使用场景和原理图解

一.this有什么用&#xff1f;我们用例子的形式来体现一下。下面的截图说明&#xff0c;给人对象一初始化的时候&#xff0c;赋值个姓名。赋值姓名的时候&#xff0c;拿n赋值给name&#xff0c;DOS显示创建对象时&#xff0c;构造函数初始化成功。在例子中&#xff0c;我们将“旺…

python汉诺塔_汉诺塔递归算法/搬金盘的婆罗门 - Python实现

汉诺塔递归算法/搬金盘的婆罗门 - Python实现版权声明本文节选自作者本人的图书《Python编程基础及应用》&#xff0c;高等教育出版社。本文可以在互联网上自由转载&#xff0c;但必须&#xff1a;注明出处(作者&#xff1a;海洋饼干叔叔)并包含指向本页面的链接。本文不可以以…

reactor模型_Reactor模式以及Netty中的应用

思维导图一、Reactor模式介绍本文主要参考Doug Lea(大神)的《Scalable IO in Java》中讲述的Reactor模式。

sa是什么岗位_服务顾问SA在维修企业扮演的重要角色

前言&#xff1a;SA在维修企业中扮演什么样的角色&#xff0c;这就是我们今天的话题。作者 | 李连俊来源 | 汽车服务世界(ID&#xff1a;asworld168)客户的群体很多&#xff0c;不同的客户群体消费的能力不一样&#xff0c;SA简称【服务顾问】。SA要对客户的消费能力进行评估&a…

java cxf 不使用springmvc_使用cfx与springMVC集成发布与调用webservice

客户端调用代码 Client.java package com.quickmap.common;import javax.xml.namespace.QName;import javax.xml.ws.Service;import javax.xml.ws.soap.SOAPBinding;public final class Client {private static final QName SERVICE_NAME new QName("http://common.quickm…

基于hadoop的商品推荐系统_【论文笔记】基于矩阵分解的推荐系统

本文是对经典论文的阅读笔记&#xff0c;大部分为论文的中文翻译内容&#xff08;笔者英语水平也就六级飘过的水准&#xff0c;不喜勿喷&#xff09;论文标题&#xff1a;Matrix factorization techniques for recommender systems随着Netflix竞赛的结果所示&#xff0c;矩阵分…

php fpm在哪配置,php7的php-fpm.conf文件在哪里

php7下的php-fpm.conf文件一般是在php的安装目录下的etc目录中&#xff0c;文件路径为/usr/local/php-fpm/etc/php-fpm.conf。php-fpm.conf是php-fpm进程管理器的配置文件。php-fpm.conf是php-fpm进程管理器的配置文件。文件路径&#xff1a;/usr/local/php-fpm/etc/php-fpm.co…

aba会导致问题_肌肤缺水会导致哪些问题呢?我们又该怎么判断呢?

一.如何判断皮肤是否缺水&#xff1f;01.脸部肌肤粗糙&#xff0c;有紧绷感很多人在日常中都会用到洗面奶吧&#xff0c;在用过洗面奶之后如果发现肌肤变得粗糙&#xff0c;有紧绷感&#xff0c;那么说明你的肌肤就需要补水了。02.化妆之后浮粉、起皮如果给肌肤补足充足的水分&…

spring更新后 外层事务查不到_再深一点:面试工作两不误,源码级理解Spring事务...

原创&#xff1a;小姐姐味道(微信公众号ID&#xff1a;xjjdog)&#xff0c;欢迎分享&#xff0c;转载请保留出处。Spring有5种隔离级别&#xff0c;7种传播行为。这是面试常问的内容&#xff0c;也是代码中经常碰到的知识点。这些知识枯燥而且乏味&#xff0c;其中有些非常的绕…

箭头函数的this指向谁_高阶函数

NodeJS 系列文章&#xff0c;本篇是第一篇&#xff0c;首先&#xff0c;预计将后续高频使用逻辑串一遍&#xff0c;依次是高阶函数&#xff0c;promise以及事件机制。本篇主要是高阶函数。call、bind、applycall、apply 都是改变 this 指向&#xff0c;区别是接受参数的方式不一…