Oracle windowing function - turn

oracle and analysis functions over windowing function

Transfer: http://zonghl8006.blog.163.com/blog/static/4528311520083995931317/
a: the analysis function over
the Oracle function analysis began offering from 8.1.6, analysis functions for calculating the aggregate value based on a certain group, it the polymerization is different from the function
for each group to return multiple rows, and the aggregate function returns only one row for each group.
Here will be described a few examples of its application.                                      
1: Statistical turnover of a store.    

would like to purchase DATE
     1 20
     2 15
     3 14
     4 18
     5 30
    rules: by day statistics: Statistics are a daily total of the previous few days
    the results obtained:
DATE SALE SUM
    ----- -------- ---- -
    . 1 20 is 20 is --1 days          
    21535--1 days + 2 days          
    31449--1 days + 2 days + 3 days          
    . 41,867         
    . 30 97. 5
    
2: first grade students each class statistical information
    NAME S the CLASS                        
    ----- ----- ----------------------
    FDA. 1 80                    
    ffd    1      78                    
    dss    1      95                    
    cfe    2      74           

 gds    2      92                    
    gf     3      99                    
    ddd    3      99                    
    adf    3      45                    
    asdf   3      55                    
    3dd    3      78             
  
    通过:  
    --
    select * from                                                                      
    (                                                                           
    select name,class,s,rank()over(partition by class order by s desc) mm from t2
    )                                                                           
    where mm=1
    --
    得到结果:
    NAME   CLASS S                       MM                                                                                       
    ----- ----- ---------------------- ----------------------
    dss    1      95                      1                     
    gds    2      92                      1                     
    gf     3      99                      1                     
    ddd    3      99                      1

 Note:
    1. When seeking first results can not be used row_number (), because if the same class there are two tied for first, row_number () returns a result only        
    difference 2.rank () and dense_rank () are:
      - -rank () is jumping sorted, the next is the fourth time two second
      --dense_rank () l is a continuous sort, still followed by third place when two second

3. classification statistics (and display information)
    A B C                     
    - - ----------------------
    m 2 A                     
    n-A. 3                     
    m 2 A                     
    n-2 B                     
    n-B. 1                     
    X. 3 B                     
    X B 2                     
    . 4 XB                     
    H B. 3
   SELECT A, C, SUM (C) over (Partition by A) from T2               
   obtained results:
   A B C the SUM (C) the oVER (PARTITIONBYA)  

 - - ------- ------------------------
   H B. 3. 3                       
   m 2. 4 A                       
   m A 2. 4                       
   n-A. 6. 3                       
   n-2. 6 B                       
   . 6. 1 Nb                       
   X B. 3. 9                       
   X 2. 9 B                       
   X B. 4. 9                       
 
   if the sum, group by only the obtained
   A SUM (C)                     

- ----------------------
   H. 3                     
   m. 4                     
   n-. 6                     
   X. 9                     
   can not get the value in column B      
 
=====
SELECT * from Test

Data:
the ABC
. 1. 1. 1
. 1 2 2
. 1. 3. 3
2 2. 5
. 3. 4. 6

--- The C value of the same field corresponding to the bit value B column sum select a, b, c, SUM (C) OVER (PARTITION BY B) C_Sum from test

A B C C_SUM
1 1 1 1
1 2 2 7
2 2 5 7
1 3 3 3
3 4 6 6

--- If you do not already split the value of a field, and then use the null

eg: C is the value of the field on the back of each line summary

select a,b,c, SUM(C) OVER (PARTITION BY null) C_Sum from test

A B C C_SUM

1 1 1 17
1 2 2 17
1 3 3 17
2 2 5 17
3 4 6 17

Seeking individual wages as a percentage of wages department

SQL> select * from salary;

NAME DEPT SAL

---------- ---- -----

a 10 2000
b 10 3000
c 10 5000
d 20 4000

SQL> select name,dept,sal,sal*100/sum(sal) over(partition by dept) percent from salary;

NAME DEPT SAL PERCENT

---------- ---- ----- ----------

a 10 2000 20
b 10 3000 30
c 10 5000 50
d 20 4000 100

II: windowing function          
      windowing function specifies the data size analysis window function to work, the data window size may change with changes in the line, for example as follows:
. 1:    
   over (Order by salary) according accumulating salary sort, order by a default windowing function
   over (partition by deptno) according to the department partition
