Example of Oracle over () function usage

Source: Internet
Author: User

Oracle over () functions are commonly used functions. The following describes two examples of using Oracle over () functions () functions have a deeper understanding.

1. 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:

 
 
  1. 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 (%)

 
 
  1. ------------------------------------------------------------
  2. 20 ADAMS 1100 1100 29025
  3. 30 ALLEN 1600 2700 29025 5.51
  4. 30 BLAKE 2850 5550 29025 9.82
  5. 10 Clarke 2450 8000 29025 8.44
  6. 20 FORD 3000 11000 29025 10.34
  7. 30 JAMES 950 11950 29025 3.27
  8. 20 JONES 2975 14925 29025 10.25
  9. 10 KING 5000 19925 29025 17.23
  10. 30 MARTIN 1250 21175 29025 4.31
  11. 10 MILLER 1300 22475 29025 4.48
  12. 20 SCOTT 3000 25475 29025 10.34
  13. 20 SMITH 800 26275 29025 2.76
  14. 30 TURNER 1500 27775 29025 5.17
  15. 30 WARD 1250 29025 29025 4.31
  16.  
  17. 14 rows have been selected.
  18.  



2. The Oracle over () function uses subpartitions to find the continuous sum of salaries of each department. Note: partition by department. Note that the over (...) conditions are different,

 
 
  1. Sum (sal) over (partition by deptno order by ename) calculates the sum
  2. Sum (sal) over (partition by deptno) sum by department
  3. Sum (sal) over (order by deptno, ename) does not calculate the sum based on the Department's "continuous"
  4. Sum (sal) over () does not calculate the total number of all employees by department. The effect is equivalent to sum (sal ).
  5.  
  6. SQL> select deptno, ename, sal,
  7. Sum (sal) over (partition by deptno order by ename) Department continuous sum, -- department salary "continuous" sum
  8. Sum (sal) over (partition by deptno) Department sum, -- the sum of Department statistics, the sum of the same department unchanged
  9. 100 * round (sal/sum (sal) over (partition by deptno), 4) "department share (% )",
  10. Sum (sal) over (order by deptno, ename) continuous sum, -- the salary of all departments "continuous" sum
  11. Sum (sal) over () sum, -- sum (sal) over () is equivalent to sum (sal), total salary of all employees.
  12. 100 * round (sal/sum (sal) over (), 4) "total share (% )"
  13. From emp
  14. /

Deptno ename sal Department continuous sum Department total department share (%) total sum of continuous sum (%)

 
 
  1. ----------------------------------------------------------------------------
  2. 10 CLARK 2450 2450 8750 28 2450 29025 8.44
  3. KING 5000 7450 8750 57.14 7450 29025
  4. MILLER 1300 8750 8750 14.86 8750 29025 4.48
  5.  
  6. 20 ADAMS 1100 1100 10875 10.11 9850 29025
  7. FORD 3000 4100 10875 27.59 12850 29025
  8. JONES 2975 7075 10875 27.36 15825 29025 10.25
  9. SCOTT 3000 10075 10875 27.59 18825 29025
  10. SMITH 800 10875 10875 7.36 19625 29025 2.76
  11.  
  12. 30 ALLEN 1600 1600 9400 17.02 21225 29025 5.51
  13. BLAKE 2850 4450 9400 30.32 24075 29025 9.82
  14. JAMES 950 5400 9400 10.11 25025 29025 3.27
  15. MARTIN 1250 6650 9400 13.3 26275 29025
  16. TURNER 1500 8150 9400 15.96 27775 29025 5.17
  17. WARD 1250 9400 9400 13.3 29025 29025
  18.  
  19. 14 rows have been selected.

Use of Oracle to_char Functions

Implementation of oracle function return table

Learn about the Oracle FBI Index

How to uninstall an Oracle database in Windows

Multiple table Connection Methods in Oracle

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.