Example of using the over function in Oracle
Sum (sal) over (partition by deptno order by ename) Department continuous sum, -- the salary of each department "continuous" sum,
Environment: windows 2000 server + Oracle8.1.7 + SQL * plus
Objective: To use the scott model provided by oracle as the testing environment and analyze the usage of functions through testing.
Usage similar to sum (...) over...
1. Original table information:
SQL> break on deptno skip 1 -- display the data segments of different departments for better results.
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 1, 3000
JONES 2975
30 ALLEN 1600
BLAKE 2850
MARTIN 1250
JAMES 950
TURNER 1500
WARD 1250
14 rows have been selected.
2. Let's take a simple look. Note that the over (...) conditions are different,
Use sum (sal) over (order by ename)... to query the "continuous" sum of employees' salaries,
Note that if the over (order by ename) clause does not have an order by clause, the sum is not "continuous,
Put them together and try the following differences:
SQL> break on ''-- cancel the display of data segments
SQL> select deptno, ename, sal,
2 sum (sal) over (order by ename) continuous summation,
3 sum (sal) over () sum, -- sum (sal) over () is equivalent to sum (sal)
4 100 * round (sal/sum (sal) over (), 4) "share (% )"
5 from emp
6/
Deptno ename sal sum of continuous sum (%)
------------------------------------------------------------
20 ADAMS 1100 1100 29025
30 ALLEN 1600 2700 29025 5.51
30 BLAKE 2850 5550 29025 9.82
10 Clarke 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
14 rows have been selected.
3. Use sub-partitions to find out the continuous sum of salaries of each department. Note: partition by department. Note that the over (...) conditions are different,
Sum (sal) over (partition by deptno order by ename) calculates the sum
Sum (sal) over (partition by deptno) sum by department
Sum (sal) over (order by deptno, ename) does not calculate the sum based on the Department's "continuous"
Sum (sal) over () does not calculate the total number of all employees by department. The effect is equivalent to sum (sal ).
SQL> break on deptno skip 1 -- display the data segments of different departments for better results.
SQL> select deptno, ename, sal,
2 sum (sal) over (partition by deptno order by ename) Department continuous sum, -- department salary "continuous" sum
3 sum (sal) over (partition by deptno) Department sum, -- the sum of Department statistics, the sum of the same department unchanged
4 100 * round (sal/sum (sal) over (partition by deptno), 4) "department share (% )",
5 sum (sal) over (order by deptno, ename) continuous sum, -- the salary of all departments "continuous" sum
6 sum (sal) over () sum, -- sum (sal) over () is equivalent to sum (sal), total salary of all employees.
7 100 * round (sal/sum (sal) over (), 4) "total share (% )"
8 from emp
9/
Edit recommendations]