select id,area,
sum (1) over () as the total number of records,
sum (1) over (partition by id) as the number of packets recorded
sum (score) over () as total,
sum (score) over (partition by id) as packet summation,
sum (score) over (order by id) as a continuous summation packet,
sum (score) over (partition by id, area) as the packet ID and area sum,
sum (score) over (partition by id order by area) as the packet ID and the continuous area by summation,
max (score) over () as the maximum value,
max (score) over (partition by id) as the maximum value of the packet,
max (score) over (order by id) as the maximum consecutive packets,
max (score) over (partition by id, area) as the packet ID and area selecting the maximum value,
max (score) over (partition by id order by area) as the packet ID and the continuous area by selecting the maximum value,
avg (score) over () as the average of all,
avg (score) over (partition by id) as the average packet,
avg (score) over (order by id) as a running average packet,
avg (score) over (partition by id, area) as the packet ID and area average,
avg (score) over (partition by id order by area) as the packet ID and the continuous press area average,
RATIO_TO_REPORT (score) over () as "% cent of all"
RATIO_TO_REPORT(score) over(partition by id) as "占分组%",
score from students;
3, LAG (COL, n, default), LEAD (OL, n, default) - N data on upstream side
Take values previously recorded: lag (score, n, x) over (order by id)
Value is taken back recorded: lead (score, n, x) over (order by id)
Parameters: n represents N mobile records, X represents absence of padding values, iD indicates the sort column
select id,lag(score,1,0) over(order by id) lg,score from students;
select id,lead(score,1,0) over(order by id) lg,score from students;
4、FIRST_VALUE()、LAST_VALUE()
1 taken on the starting line value: first_value (score, n) over (order by id)
1 taken on the last row values: LAST_value (score, n) over (order by id)
select id,first_value(score) over(order by id) fv,score from students;
select id,last_value(score) over(order by id) fv,score from students;
sum(...) over ...
[Function] continuous summation analysis functions
Specific examples of parameters Parameters
[Description] Oracle analytical functions
NC Example:
select bdcode,sum(1) over(order by bdcode) aa from bd_bdinfo
[Example]
Table 1. Original information: SQL> break on deptno skip 1 - is a more effective, the data of the different sectors spacer segment display.
SQL> select deptno,ename,sal
2 from emp
3 order by deptno;
DEPTNO ENAME SAL
---------- ---------- ----------
10 CLARK 2450
KING 5000
MILLER 1300
20 SMITH 800
ADAMS 1100
FORD 3000
SCOTT 3000
JONES 2975
30 ALLEN 1600
BLAKE 2850
MARTIN 1250
JAMES 950
TURNER 1500
WARD 1250
2. a simple first-come, pay attention over (...) different conditions,
Use sum (sal) over (order by ename) ... query salaries of employees of "continuous" sum,
Note over (order by ename) if there is no order by clause, the sum is not "continuous", and
Put together to experience the difference:
SQL> select deptno,ename,sal,
2 sum (sal) over (order by ename) continuous summation,
3 sum (sal) over () sum, - where sum (sal) over () is equivalent to the sum (sal)
4 100*round(sal/sum(sal) over (),4) "份额(%)"
5 from emp
6 /
DEPTNO ENAME SAL continuously summing the total share (%)
---------- ---------- ---------- ---------- ---------- ----------
20 ADAMS 1100 1100 29025 3.79
30 ALLEN 1600 2700 29025 5.51
30 BLAKE 2850 5550 29025 9.82
10 CLARK 2450 8000 29025 8.44
20 FORD 3000 11000 29025 10.34
30 JAMES 950 11950 29025 3.27
20 JONES 2975 14925 29025 10.25
10 KING 5000 19925 29025 17.23
30 MARTIN 1250 21175 29025 4.31
10 MILLER 1300 22475 29025 4.48
20 SCOTT 3000 25475 29025 10.34
20 SMITH 800 26275 29025 2.76
30 TURNER 1500 27775 29025 5.17
30 WARD 1250 29025 29025 4.31
3. Using Child Partition find out the sum of the salary consecutive sectors. Note by sector partition. Note over (...) different conditions,
sum (sal) over (partition by deptno order by ename) by sector "continuous" for obtaining the sum
sum (sal) over (partition by deptno) for obtaining the sum by sector
sum (sal) over (order by deptno, ename) by sector not "continuous" for obtaining the sum
sum (sal) over () not by sector, for obtaining the sum of all employees, the effect is equivalent to the sum (sal).
SQL> select deptno,ename,sal,
2 sum (sal) over (partition by deptno order by ename) department for sums - each department's salary "continuous" sum
The sum of the sector 3 sum (sal) over (partition by deptno), - statistics department of the sum of the sum of the same sector unchanged
4 100 * round (sal / sum (sal) over (partition by deptno), 4) "sector share (%)"
5 sum (sal) over (order by deptno, ename) continuous summation - all sectors of salary "continuous" sum
6 sum (sal) over () sum, - where sum (sal) over () is equivalent to the sum (sal), the sum of all employees salary
7 100 * round (sal / sum (sal) over (), 4) "total share (%)"
8 from emp
9 /
DEPTNO ENAME SAL department for summing department sum sector share (%) share of the total sum of continuous sum (%)
------ ------ ----- ------------ ---------- ----------- ---------- ------ ----------
10 CLARK 2450 2450 8750 28 2450 29025 8.44
KING 5000 7450 8750 57.14 7450 29025 17.23
MILLER 1300 8750 8750 14.86 8750 29025 4.48
20 ADAMS 1100 1100 10875 10.11 9850 29025 3.79
FORD 3000 4100 10875 27.59 12850 29025 10.34
JONES 2975 7075 10875 27.36 15825 29025 10.25
SCOTT 3000 10075 10875 27.59 18825 29025 10.34
SMITH 800 10875 10875 7.36 19625 29025 2.76
30 ALLEN 1600 1600 9400 17.02 21225 29025 5.51
BLAKE 2850 4450 9400 30.32 24075 29025 9.82
JAMES 950 5400 9400 10.11 25025 29025 3.27
MARTIN 1250 6650 9400 13.3 26275 29025 4.31
TURNER 1500 8150 9400 15.96 27775 29025 5.17
WARD 1250 9400 9400 13.3 29025 29025 4.31
4. to a comprehensive example of sum rule there by sector partition, there is no partition examples
SQL> select deptno,ename,sal,sum(sal) over (partition by deptno order by sal) dept_sum,
2 sum(sal) over (order by deptno,sal) sum
3 from emp;
DEPTNO ENAME SAL DEPT_SUM SUM
---------- ---------- ---------- ---------- ----------
10 MILLER 1300 1300 1300
CLARK 2450 3750 3750
KING 5000 8750 8750
20 SMITH 800 800 9550
ADAMS 1100 1900 10650
JONES 2975 4875 13625
SCOTT 3000 10875 19625
FORD 3000 10875 19625
30 JAMES 950 950 20575
WARD 1250 3450 23075
MARTIN 1250 3450 23075
TURNER 1500 4950 24575
ALLEN 1600 6550 26175
BLAKE 2850 9400 29025
5. to a reverse order, that is, in descending order department, department of each employee's salary from highest to lowest, cumulative and the rules unchanged.
SQL> select deptno,ename,sal,
2 sum(sal) over (partition by deptno order by deptno desc,sal desc) dept_sum,
3 sum(sal) over (order by deptno desc,sal desc) sum
4 from emp;
DEPTNO ENAME SAL DEPT_SUM SUM
---------- ---------- ---------- ---------- ----------
30 BLAKE 2850 2850 2850
ALLEN 1600 4450 4450
TURNER 1500 5950 5950
WARD 1250 8450 8450
MARTIN 1250 8450 8450
JAMES 950 9400 9400
20 SCOTT 3000 6000 15400
FORD 3000 6000 15400
JONES 2975 8975 18375
ADAMS 1100 10075 19475
SMITH 800 10875 20275
10 KING 5000 5000 25275
CLARK 2450 7450 27725
MILLER 1300 8750 29025
6. Experience: "from emp ...;" in the back do not add an order by clause analysis functions used (partition by deptno order by sal)
There are already a sort of sentence, if the end of the sentence add the sort clause down fills consistent, inconsistent, the result is very strenuous. Such as:
SQL> select deptno,ename,sal,sum(sal) over (partition by deptno order by sal) dept_sum,
2 sum(sal) over (order by deptno,sal) sum
3 from emp
4 order by deptno desc;
DEPTNO ENAME SAL DEPT_SUM SUM
---------- ---------- ---------- ---------- ----------
30 JAMES 950 950 20575
WARD 1250 3450 23075
MARTIN 1250 3450 23075
TURNER 1500 4950 24575
ALLEN 1600 6550 26175
BLAKE 2850 9400 29025
20 SMITH 800 800 9550
ADAMS 1100 1900 10650
JONES 2975 4875 13625
SCOTT 3000 10875 19625
FORD 3000 10875 19625
10 MILLER 1300 1300 1300
CLARK 2450 3750 3750
KING 5000 8750 8750