使用分析单

来源:互联网 发布:linux历史命令文件 编辑:程序博客网 时间:2024/05/21 11:59

分析函数计算基于行组的值,并可以用来实现rownum伪列表述的结果;

Sql代码 复制代码
  1. select ename ,sal,row_number() over(order by sal descas sal_order from emp;  


结果:

Sql代码 复制代码
  1. ENAME             SAL  SAL_ORDER   
  2. ---------- ---------- ----------   
  3. KING             5000          1   
  4. SCOTT            3000          2   
  5. FORD             3000          3   
  6. JONES            2975          4   
  7. BLAKE            2850          5   
  8. CLARK            2450          6   
  9. ALLEN            1600          7   
  10. TURNER           1500          8   
  11. MILLER           1300          9   
  12. WARD             1250         10   
  13. MARTIN           1250         11   
  14. ADAMS            1100         12   
  15. JAMES             950         13   
  16. SMITH             800         14  


在sql语句中,可以在行的顺序集上获得行号。肥西函数知道在非配行号之前使用order by 从句。

Sql代码 复制代码
  1. select deptno ename ,sal,row_number() over(partition by deptno order by sal descas sal_order from emp;  


结果:

Sql代码 复制代码
  1. ENAME        SAL  SAL_ORDER   
  2. ----- ---------- ----------   
  3.    10       5000          1   
  4.    10       2450          2   
  5.    10       1300          3   
  6.    20       3000          1   
  7.    20       3000          2   
  8.    20       2975          3   
  9.    20       1100          4   
  10.    20        800          5   
  11.    30       2850          1   
  12.    30       1600          2   
  13.    30       1500          3   
  14.    30       1250          4   
  15.    30       1250          5   
  16.    30        950          6  


row_number ()分析函数可以仅为行的单独顺序集分配行号码,也可以在结果集内部使用它。

Sql代码 复制代码
  1. select deptno ,ename, rownum ,sal from (select deptno, ename ,sal, row_number() over(partition by deptno order by sal descas sal_order from emp)where sal_order<2  


结果:

Sql代码 复制代码
  1. DEPTNO ENAME          ROWNUM        SAL   
  2. ------ ---------- ---------- ----------   
  3.     10 KING                1       5000   
  4.     20 SCOTT               2       3000   
  5.     30 BLAKE               3       2850