csq39 发表于 2017-6-2 13:07:30

Oracle高级查询之OVER (PARTITION BY ..)

<p><em style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px; white-space: normal; background-color: rgb(255, 255, 255);">注:标题中的红色order by是说明在使用该方法的时候必须要带上order by。</em><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px; background-color: rgb(255, 255, 255);"></span></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px; white-space: normal; background-color: rgb(255, 255, 255);"><strong><span style="font-size: 16px;">一、rank()/dense_rank() over(partition by ...<span style="color: rgb(255, 0, 0);">order by ...</span>)</span></strong></p><p><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px; background-color: rgb(255, 255, 255);">现在客户有这样一个需求,查询每个部门工资最高的雇员的信息,相信有一定oracle应用知识的同学都能写出下面的SQL语句:</span><br/></p><p><strong></strong>&nbsp;<a href="http://blog.csdn.net/fu0208/article/details/7179001#" class="ViewSource" title="view plain" target="_blank" style="color: rgb(160, 160, 160); text-decoration: none; border: none; padding: 1px; margin: 0px 10px 0px 0px; font-size: 9px; display: inline-block; width: 16px; height: 16px; text-indent: -2000px; background-image: url(http://static.blog.csdn.net/scripts/SyntaxHighlighter/styles/images/default/ico_plain.gif); background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: 0% 0%; background-repeat: no-repeat;">view plain</a>&nbsp;<a href="http://blog.csdn.net/fu0208/article/details/7179001#" class="CopyToClipboard" title="copy" target="_blank" style="color: rgb(160, 160, 160); text-decoration: none; border: none; padding: 1px; margin: 0px 10px 0px 0px; font-size: 9px; display: inline-block; width: 16px; height: 16px; text-indent: -2000px; background-image: url(http://static.blog.csdn.net/scripts/SyntaxHighlighter/styles/images/default/ico_copy.gif); background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: 0% 0%; background-repeat: no-repeat;">copy</a></p><ol class="dp-sql list-paddingleft-2" style="padding: 0px; border: none; color: rgb(92, 92, 92); margin-bottom: 1px !important; margin-left: 45px !important; background-color: rgb(255, 255, 255);"><li><p><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">select</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;e.ename,&nbsp;e.job,&nbsp;e.sal,&nbsp;e.deptno&nbsp;&nbsp;</span></span></p></li><li><p><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;<span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">from</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;scott.emp&nbsp;e,&nbsp;&nbsp;</span></span></p></li><li><p><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(<span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">select</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;e.deptno,&nbsp;</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">max</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">(e.sal)&nbsp;sal&nbsp;</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">from</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;scott.emp&nbsp;e&nbsp;</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">group</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">by</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;e.deptno)&nbsp;me&nbsp;&nbsp;</span></span></p></li><li><p><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;<span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">where</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;e.deptno&nbsp;=&nbsp;me.deptno&nbsp;&nbsp;</span></span></p></li><li><p><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;<span class="op" style="margin: 0px; padding: 0px; border: none; color: rgb(128, 128, 128); background-color: inherit;">and</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;e.sal&nbsp;=&nbsp;me.sal;&nbsp;&nbsp;</span></span></p></li></ol><p><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px; background-color: rgb(255, 255, 255);">在满足客户需求的同时,大家应该习惯性的思考一下是否还有别的方法。这个是肯定的,就是使用本小节标题中rank() over(partition by...)或dense_rank() over(partition by...)语法,SQL分别如下:</span></p><p><strong></strong>&nbsp;<a href="http://blog.csdn.net/fu0208/article/details/7179001#" class="ViewSource" title="view plain" target="_blank" style="color: rgb(160, 160, 160); text-decoration: none; border: none; padding: 1px; margin: 0px 10px 0px 0px; font-size: 9px; display: inline-block; width: 16px; height: 16px; text-indent: -2000px; background-image: url(http://static.blog.csdn.net/scripts/SyntaxHighlighter/styles/images/default/ico_plain.gif); background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: 0% 0%; background-repeat: no-repeat;">view plain</a>&nbsp;<a href="http://blog.csdn.net/fu0208/article/details/7179001#" class="CopyToClipboard" title="copy" target="_blank" style="color: rgb(160, 160, 160); text-decoration: none; border: none; padding: 1px; margin: 0px 10px 0px 0px; font-size: 9px; display: inline-block; width: 16px; height: 16px; text-indent: -2000px; background-image: url(http://static.blog.csdn.net/scripts/SyntaxHighlighter/styles/images/default/ico_copy.gif); background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: 0% 0%; background-repeat: no-repeat;">copy</a></p><ol class="dp-sql list-paddingleft-2" style="padding: 0px; border: none; color: rgb(92, 92, 92); margin-bottom: 1px !important; margin-left: 45px !important; background-color: rgb(255, 255, 255);"><li><p><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">select</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;e.ename,&nbsp;e.job,&nbsp;e.sal,&nbsp;e.deptno&nbsp;&nbsp;</span></span></p></li><li><p><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;<span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">from</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;(</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">select</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;e.ename,&nbsp;&nbsp;</span></span></p></li><li><p><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.job,&nbsp;&nbsp;</span></p></li><li><p><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.sal,&nbsp;&nbsp;</span></p></li><li><p><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.deptno,&nbsp;&nbsp;</span></p></li><li><p><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rank()&nbsp;over(partition&nbsp;<span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">by</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;e.deptno&nbsp;</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">order</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">by</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;e.sal&nbsp;</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">desc</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">)&nbsp;rank&nbsp;&nbsp;</span></span></p></li><li><p><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">from</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;scott.emp&nbsp;e)&nbsp;e&nbsp;&nbsp;</span></span></p></li><li><p><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;<span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">where</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;e.rank&nbsp;=&nbsp;1;&nbsp;&nbsp;</span></span></p></li></ol><p><strong></strong>&nbsp;<a href="http://blog.csdn.net/fu0208/article/details/7179001#" class="ViewSource" title="view plain" target="_blank" style="color: rgb(160, 160, 160); text-decoration: none; border: none; padding: 1px; margin: 0px 10px 0px 0px; font-size: 9px; display: inline-block; width: 16px; height: 16px; text-indent: -2000px; background-image: url(http://static.blog.csdn.net/scripts/SyntaxHighlighter/styles/images/default/ico_plain.gif); background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: 0% 0%; background-repeat: no-repeat;">view plain</a>&nbsp;<a href="http://blog.csdn.net/fu0208/article/details/7179001#" class="CopyToClipboard" title="copy" target="_blank" style="color: rgb(160, 160, 160); text-decoration: none; border: none; padding: 1px; margin: 0px 10px 0px 0px; font-size: 9px; display: inline-block; width: 16px; height: 16px; text-indent: -2000px; background-image: url(http://static.blog.csdn.net/scripts/SyntaxHighlighter/styles/images/default/ico_copy.gif); background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: 0% 0%; background-repeat: no-repeat;">copy</a></p><ol class="dp-sql list-paddingleft-2" style="padding: 0px; border: none; color: rgb(92, 92, 92); margin-bottom: 1px !important; margin-left: 45px !important; background-color: rgb(255, 255, 255);"><li><p><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">select</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;e.ename,&nbsp;e.job,&nbsp;e.sal,&nbsp;e.deptno&nbsp;&nbsp;</span></span></p></li><li><p><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;<span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">from</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;(</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">select</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;e.ename,&nbsp;&nbsp;</span></span></p></li><li><p><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.job,&nbsp;&nbsp;</span></p></li><li><p><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.sal,&nbsp;&nbsp;</span></p></li><li><p><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.deptno,&nbsp;&nbsp;</span></p></li><li><p><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dense_rank()&nbsp;over(partition&nbsp;<span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">by</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;e.deptno&nbsp;</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">order</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">by</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;e.sal&nbsp;</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">desc</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">)&nbsp;rank&nbsp;&nbsp;</span></span></p></li><li><p><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">from</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;scott.emp&nbsp;e)&nbsp;e&nbsp;&nbsp;</span></span></p></li><li><p><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;<span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">where</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;e.rank&nbsp;=&nbsp;1;&nbsp;&nbsp;</span></span></p></li></ol><p><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px; background-color: rgb(255, 255, 255);">为什么会得出跟上面的语句一样的结果呢?这里补充讲解一下rank()/dense_rank() over(partition by e.deptno order by e.sal desc)语法。</span><br/><strong style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px; white-space: normal; background-color: rgb(255, 255, 255);">over:&nbsp;&nbsp;</strong><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px; background-color: rgb(255, 255, 255);">在什么条件之上。</span><br/><strong style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px; white-space: normal; background-color: rgb(255, 255, 255);">partition by e.deptno:&nbsp;&nbsp;</strong><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px; background-color: rgb(255, 255, 255);">按部门编号划分(分区)。</span><br/><strong style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px; white-space: normal; background-color: rgb(255, 255, 255);">order by e.sal desc:&nbsp;&nbsp;</strong><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px; background-color: rgb(255, 255, 255);">按工资从高到低排序(使用rank()/dense_rank() 时,必须要带order by否则非法)</span><br/><strong style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px; white-space: normal; background-color: rgb(255, 255, 255);">rank()/dense_rank():&nbsp;&nbsp;</strong><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px; background-color: rgb(255, 255, 255);">分级</span><br/><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px; background-color: rgb(255, 255, 255);">整个语句的意思就是:在按部门划分的基础上,按工资从高到低对雇员进行分级,“级别”由从小到大的数字表示(最小值一定为1)。&nbsp;</span></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px; white-space: normal; background-color: rgb(255, 255, 255);">那么rank()和dense_rank()有什么区别呢?<br/><strong>rank(): &nbsp;</strong>跳跃排序,如果有两个第一级时,接下来就是第三级。<br/><strong>dense_rank(): &nbsp;</strong>连续排序,如果有两个第一级时,接下来仍然是第二级。</p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px; white-space: normal; background-color: rgb(255, 255, 255);"><em>小作业:查询部门最低工资的雇员信息。</em></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px; white-space: normal; background-color: rgb(255, 255, 255);"><strong><span style="font-size: 16px;">二、min()/max() over(partition by ...)</span></strong></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px; white-space: normal; background-color: rgb(255, 255, 255);">现在我们已经查询得到了部门最高/最低工资,客户需求又来了,查询雇员信息的同时算出雇员工资与部门最高/最低工资的差额。这个还是比较简单,在第一节的groupby语句的基础上进行修改如下:<br/></p><p><strong></strong>&nbsp;<a href="http://blog.csdn.net/fu0208/article/details/7179001#" class="ViewSource" title="view plain" target="_blank" style="color: rgb(160, 160, 160); text-decoration: none; border: none; padding: 1px; margin: 0px 10px 0px 0px; font-size: 9px; display: inline-block; width: 16px; height: 16px; text-indent: -2000px; background-image: url(http://static.blog.csdn.net/scripts/SyntaxHighlighter/styles/images/default/ico_plain.gif); background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: 0% 0%; background-repeat: no-repeat;">view plain</a>&nbsp;<a href="http://blog.csdn.net/fu0208/article/details/7179001#" class="CopyToClipboard" title="copy" target="_blank" style="color: rgb(160, 160, 160); text-decoration: none; border: none; padding: 1px; margin: 0px 10px 0px 0px; font-size: 9px; display: inline-block; width: 16px; height: 16px; text-indent: -2000px; background-image: url(http://static.blog.csdn.net/scripts/SyntaxHighlighter/styles/images/default/ico_copy.gif); background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: 0% 0%; background-repeat: no-repeat;">copy</a></p><ol class="dp-sql list-paddingleft-2" style="padding: 0px; border: none; color: rgb(92, 92, 92); margin-bottom: 1px !important; margin-left: 45px !important; background-color: rgb(255, 255, 255);"><li><p><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">select</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;e.ename,&nbsp;&nbsp;</span></span></p></li><li><p><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.job,&nbsp;&nbsp;</span></p></li><li><p><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.sal,&nbsp;&nbsp;</span></p></li><li><p><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.deptno,&nbsp;&nbsp;</span></p></li><li><p><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.sal&nbsp;-&nbsp;me.min_sal&nbsp;diff_min_sal,&nbsp;&nbsp;</span></p></li><li><p><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;me.max_sal&nbsp;-&nbsp;e.sal&nbsp;diff_max_sal&nbsp;&nbsp;</span></p></li><li><p><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">from</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;scott.emp&nbsp;e,&nbsp;&nbsp;</span></span></p></li><li><p><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(<span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">select</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;e.deptno,&nbsp;</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">min</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">(e.sal)&nbsp;min_sal,&nbsp;</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">max</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">(e.sal)&nbsp;max_sal&nbsp;&nbsp;</span></span></p></li><li><p><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">from</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;scott.emp&nbsp;e&nbsp;&nbsp;</span></span></p></li><li><p><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">group</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">by</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;e.deptno)&nbsp;me&nbsp;&nbsp;</span></span></p></li><li><p><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;<span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">where</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;e.deptno&nbsp;=&nbsp;me.deptno&nbsp;&nbsp;</span></span></p></li><li><p><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;<span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">order</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">by</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;e.deptno,&nbsp;e.sal;&nbsp;&nbsp;</span></span></p></li></ol><p><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px; background-color: rgb(255, 255, 255);">上面我们用到了min()和max(),前者求最小值,后者求最大值。如果这两个方法配合over(partition by ...)使用会是什么效果呢?大家看看下面的SQL语句:</span></p><p><strong></strong>&nbsp;<a href="http://blog.csdn.net/fu0208/article/details/7179001#" class="ViewSource" title="view plain" target="_blank" style="color: rgb(160, 160, 160); text-decoration: none; border: none; padding: 1px; margin: 0px 10px 0px 0px; font-size: 9px; display: inline-block; width: 16px; height: 16px; text-indent: -2000px; background-image: url(http://static.blog.csdn.net/scripts/SyntaxHighlighter/styles/images/default/ico_plain.gif); background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: 0% 0%; background-repeat: no-repeat;">view plain</a>&nbsp;<a href="http://blog.csdn.net/fu0208/article/details/7179001#" class="CopyToClipboard" title="copy" target="_blank" style="color: rgb(160, 160, 160); text-decoration: none; border: none; padding: 1px; margin: 0px 10px 0px 0px; font-size: 9px; display: inline-block; width: 16px; height: 16px; text-indent: -2000px; background-image: url(http://static.blog.csdn.net/scripts/SyntaxHighlighter/styles/images/default/ico_copy.gif); background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: 0% 0%; background-repeat: no-repeat;">copy</a></p><ol class="dp-sql list-paddingleft-2" style="padding: 0px; border: none; color: rgb(92, 92, 92); margin-bottom: 1px !important; margin-left: 45px !important; background-color: rgb(255, 255, 255);"><li><p><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">select</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;e.ename,&nbsp;&nbsp;</span></span></p></li><li><p><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.job,&nbsp;&nbsp;</span></p></li><li><p><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.sal,&nbsp;&nbsp;</span></p></li><li><p><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.deptno,&nbsp;&nbsp;</span></p></li><li><p><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nvl(e.sal&nbsp;-&nbsp;<span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">min</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">(e.sal)&nbsp;over(partition&nbsp;</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">by</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;e.deptno),&nbsp;0)&nbsp;diff_min_sal,&nbsp;&nbsp;</span></span></p></li><li><p><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nvl(<span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">max</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">(e.sal)&nbsp;over(partition&nbsp;</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">by</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;e.deptno)&nbsp;-&nbsp;e.sal,&nbsp;0)&nbsp;diff_max_sal&nbsp;&nbsp;</span></span></p></li><li><p><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;<span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">from</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;scott.emp&nbsp;e;&nbsp;&nbsp;</span></span></p></li></ol><p><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px; background-color: rgb(255, 255, 255);">这两个语句的查询结果是一样的,大家可以看到min()和max()实际上求的还是最小值和最大值,只不过是在partition by分区基础上的。</span><br/></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px; white-space: normal; background-color: rgb(255, 255, 255);"><em>小作业:如果在本例中加上order by,会得到什么结果呢?</em></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px; white-space: normal; background-color: rgb(255, 255, 255);"><strong><span style="font-size: 16px;">三、lead()/lag() over(partition by ...&nbsp;order by ...)</span></strong><br/></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px; white-space: normal; background-color: rgb(255, 255, 255);">中国人爱攀比,好面子,闻名世界。客户更是好这一口,在和最高/最低工资比较完之后还觉得不过瘾,这次就提出了一个比较变态的需求,计算个人工资与比自己高一位/低一位工资的差额。这个需求确实让我很是为难,在groupby语句中不知道应该怎么去实现。不过。。。。现在我们有了over(partition by ...),一切看起来是那么的简单。如下:</p><p><strong></strong>&nbsp;<a href="http://blog.csdn.net/fu0208/article/details/7179001#" class="ViewSource" title="view plain" target="_blank" style="color: rgb(160, 160, 160); text-decoration: none; border: none; padding: 1px; margin: 0px 10px 0px 0px; font-size: 9px; display: inline-block; width: 16px; height: 16px; text-indent: -2000px; background-image: url(http://static.blog.csdn.net/scripts/SyntaxHighlighter/styles/images/default/ico_plain.gif); background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: 0% 0%; background-repeat: no-repeat;">view plain</a>&nbsp;<a href="http://blog.csdn.net/fu0208/article/details/7179001#" class="CopyToClipboard" title="copy" target="_blank" style="color: rgb(160, 160, 160); text-decoration: none; border: none; padding: 1px; margin: 0px 10px 0px 0px; font-size: 9px; display: inline-block; width: 16px; height: 16px; text-indent: -2000px; background-image: url(http://static.blog.csdn.net/scripts/SyntaxHighlighter/styles/images/default/ico_copy.gif); background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: 0% 0%; background-repeat: no-repeat;">copy</a></p><ol class="dp-sql list-paddingleft-2" style="padding: 0px; border: none; color: rgb(92, 92, 92); margin-bottom: 1px !important; margin-left: 45px !important; background-color: rgb(255, 255, 255);"><li><p><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">select</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;e.ename,&nbsp;&nbsp;</span></span></p></li><li><p><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.job,&nbsp;&nbsp;</span></p></li><li><p><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.sal,&nbsp;&nbsp;</span></p></li><li><p><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.deptno,&nbsp;&nbsp;</span></p></li><li><p><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lead(e.sal,&nbsp;1,&nbsp;0)&nbsp;over(partition&nbsp;<span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">by</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;e.deptno&nbsp;</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">order</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">by</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;e.sal)&nbsp;lead_sal,&nbsp;&nbsp;</span></span></p></li><li><p><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lag(e.sal,&nbsp;1,&nbsp;0)&nbsp;over(partition&nbsp;<span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">by</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;e.deptno&nbsp;</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">order</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">by</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;e.sal)&nbsp;lag_sal,&nbsp;&nbsp;</span></span></p></li><li><p><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nvl(lead(e.sal)&nbsp;over(partition&nbsp;<span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">by</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;e.deptno&nbsp;</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">order</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">by</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;e.sal)&nbsp;-&nbsp;e.sal,&nbsp;&nbsp;</span></span></p></li><li><p><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0)&nbsp;diff_lead_sal,&nbsp;&nbsp;</span></p></li><li><p><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nvl(e.sal&nbsp;-&nbsp;lag(e.sal)&nbsp;over(partition&nbsp;<span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">by</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;e.deptno&nbsp;</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">order</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">by</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;e.sal),&nbsp;0)&nbsp;diff_lag_sal&nbsp;&nbsp;</span></span></p></li><li><p><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;<span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">from</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;scott.emp&nbsp;e;&nbsp;&nbsp;&nbsp;</span></span></p></li></ol><p><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px; background-color: rgb(255, 255, 255);">看了上面的语句后,大家是否也会觉得虚惊一场呢(惊出一身冷汗后突然鸡冻起来,这样容易感冒)?我们还是来讲解一下上面用到的两个新方法吧。</span><br/><strong style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px; white-space: normal; background-color: rgb(255, 255, 255);">lead(列名,n,m): &nbsp;</strong><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px; background-color: rgb(255, 255, 255);">当前记录后面第n行记录的&lt;列名&gt;的值,没有则默认值为m;如果不带参数n,m,则查找当前记录后面第一行的记录&lt;列名&gt;的值,没有则默认值为null。</span><br/><strong style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px; white-space: normal; background-color: rgb(255, 255, 255);">lag(列名,n,m): &nbsp;</strong><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px; background-color: rgb(255, 255, 255);">当前记录前面第n行记录的&lt;列名&gt;的值,没有则默认值为m;如果不带参数n,m,则查找当前记录前面第一行的记录&lt;列名&gt;的值,没有则默认值为null。</span></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px; white-space: normal; background-color: rgb(255, 255, 255);">下面再列举一些常用的方法在该语法中的应用(<em><strong>注:带order by子句的方法说明在使用该方法的时候必须要带order by</strong></em>):</p><p><strong></strong>&nbsp;<a href="http://blog.csdn.net/fu0208/article/details/7179001#" class="ViewSource" title="view plain" target="_blank" style="color: rgb(160, 160, 160); text-decoration: none; border: none; padding: 1px; margin: 0px 10px 0px 0px; font-size: 9px; display: inline-block; width: 16px; height: 16px; text-indent: -2000px; background-image: url(http://static.blog.csdn.net/scripts/SyntaxHighlighter/styles/images/default/ico_plain.gif); background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: 0% 0%; background-repeat: no-repeat;">view plain</a>&nbsp;<a href="http://blog.csdn.net/fu0208/article/details/7179001#" class="CopyToClipboard" title="copy" target="_blank" style="color: rgb(160, 160, 160); text-decoration: none; border: none; padding: 1px; margin: 0px 10px 0px 0px; font-size: 9px; display: inline-block; width: 16px; height: 16px; text-indent: -2000px; background-image: url(http://static.blog.csdn.net/scripts/SyntaxHighlighter/styles/images/default/ico_copy.gif); background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: 0% 0%; background-repeat: no-repeat;">copy</a></p><ol class="dp-sql list-paddingleft-2" style="padding: 0px; border: none; color: rgb(92, 92, 92); margin-bottom: 1px !important; margin-left: 45px !important; background-color: rgb(255, 255, 255);"><li><p><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">select</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;e.ename,&nbsp;&nbsp;</span></span></p></li><li><p><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.job,&nbsp;&nbsp;</span></p></li><li><p><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.sal,&nbsp;&nbsp;</span></p></li><li><p><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.deptno,&nbsp;&nbsp;</span></p></li><li><p><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;first_value(e.sal)&nbsp;over(partition&nbsp;<span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">by</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;e.deptno)&nbsp;first_sal,&nbsp;&nbsp;</span></span></p></li><li><p><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;last_value(e.sal)&nbsp;over(partition&nbsp;<span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">by</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;e.deptno)&nbsp;last_sal,&nbsp;&nbsp;</span></span></p></li><li><p><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="func" style="margin: 0px; padding: 0px; border: none; color: rgb(255, 20, 147); background-color: inherit;">sum</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">(e.sal)&nbsp;over(partition&nbsp;</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">by</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;e.deptno)&nbsp;sum_sal,&nbsp;&nbsp;</span></span></p></li><li><p><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="func" style="margin: 0px; padding: 0px; border: none; color: rgb(255, 20, 147); background-color: inherit;">avg</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">(e.sal)&nbsp;over(partition&nbsp;</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">by</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;e.deptno)&nbsp;avg_sal,&nbsp;&nbsp;</span></span></p></li><li><p><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="func" style="margin: 0px; padding: 0px; border: none; color: rgb(255, 20, 147); background-color: inherit;">count</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">(e.sal)&nbsp;over(partition&nbsp;</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">by</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;e.deptno)&nbsp;count_num,&nbsp;&nbsp;</span></span></p></li><li><p><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;row_number()&nbsp;over(partition&nbsp;<span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">by</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;e.deptno&nbsp;</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">order</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">by</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;e.sal)&nbsp;row_num&nbsp;&nbsp;</span></span></p></li><li><p><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;<span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">from</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;scott.emp&nbsp;e;&nbsp;&nbsp;</span></span></p></li></ol><p><br/><strong style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px; white-space: normal; background-color: rgb(255, 255, 255);"><span style="color: rgb(255, 102, 102);">重要提示:大家在读完本片文章之后可能会有点误解,就是OVER (PARTITION BY ..)比GROUP BY更好,实际并非如此,前者不可能替代后者,而且在执行效率上前者也没有后者高,只是前者提供了更多的功能而已,所以希望大家在使用中要根据需求情况进行选择。</span></strong></p><p><br/></p>
页: [1]
查看完整版本: Oracle高级查询之OVER (PARTITION BY ..)