The SUM aggregate function allows selecting the total for a numeric column.
The SUM syntax is displayed below:
|
SELECT SUM(Column1)
FROM Table1 |
We are going to use the Sales table to illustrate the use of SUM clause:
Sales:
|
CustomerID
|
Date
|
SaleAmount
|
|
2
|
5/6/2004
|
$100.22
|
|
1
|
5/7/2004
|
$99.95
|
|
3
|
5/7/2004
|
$122.95
|
|
3
|
5/13/2004
|
$100.00
|
|
4
|
5/22/2004
|
$555.55
|
Consider the following SUM statement:
|
SELECT SUM(SaleAmount)
FROM Sales |
This SQL statement will return the sum of all SaleAmount fields and the result of it will be:
|
SaleAmount
|
|
$978.67
|
Of course you can specify search criteria using the WHERE clause in your SUM statement. If you want to select the total sales for customer with CustomerID = 3, you will use the following SUM statement:
|
SELECT SUM(SaleAmount)
FROM Sales WHERE CustomerID = 3 |
The result will be:
|
SaleAmount
|
|
$222.95
|
No comments:
Post a Comment