教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 精品文档 > 互联网资料 >

Oracle分析函数与分组关键字的用法

来源:网络收集 时间:2026-09-26
导读: Oracle分析函数与分组关键字的用法 以下是我以前工作中做报表常用的几个函数,在此分享一下,希望对大家有帮助。 (一)分析函数 ?row_number Purpose ROW_NUMBER is an analytic function. It assigns a unique number to each row to which it is applied (e

Oracle分析函数与分组关键字的用法

以下是我以前工作中做报表常用的几个函数,在此分享一下,希望对大家有帮助。

(一)分析函数 ?row_number

Purpose

ROW_NUMBER is an analytic function. It assigns a unique number to each row to which it is applied (either each row in the partition or each row returned by the query), in the ordered sequence of rows specified in the order_by_clause, beginning with 1.

You cannot use ROW_NUMBER or any other analytic function for expr. That is, you can use other built-in function expressions for expr, but you cannot nest analytic functions.

按部门分组后根据工资排序,序号rn特征:连续、无并列 select t.*, row_number() over(partition by t.deptno order

by sal desc) rn from emp t;

?rank

Purpose

RANK calculates the rank of a value in a group of values. Rows with equal values for the ranking criteria receive the same rank. Oracle then adds the number of tied rows to the tied rank to calculate the next rank. Therefore, the ranks may not be consecutive numbers. ?

As an aggregate function, RANK calculates the rank of a hypothetical row identified by the arguments of the function with respect to a given sort specification. The arguments of the function must all evaluate to constant expressions within each aggregate group, because they identify a single row within each group. The constant argument expressions and the expressions in the ORDER BY clause of the aggregate match by position. Therefore, the number of arguments must be the same and their types must be compatible.

As an analytic function, RANK computes the rank of each row returned from a query with respect to the other rows returned by the query, based on the values of the value_exprs in the order_by_clause.

按部门分组后根据工资排序,序号rn特征:不连续、有并列

select t.*, rank() over(partition by t.deptno order

by sal desc) rn from emp t;

?dense_rank

Purpose

DENSE_RANK computes the rank of a row in an ordered group of rows. The ranks are consecutive integers beginning with 1. The largest rank value is the number of unique values returned by the query. Rank values are not skipped in the event of ties. Rows with equal values for the ranking criteria receive the same rank.

As an aggregate function, DENSE_RANK calculates the dense rank of a hypothetical row identified by the arguments of the function with respect to a given sort specification. The arguments of the function must all evaluate to constant expressions within each aggregate group, because they identify a single row within each group. The constant argument expressions and the expressions in the order_by_clause of the aggregate match by position. Therefore, the number of arguments must be the same and types must be compatible.

As an analytic function, DENSE_RANK computes the rank of each row returned from a query with respect to the other rows, based on the values of the value_exprs in the order_by_clause.

按部门分组后根据工资排序,序号rn特征:连续、有并列

select t.*, dense_rank() over(partition by t.deptno order

by sal desc) rn from emp t;

(二)分组函数

根据查询结果观察三者的区别,grouping sets用起来更灵活。

友情提示:grouping(expr)函数仅用于分组中,如果expr 为null 返回1

?Rollup

select decode(grouping(dept.dname), 1, '总计', dept.dname) dname, decode(grouping(emp.job), 1, '小计', emp.job) job, sum(sal), avg(sal) from emp, dept

where dept.deptno = emp.deptno group by rollup(dept.dname, emp.job) order by dname, job;

?Cube

select decode(grouping(dept.dname), 1, '所有部门', dept.dname) dname, nvl(emp.job, '小计') job, sum(sal), avg(sal) from emp, dept

where dept.deptno = emp.deptno group by cube(dept.dname, emp.job) order by dname, job;

?grouping sets

select dept.dname, emp. job, sum(sal), avg(sal) from emp, dept

where dept.deptno = emp.deptno

group by grouping sets((dept.dname, emp.job), dept.dname, emp.job) order by dname, job;

…… 此处隐藏:1682字,全部文档内容请下载后查看。喜欢就下载吧 ……
Oracle分析函数与分组关键字的用法.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wendang/446270.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150份
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150份
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)