T-SQL » Functions » SUM

Syntax:
SELECT SUM ( field ) [ AS expression ]
FROM table
[ WHERE criteria ]
[ GROUP BY grouping_expression ]
[ HAVING criteria ]
field
The name of the column that the sum of the values is to be taken.
expression
The name to be displayed for the resulting sum.
table
The name of the table the information is being retrieved from.
criteria
The condition(s) that dictate which rows are to be added together.
grouping_expression
An optional parameter that specifies a set of columns on which to group matching data.

The SUM function simply adds all of the values in a field and returns the total sum.

Examples

Code:
SELECT SUM(DaysInHospital) AS MCTotalDays
FROM MediCarePatients
WHERE age > 65;
Output:
MCTotalDays
497182

(1 row(s) affected)
Explanation:

This code finds the total number of days that MediCare patients stayed in the hospital and stores that information in 'MCTotalDays'.

Language(s): MS SQL Server

See Also: