This SQL Server tutorial explains how to use the SUM functions in SQL Server (Transact-SQL) with syntax and examples.
Description
In SQL Server (Transact-SQL), the SUM function returns the summed value of an expression.
Syntax
The syntax for the SUM function in SQL Server (Transact-SQL) is:
SELECT SUM(aggregate_expression) FROM tables [WHERE conditions];
OR the syntax for the SUM function when grouping the results by one or more columns is:
SELECT expression1, expression2, ... expression_n, SUM(aggregate_expression) FROM tables [WHERE conditions] GROUP BY expression1, expression2, ... expression_n;
Parameters or Arguments
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.
Applies To
The SUM function can be used in the following versions of SQL Server (Transact-SQL):
- SQL Server 2017, SQL Server 2016, SQL Server 2014, SQL Server 2012, SQL Server 2008 R2, SQL Server 2008, SQL Server 2005
Example – With Single Field
Let’s look at some SQL Server SUM function examples and explore how to use the SUM function in SQL Server (Transact-SQL).
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.
Example – Using DISTINCT
You can use the DISTINCT clause within the SUM function. For example, the SQL statement below returns the combined total salary of unique salary values where the salary is below $29,000 / year.
SELECT SUM(DISTINCT salary) AS "Total Salary" FROM employees WHERE salary < 29000;
If there were two salaries of $24,000/year, only one of these values would be used in the SUM function.
Example – Using Formula
The expression contained within the SUM functions does not need to be a single field. You could also use a formula. For example, you might want to calculate the total commission.
SELECT SUM(sales * 0.03) AS "Total Commission" FROM orders;
Example – Using GROUP BY
In some cases, you will be required to use the GROUP BY clause with the SUM function.
For example, you could also use the SUM function to return the name of the department and the total quantity (in the associated department) where the quantity is over 10.
SELECT department, SUM(quantity) AS "Total Quantity" FROM products WHERE quantity > 10 GROUP BY department;
Because you have listed one column in your SELECT statement that is not encapsulated in the SUM function, you must use a GROUP BY clause. The department field must, therefore, be listed in the GROUP BY section.
Pingback: SQL Server: GROUP BY Clause - Adglob Infosystem Pvt Ltd
Pingback: SQL Server: HAVING Clause - Adglob Infosystem Pvt Ltd