2:
  over (Order by the salary Range BETWEEN. 5 PRECEDING and. 5 following)
   corresponding to each row data window before the row amplitude value of not more than 5, then the line amplitude value no more than 5
   , for example: for the following

aa
     1
     2
     2
     2
     3
     4
     5
     6
     7
     9

 sum (aa) over (order by aa range between 2 preceding and 2 following)
   the result is
            AA SUM

 ---------------------- -------------------------------------------------------
            1                       10                                                     
            2                       14                                                     
            2                       14                                                     
            2                       14                                                     
            3                       18                                                     
            4                       18                                                     
            5                       22                                                     
            6                       18                                                               
            7                       22                                                               
            9                       9   

That is, for a line aa = 5, sum to 5-1 <= aa <= 5 + 2, and is for aa = 2, sum = 1 + 2 + 2 + 2 + 3 + 4 = 14; and if for aa = 9, 9-1 <= aa <= 9 + 2 9 only a few, so the sum = 9; 3: other: over (order by salary rows between 2 preceding and 4 following) corresponding to each row data window is before row 2, line 4 after 4: the following three statements are equivalent: over (order by salary rows between unbounded preceding and unbounded following) corresponding to each row of data from the first window to the last row, equivalent: over (order by salary range between unbounded preceding and unbounded following) equivalent over (partition by null)

 

Common analysis functions are listed below:

row_number() over(partition by ... order by ...) rank() over(partition by ... order by ...) dense_rank() over(partition by ... order by ...) count() over(partition by ... order by ...) max() over(partition by ... order by ...) min() over(partition by ... order by ...) sum() over(partition by ... order by ...) avg() over(partition by ... order by ...) first_value() over(partition by ... order by ...) last_value() over(partition by ... order by ...) lag() over(partition by ... order by ...) lead() over(partition by ... order by ...)

示例 SQL> select type,qty from test;

TYPE QTY

---------- ----------

1 6
2 9

SQL> select type,qty,to_char(row_number() over(partition by type order by qty))||'/'||to_char(count(*) over(partition by type)) as cnt2 from test;

TYPE QTY CNT2

---------- ---------- ------------
3 1/2
1 6 2/2
2 5 1/3
7 2/3
2 9 3/3

 SQL> select * from test;

---------- -------------------------------------------------
1 11111
2 22222
3 33333
4 44444

SQL> select t.id,mc,to_char(b.rn)||'/'||t.id)e 2 from test t, (select rownum rn from (select max(to_number(id)) mid from test) connect by rownum <=mid ))L 4 where b.rn<=to_number(t.id) order by id

ID MC TO_CHAR(B.RN)||'/'||T.ID

--------- -------------------------------------------------- ---------------------------------------------------
1 11111 1/1
2 22222 1/2
2 22222 2/2
3 33333 1/3
3 33333 2/3
3 33333 3/3
44444 1/4 44444 2/4
4 44444 3/4CNOUG4 44444 4/4

10 rows selected

*******************************************************************

About partition by

These are analytical functions, seems to be only 8.0 after the row_number () and rownum similar, more powerful point (can be sorted within each group open from 1) rank () is jumping sort, there are two second place Then there is the fourth (also in the various groups within) dense_rank () l is a continuous sort, still followed by third place when two second place. lag (arg1, arg2, arg3) compared row_number no duplicate values: arg1 is returned from the other rows are offset expression arg2 current row partition to be searched. Is a positive offset, back when a previous number of rows retrieved. arg3 value is returned when the number is outside the range represented arg2 packet.

1. select deptno,row_number() over(partition by deptno order by sal) from emp order by deptno;

2. select deptno,rank() over (partition by deptno order by sal) from emp order by deptno;

3. select deptno,dense_rank() over(partition by deptno order by sal) from emp order by deptno;

4. select deptno,ename,sal,lag(ename,1,null) over(partition by deptno order by ename) from emp ord er by deptno;

5. select deptno,ename,sal,lag(ename,2,'example') over(partition by deptno order by ename) from em p order by deptno;

6. select deptno, sal, sum (sal) over (partition by deptno) from emp; - recorded after each line has a total value select deptno, sum (sal) from emp group by deptno;

7. averaging wages and wage differentials for each department and each person's department

select deptno,ename,sal ,  

     round(avg(sal) over(partition by deptno)) as dept_avg_sal,

     round(sal-avg(sal) over(partition by deptno)) as dept_sal_diff

from emp;

Guess you like

Origin www.cnblogs.com/Mr-Simple001/p/10953767.html