博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Oracle中常用的计算、统计类函数
阅读量:5300 次
发布时间:2019-06-14

本文共 7925 字,大约阅读时间需要 26 分钟。

Oracle中常用的计算、统计类函数


group by

scott@DBHAWK>select deptno,sum(sal) from emp group by deptno;    DEPTNO   SUM(SAL)---------- ----------    30       9400    20       10875    10       8750

rank ( ) over (partition by … order by … )

scott@DBHAWK>select t.*,rank() over(partition by deptno order by sal desc) RANK from emp t;     EMPNO ENAME      JOB          MGR HIREDATE     SAL           COMM     DEPTNO       RANK---------- ---------- --------- ---------- --------- ---------- ---------- ---------- ----------      7839 KING       PRESIDENT            17-NOV-81       5000            10          1      7782 CLARK      MANAGER     7839     09-JUN-81       2450            10          2      7934 MILLER     CLERK       7782     23-JAN-82       1300            10          3      7788 SCOTT      ANALYST     7566     19-APR-87       3000            20          1      7902 FORD       ANALYST     7566     03-DEC-81       3000            20          1      7566 JONES      MANAGER     7839     02-APR-81       2975            20          3      7876 ADAMS      CLERK       7788     23-MAY-87       1100            20          4      7369 SMITH      CLERK       7902     17-DEC-80        800            20          5      7698 BLAKE      MANAGER     7839     01-MAY-81       2850            30          1      7499 ALLEN      SALESMAN    7698     20-FEB-81       1600     300    30          2      7844 TURNER     SALESMAN    7698     08-SEP-81       1500      0     30          3      7521 WARD       SALESMAN    7698     22-FEB-81       1250     500    30          4      7654 MARTIN     SALESMAN    7698     28-SEP-81       1250    1400    30          4      8888 MARK       HAWKER      7698     28-SEP-81       1250    1400    30          4      7900 JAMES      CLERK       7698     03-DEC-81        950            30          7

dense_rank ( ) over (partition by … order by … )

scott@DBHAWK>select t.*,dense_rank() over(partition by deptno order by sal desc) DENSERANK from emp t;     EMPNO ENAME      JOB               MGR HIREDATE        SAL       COMM     DEPTNO  DENSERANK---------- ---------- --------- ---------- --------- ---------- ---------- ---------- ----------      7839 KING       PRESIDENT            17-NOV-81       5000            10          1      7782 CLARK      MANAGER     7839     09-JUN-81       2450            10          2      7934 MILLER     CLERK       7782     23-JAN-82       1300            10          3      7788 SCOTT      ANALYST     7566     19-APR-87       3000            20          1      7902 FORD       ANALYST     7566     03-DEC-81       3000            20          1      7566 JONES      MANAGER     7839     02-APR-81       2975            20          2      7876 ADAMS      CLERK       7788     23-MAY-87       1100            20          3      7369 SMITH      CLERK       7902     17-DEC-80        800            20          4      7698 BLAKE      MANAGER     7839     01-MAY-81       2850            30          1      7499 ALLEN      SALESMAN    7698     20-FEB-81       1600     300    30          2      7844 TURNER     SALESMAN    7698     08-SEP-81       1500      0     30          3      7521 WARD       SALESMAN    7698     22-FEB-81       1250     500    30          4      7654 MARTIN     SALESMAN    7698     28-SEP-81       1250    1400    30          4      8888 MARK       HAWKER      7698     28-SEP-81       1250    1400    30          4      7900 JAMES      CLERK       7698     03-DEC-81        950            30          5

row_number () over (partition by … order by … )

scott@DBHAWK>select t.*,row_number () over (partition by deptno order by sal desc)ROWNUMBER from emp t;     EMPNO ENAME      JOB          MGR      HIREDATE        SAL       COMM     DEPTNO  ROWNUMBER---------- ---------- --------- ---------- --------- ---------- ---------- ---------- ----------      7839 KING       PRESIDENT            17-NOV-81       5000            10          1      7782 CLARK      MANAGER     7839     09-JUN-81       2450            10          2      7934 MILLER     CLERK       7782     23-JAN-82       1300            10          3      7788 SCOTT      ANALYST     7566     19-APR-87       3000            20          1      7902 FORD       ANALYST     7566     03-DEC-81       3000            20          2      7566 JONES      MANAGER     7839     02-APR-81       2975            20          3      7876 ADAMS      CLERK       7788     23-MAY-87       1100            20          4      7369 SMITH      CLERK       7902     17-DEC-80        800            20          5      7698 BLAKE      MANAGER     7839     01-MAY-81       2850            30          1      7499 ALLEN      SALESMAN    7698     20-FEB-81       1600     300    30          2      7844 TURNER     SALESMAN    7698     08-SEP-81       1500      0     30          3      7521 WARD       SALESMAN    7698     22-FEB-81       1250     500    30          4      7654 MARTIN     SALESMAN    7698     28-SEP-81       1250    1400    30          5      8888 MARK       HAWKER      7698     28-SEP-81       1250    1400    30          6      7900 JAMES      CLERK       7698     03-DEC-81        950            30          7

