Thursday, July 15, 2010

SQL SUM() Function


THE SUM() FUNCTION

The SUM() function returns the total sum of a numeric column.

SQL SUM() Syntax

SELECT SUM(column_name) FROM table_name



SQL SUM() EXAMPLE

We have the following "Orders" table:
O_IdOrderDateOrderPriceCustomer
12008/11/121000Hansen
22008/10/231600Nilsen
32008/09/02700Hansen
42008/09/03300Hansen
52008/08/302000Jensen
62008/10/04100Nilsen
Now we want to find the sum of all "OrderPrice" fields".
We use the following SQL statement:
SELECT SUM(OrderPrice) AS OrderTotal FROM Orders
The result-set will look like this:
OrderTotal
5700

No comments:

Post a Comment