温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

分析函数

发布时间:2020-08-04 11:48:13 来源:网络 阅读:258 作者:llc018198 栏目:关系型数据库

--row_number

SELECT ename,

       deptno,

       rownum,

       row_number() over(ORDER BY deptno) rn,

       rank() over(ORDER BY deptno) rk, --跳号

       dense_rank() over(ORDER BY deptno) dense_rk --不跳号

  FROM emp-- over 必须跟order by

 --row_number

SELECT ename,

       deptno,

       rownum,

       row_number() over(partition by deptno ORDER BY deptno) rn,

       rank() over(partition by deptno ORDER BY deptno) rk, 

       dense_rank() over(partition by deptno ORDER BY deptno) dense_rk 

  FROM emp



 --row_number

SELECT ename,

       deptno,

       rownum,

       row_number() over(partition by deptno ORDER BY sal) rn,

       rank() over(partition by deptno ORDER BY sal) rk,

       dense_rank() over(partition by deptno ORDER BY sal) dense_rk

  FROM emp

--求每个部门的最高工资

SELECT *

  FROM (SELECT ename,

               deptno,

               rownum,

               row_number() over(PARTITION BY deptno ORDER BY sal DESC) rn

        --rank() over(partition by deptno ORDER BY sal) rk,

        --dense_rank() over(partition by deptno ORDER BY sal) dense_rk

          FROM emp)

 WHERE rn = 1

 --sum,avg,max,min

 SELECT ename,

        deptno,

        sal,

        SUM(sal) over(PARTITION BY deptno) AS "部门汇总", SUM(sal) over() AS "全部汇总"

   FROM emp;


--累加

SELECT ename,

        deptno,

        sal,

        SUM(sal) over(order BY sal), SUM(sal) over(order BY sal,rowid)

   FROM emp;



 SELECT ename,

        deptno,

        sal,

        avg(sal) over(PARTITION BY deptno) AS "部门汇总", avg(sal) over() AS "全部汇总"

   FROM emp;

SELECT empno,

       ename,

       sal

  FROM emp a

 WHERE sal = (SELECT MIN(sal) FROM emp b WHERE a.deptno = b.deptno)

--改写

SELECT *

  FROM (

        

        SELECT a.*,

                row_number() over(PARTITION BY a.deptno ORDER BY a.sal) rn

        

          FROM emp a)

 WHERE rn = 1

 


SELECT *

  FROM emp

 WHERE sal in(SELECT MIN(sal) over(PARTITION BY deptno ORDER BY sal) FROM emp a);


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI