Oracle分析函数与分组关键字的用法
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字,全部文档内容请下载后查看。喜欢就下载吧 ……
相关推荐:
- [互联网资料]2022年厦门大学机电工程系824机械设计
- [互联网资料]东南大学2022年硕士研究生拟录取名单公
- [互联网资料]能源调研报告(精选多篇)
- [互联网资料]初三英语下学期 中考英语 语法填空训练
- [互联网资料]2022内蒙古选调生行测常识备考:新事物
- [互联网资料]自驾必备!在新西兰租什么样的车自驾游
- [互联网资料]佛教素食菜谱44页未完
- [互联网资料]盈利能力分析外文翻译
- [互联网资料]2022年南昌航空大学音乐学院736马克思
- [互联网资料]优选外贸跟单实习报告总结(精品版)
- [互联网资料]银行新员工培训总结
- [互联网资料]2_year_visa_new_guidance_190316
- [互联网资料]天津市五校宝坻一中静海一中杨村一中芦
- [互联网资料]2007--2008学年第一学期高三数学宁波市
- [互联网资料]Chromatic framework for vision in ba
- [互联网资料]幼儿园大班上学期美术教案《心愿树》含
- [互联网资料]2022年华中农业大学信息学院820微型计
- [互联网资料]硬盘坏道的表现 __硬盘使用久了
- [互联网资料]江苏省2016年会计从业资格考试《会计基
- [互联网资料]公共场所卫生监督试卷全解
- 高级英语第一册所有修辞方法及例子总结
- 综合交通枢纽规划与城市发展
- 沃尔玛的企业文化案例分析
- 美国Thanksgiving Day 感恩节 介绍
- PEP六年级英语上册Unit6How do you fee
- 最齐全的中国大型商场购物中心名单
- 数据结构实验报告八—哈夫曼编译码
- 杭州市余杭区人民政府(通知)
- 七年级语文成语运用专项训练
- 微观经济学第三章 消费者行为 课后习题
- 对_钱学森之问_的思考
- Excel_三级联动_下拉菜单
- 办公用品需求计划申请表
- 对外汉语教材必须要知道的发展史
- 挑战杯大学生学术科技作品竞赛作品申报
- 举办民办教育培训机构应具备下列条件
- 太阳能路灯项目设计方案
- 2013年八年级上最新人教版新教材Unit3I
- 【历史】 6-4 《近代科学之父牛顿》 课
- 高中生物《第四章 第二节 探讨加酶洗衣




