Oracle分析函数与分组关键字的用法
更新时间:2024-04-18 21:41:01 阅读量: 综合文库 文档下载
- 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;
正在阅读:
Oracle分析函数与分组关键字的用法04-18
外汇及现货黄金投资学习资料02-20
材料力学习题综合 - 图文10-28
《走近残疾人》教学设计07-27
风巷技术人员工作总结09-28
辅导讲义:常见辅助线作法03-08
技术部工程师薪酬绩效方案10-06
波浪理论精典教学课程06-26
家庭装修合同书(单包)06-09
- 多层物业服务方案
- (审判实务)习惯法与少数民族地区民间纠纷解决问题(孙 潋)
- 人教版新课标六年级下册语文全册教案
- 词语打卡
- photoshop实习报告
- 钢结构设计原理综合测试2
- 2014年期末练习题
- 高中数学中的逆向思维解题方法探讨
- 名师原创 全国通用2014-2015学年高二寒假作业 政治(一)Word版
- 北航《建筑结构检测鉴定与加固》在线作业三
- XX县卫生监督所工程建设项目可行性研究报告
- 小学四年级观察作文经典评语
- 浅谈110KV变电站电气一次设计-程泉焱(1)
- 安全员考试题库
- 国家电网公司变电运维管理规定(试行)
- 义务教育课程标准稿征求意见提纲
- 教学秘书面试技巧
- 钢结构工程施工组织设计
- 水利工程概论论文
- 09届九年级数学第四次模拟试卷
- 分组
- 用法
- 函数
- 关键字
- 分析
- Oracle
- 2014年湖南省会计从业资格证考试《会计基础》专项真题练习
- 乌鲁木齐经济技术开发区(头屯河区)总部经济发展策略思考
- 党委会职责及议事规则
- 进网证高压试验模拟题
- 合并财务报表习题
- WKB-801BG3技术说明书
- 基于FPGA的I2C接口程序实现毕业论文
- 艺安全可靠性论证报告
- 1-6章理论试题B卷--《临床护理实践指南》(带答案)
- 我对集体备课的几点思考
- 2010上服装设计大赛
- 矿级领导带班、跟班制度修改
- 河北省石家庄2018届高三教学质量检测(二)理综化学
- 佳和名都施工组织设计(1)2
- 雷暴
- 2016年商业补充医疗保险索赔需知-试行
- 东师企业经济活动分析17春在线作业2 免费答案
- 各种阀门的特点
- 电力拖动复习资料
- 会计学基础第4章 复式记账习题