Home > Database > Oracle > How to use sum function in oracle

How to use sum function in oracle

下次还敢
Release: 2024-04-30 06:54:16
Original
718 people have browsed it

The SUM function calculates the sum of a series of values ​​in Oracle. Syntax: SUM(expression), where expression is a numeric expression or column name. It can sum all row values ​​in a column, or sum rows that meet a specific condition via a WHERE clause. The SUM function returns a value of type NUMBER and returns a positive number even if the argument is negative. It supports advanced usage, such as with GROUP BY and HAVING clauses, and as a window function.

How to use sum function in oracle

The SUM function in Oracle

Definition

The SUM function is in Used in Oracle to calculate the sum of a set of values ​​or expressions.

Syntax

<code>SUM(expression)</code>
Copy after login

Where:

  • expression: The numeric expression or column name to calculate the sum.

Usage

  • #The SUM function sums all row values ​​in a column.
  • You can also use the SUM function in the WHERE clause to sum rows that meet specific conditions.
  • The return value of the SUM function is the data type NUMBER.

Example

<code>-- 计算所有员工的薪水总和
SELECT SUM(salary) FROM employees;

-- 计算部门 ID 为 10 的所有员工的薪水总和
SELECT SUM(salary) FROM employees WHERE department_id = 10;

-- 计算大于 5000 美元的薪水总和
SELECT SUM(salary) FROM employees WHERE salary > 5000;</code>
Copy after login

Note

  • If the parameter of the SUM function is NULL, it will be returned The value is NULL.
  • If the parameter of the SUM function is a negative number, the return value is still a positive number.
  • The SUM function can be applied to both numbers and strings. If you want to sum strings, the strings must be enclosed in single quotes.

Advanced Usage

In addition to basic usage, the SUM function also supports some advanced usage:

  • GROUP BY clause: The SUM function can be used with the GROUP BY clause to sum grouped data.
  • HAVING clause: The SUM function can be used with the HAVING clause to sum groupings that meet certain conditions.
  • Window function: The SUM function can be used as a window function to calculate the sum of values ​​within the window.

The above is the detailed content of How to use sum function in oracle. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template