min ( ) over (partition by … )

max ( ) over (partition by … )

scott@DBHAWK>select t.*,min(sal) over(partition by deptno)min_sal ,max(sal) over(partition by deptno)max_sal  from emp t;       EMPNO ENAME      JOB          MGR HIREDATE         SAL     COMM   DEPTNO MIN_SAL    MAX_SAL---------- ---------- --------- ---------- --------- ---------- -----  ------ --------- ------------       7782 CLARK      MANAGER     7839 09-JUN-81       2450            10       1300       5000      7934 MILLER     CLERK       7782 23-JAN-82       1300            10       1300       5000      7839 KING       PRESIDENT        17-NOV-81       5000            10       1300       5000      7566 JONES      MANAGER     7839 02-APR-81       2975            20        800       3000      7369 SMITH      CLERK       7902 17-DEC-80        800            20        800       3000      7788 SCOTT      ANALYST     7566 19-APR-87       3000            20        800       3000      7902 FORD       ANALYST     7566 03-DEC-81       3000            20        800       3000      7876 ADAMS      CLERK       7788 23-MAY-87       1100            20        800       3000      7521 WARD       SALESMAN    7698 22-FEB-81       1250     500    30        950       2850      7844 TURNER     SALESMAN    7698 08-SEP-81       1500      0     30        950       2850      7499 ALLEN      SALESMAN    7698 20-FEB-81       1600     300    30        950       2850      7900 JAMES      CLERK       7698 03-DEC-81        950            30        950       2850      8888 MARK       HAWKER      7698 28-SEP-81       1250    1400    30        950       2850      7654 MARTIN     SALESMAN    7698 28-SEP-81       1250    1400    30        950       2850      7698 BLAKE      MANAGER     7839 01-MAY-81       2850            30        950       2850

lead(列名,n,m): 当前记录后面第n行记录的<列名>的值,没有则默认值为m;如果不带参数n,m,则查找当前记录后面第一行的记录<列名>的值,没有则默认值为null。

lag(列名,n,m): 当前记录前面第n行记录的<列名>的值,没有则默认值为m;如果不带参数n,m,则查找当前记录前面第一行的记录<列名>的值,没有则默认值为null。

select t.*,         lead(sal, 1, 0) over(partition by deptno order by sal) lead_sal,         lag(sal, 1, 0) over(partition by deptno order by sal) lag_sal,         nvl(lead(sal) over(partition by deptno order by sal) - sal,             0) diff_lead_sal,         nvl(sal - lag(sal) over(partition by deptno order by sal), 0) diff_lag_sal    from emp t;

扩展使用

select t.*,         first_value(sal) over(partition by deptno) first_sal,         last_value(sal) over(partition by deptno) last_sal,         sum(sal) over(partition by deptno) sum_sal,         avg(sal) over(partition by deptno) avg_sal,         count(sal) over(partition by deptno) count_num,         row_number() over(partition by deptno order by sal) row_num    from emp t;

转载于:https://www.cnblogs.com/hawk20110625/p/9309473.html

你可能感兴趣的文章
CSS
查看>>
[LeetCode] 55. Jump Game_ Medium tag: Dynamic Programming
查看>>
[Cypress] Stub a Post Request for Successful Form Submission with Cypress
查看>>
程序集的混淆及签名
查看>>
判断9X9数组是否是数独的java代码
查看>>
00-自测1. 打印沙漏
查看>>
UNITY在VS中调试
查看>>
SDUTOJ3754_黑白棋(纯模拟)
查看>>
Scala入门(1)Linux下Scala(2.12.1)安装
查看>>
如何改善下面的代码 领导说了很耗资源
查看>>
Quartus II 中常见Warning 原因及解决方法
查看>>
php中的isset和empty的用法区别
查看>>
Android ViewPager 动画效果
查看>>
pip和easy_install使用方式
查看>>
博弈论
查看>>
Redis sentinel & cluster 原理分析
查看>>
我的工作习惯小结
查看>>
把word文档中的所有图片导出
查看>>
浏览器的判断;
查看>>
ubuntu 18.04取消自动锁屏以及设置键盘快捷锁屏
查看>>