原创

select *

  from (select province,

               commodity,

               sum(price),

               ROW_NUMBER() OVER(PARTITION BY province  order by sum(price) desc) rn

          from test141211

         group by province, commodity

        — order by province desc, sum(price) desc

        )

 where rn <= 5

 

开窗函数 其实就是group by的另一种。它于group by的区别在于开窗函数可以分组列中排序其实就是加了一列影藏列。可以在group by中在分组的意思

—————————————————————————————

 

翻译

基本介绍:

Analytic_clause的语法如下:

[ query_partition_clause ] [ order_by_clause [ windowing_clause ] ]

这里:

query_partition_clause是查询分组子句;

order_by_clause是分组排序子句;

windowing_clause是窗口范围子句。

分析函数在查询结果集确定之后才开始进行计算,Analytic_clause就是用来定义函数怎样对查询结果集进行分组计算的。

根据Oracle对查询和分析函数的处理方法可知,在select和order by子句中都可以使用分析函数。

query_partition_by、order_by_clause和windowing_clause三个子句是可选的,将三个子句分别简记为p,o,w。

合法的组合方式有如下6种:

1).        Pow

(query_partition_clause order_by_clause windowing_clause)

分组,排序,定义窗口范围

2).        Po

(query_partition_clause order_by_clause)

分组,排序,窗口默认为range between unbounded preceding and current row

3).        P

(query_partition_clause)

分组,不排序,没有窗口

4).        Ow

(order_by_clause windowing_clause)

分组为整个查询结果集,排序,定义窗口范围

5).        O

(order_by_clause)

分组为整个查询结果集,排序,窗口默认为range between unbounded preceding and current row

6).        Null

()

分组为整个查询结果集,不排序,没有窗口

因为只有存在order_by_clause,才能有windowing_clause,故不存在如下两种形式的组合:

pw(query_partition_clause windowing_clause)

w(windowing_clause)

总结:

1).        对于是否存在order_by_clause,分析函数可以分为两类,含有order_by_clause的一般称为windowing function,不含的称为reporting function。

2).        Windowing function,对查询结果集进行分组,排序,根据窗口范围计算分组中每一行的函数结果。

3).        Reporting function,对查询结果集进行分组,不排序,窗口范围为整个分组,在每一个分组内,计算整个分组的函数值,再将函数值分别赋给分组内的每一行。

一、开窗函数

    开窗函数指定了分析函数工作的数据窗口大小,这个数据窗口大小可能会随着行的变化而变化,举例如下:

    1、over(order by salary) 按照salary排序进行累计,order by是个默认的开窗函数。

    SELECT

        EMPLOYEE_ID, SALARY, MANAGER_ID, DEPARTMENT_ID,

        SUM(SALARY) OVER (ORDER BY SALARY) DD

    FROM INFA_TEST.EMPLOYEES EMP

    ORDER BY SALARY

    功能:按salary升序排序,统计小于等于当前salary的salary总和。

    返回结果:

        EMPLOYEE_ID SALARY MANAGER_ID DEPARTMENT_ID DD        

        132         2100   121        50            2100

        128         2200   120        50            6500

        136         2200   122        50            6500

        127         2400   120        50            11300

        135         2400   122        50            11300

        119         2500   114        30            26300

        140         2500   123        50            26300

        144         2500   124        50            26300

        191         2500   122        50            26300

        182         2500   120        50            26300

    注意 SALARY为2200、2400和2500行的DD值

    2、over(partition by DEPARTMENT_ID)按照部门分区。

    SELECT 

        EMPLOYEE_ID, SALARY, MANAGER_ID, DEPARTMENT_ID,

        SUM(SALARY) OVER (PARTITION BY DEPARTMENT_ID) DD

    FROM INFA_TEST.EMPLOYEES EMP

    ORDER BY DEPARTMENT_ID

    功能:按DEPARTMENT_ID分区,汇总各个部门的SALARY总和。

    返回结果:

     EMPLOYEE_ID  SALARY   MANAGER_ID   DEPARTMENT_ID   DD

     200          4400     101          10              4400

     201          13000    100          20              19000

     202          6000     201          20              19000

     114          11000    100          30              24900

     115          3100     114          30              24900

     116          2900     114          30              24900

     119          2500     114          30              24900

     118          2600     114          30              24900

     117          2800     114          30              24900

     注意 DEPARTMENT_ID为20,30的DD值

    3、over(partition by DEPARTMENT_ID order by SALARY)按照部门分区。

    SELECT 

        EMPLOYEE_ID, SALARY, MANAGER_ID, DEPARTMENT_ID,

        SUM(SALARY) OVER (PARTITION BY DEPARTMENT_ID ORDER BY SALARY) DD

    FROM INFA_TEST.EMPLOYEES EMP

    ORDER BY DEPARTMENT_ID

   功能:按DEPARTMENT_ID分区,按SALARY升序排序,统计各个部门内部小于当前SALARY的和。

    返回结果:

     EMPLOYEE_ID  SALARY   MANAGER_ID   DEPARTMENT_ID   DD

     200          4400     101          10              4400

     201          13000    100          20              6000

     202          6000     201          20              19000

     114          11000    100          30              2500

     115          3100     114          30              5100

     116          2900     114          30              7900

     119          2500     114          30              10800

     118          2600     114          30              13900

     117          2800     114          30              24900

    注意 DEPARTMENT_ID为20、30的DD值和2中的区别   

    4、over(order by salary range between 50 preceding and 150 following)

    SQL> select 

           empno,sal,mgr,deptno,

           sum(sal) over (partition by deptno order by sal 

               RANGE BETWEEN 0 PRECEDING AND 100 FOLLOWING) dd 

         from emp;   

                                                                                                  

    功能:按DEPARTMENT_ID分区,按SALARY升序排序,汇总当前SALARY到比当前SALARY大100之间的SALARY总和。

    返回结果:

