Hàm SUM – Transact-SQL – Microsoft SQL Server
Hàm SUM – Transact-SQL – Microsoft SQL Server là một trong các hàm trong danh sách được cung cấp sẵn cho người sử dụng khi thao tác với hệ quản trị cơ sở dữ liệu Microsoft SQL Server.
Hàm SUM – Transact-SQL – Microsoft SQL Server – Chức năng
In SQL Server (Transact-SQL), the SUM function returns the summed value of an expression.
Hàm SUM – Transact-SQL – Microsoft SQL Server – Cú pháp
Cú pháp Hàm SUM trong Transact-SQL – Microsoft SQL Server như sau :
SELECT SUM(aggregate_expression) FROM tables [WHERE conditions];
Tham số
- expression1, expression2, … expression_n
- Expressions that are not encapsulated within the SUM function and must be included in the GROUP BY clause at the end of the SQL statement.
- aggregate_expression
- This is the column or expression that will be summed.
- tables
- The tables that you wish to retrieve records from. There must be at least one table listed in the FROM clause.
- WHERE conditions
- Optional. These are conditions that must be met for the records to be selected.
Hàm SUM – Transact-SQL – Microsoft SQL Server – Phạm vi
Hàm SUM có thể sẵn sàng sử dụng trong Transact-SQL – Microsoft SQL Server với các phiên bản Microsoft SQL Server như sau:
SQL Server 2014, SQL Server 2012, SQL Server 2008 R2, SQL Server 2008, SQL Server 2005
Hàm SUM – Transact-SQL – Microsoft SQL Server – Ví dụ
Dưới đây là các ví dụ minh họa cách sử dụng hàm SUM trong Transact-SQL – Microsoft SQL Server như sau:
Example – With Single Field
For example, you might wish to know how the combined total quantity of all products whose quantity is greater than 10.
SELECT SUM(quantity) AS "Total Quantity" FROM products WHERE quantity > 10;
In this SUM function example, we’ve aliased the SUM(quantity) expression as "Total Quantity". As a result, "Total Quantity" will display as the field name when the result set is returned.