SUM returns the sum of a number of value.
This function ignores NULL values.
This example returns the sum of all orders.
SELECT SUM(TotalAmount) AS Total
FROM [Order]
| Total |
|---|
| 1354458.59 |
Syntax of the SUM function.
SUM(value)
value -- a value, numeric column, or subquery.
| ORDER |
|---|
| Id |
| OrderDate |
| OrderNumber |
| CustomerId |
| TotalAmount |
SELECT MONTH(OrderDate) AS Month
SUM(TotalAmount) AS 'Total Sales'
FROM [Order]
WHERE YEAR(OrderDate) = 2013
GROUP BY MONTH(OrderDate)
| Month | Total Sales |
|---|---|
| 1 | 66692.80 |
| 2 | 41207.20 |
| 3 | 39979.90 |
| 4 | 55699.39 |
| 5 | 56823.70 |
![]() |
|
| ORDER |
|---|
| Id |
| OrderDate |
| OrderNumber |
| CustomerId |
| TotalAmount |
| ORDERITEM |
|---|
| Id |
| OrderId |
| ProductId |
| UnitPrice |
| Quantity |
SELECT SUM(UnitPrice * Quantity) AS 'Total Sales'
FROM [Order] O
JOIN OrderItem I ON O.Id = I.OrderId
WHERE I.ProductId = 14 AND YEAR(OrderDate) = 2014
| Total Sales |
|---|
| 488.25 |