EMPNO    SAL    MGR     DEPTNO     DD                                                                                                       

—– —— —— ——-   ——–                                                                                                      

 7934   1300   7782      10     1300                                                                                                        

 7782   2450   7839      10     2450                                                                                                        

 7839   5000             10     5000                                                                                                        

 7369    800   7902      20      800                                                                                                        

 7566   2975   7839      20     5975       3000在2975和(2975+100)之间,故求2975与3000的和                                                                                                 

 7902   3000   7566      20     3000                                                                                                        

 7900    950   7698      30      950                                                                                                        

 7521   1250   7698      30     2500                                                                                                        

 7654   1250   7698      30     2500                                                                                                        

 7844   1500   7698      30     3100                                                                                                        

 7499   1600   7698      30     1600                                                                                                        

 7698   2850   7839      30     2850                                                                                                                                                

已选择12行。                                                                                                                                

    解释:返回前置行和当前行SALARY相等,后续行比他大100的记录,在SALARY列上求和。

    上下边界没有限制:OVER (PARTITION BY DEPARTMENT_ID ORDER BY SALARY RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)      

  SQL> select empno,sal,mgr,deptno,

           sum(sal) over (partition by deptno order by sal 

                RANGE BETWEEN unbounded PRECEDING AND unbounded FOLLOWING) dd 

       from emp;

     EMPNO        SAL        MGR     DEPTNO         DD

———- ———- ———- ———- ———-

      7934       1300       7782         10       8750

      7782       2450       7839         10       8750

      7839       5000                    10       8750

      7369        800       7902         20       6775

      7566       2975       7839         20       6775

      7902       3000       7566         20       6775

      7900        950       7698         30       9400

      7521       1250       7698         30       9400

      7654       1250       7698         30       9400

      7844       1500       7698         30       9400

      7499       1600       7698         30       9400

      7698       2850       7839         30       9400

已选择12行。

    5、over(order by salary rows between 1 preceding and 2 following)– 每行对应的数据窗口是之前行幅度值不超过1,之后行幅度值不超过2

  SQL> select empno,sal,mgr,deptno,

            sum(sal) over (partition by deptno order by sal 

            rows BETWEEN 1 PRECEDING AND 2 FOLLOWING) dd 

       from emp;

    返回结果

        EMPNO        SAL        MGR     DEPTNO         DD

———- ———- ———- ———- ———-

      7934       1300       7782         10       8750

      7782       2450       7839         10       8750

      7839       5000                    10       7450

      7369        800       7902         20       6775

      7566       2975       7839         20       6775

      7902       3000       7566         20       5975

      7900        950       7698         30       3450

      7521       1250       7698         30       4950

      7654       1250       7698         30       5600

      7844       1500       7698         30       7200

      7499       1600       7698         30       5950

      7698       2850       7839         30       4450

已选择12行。