Use sum (SUM) Function
The sum function is used to return the sum of all values in the expression. This function is usually used to summarize some data.
Syntax:
Sum ([All | distinct] expression)
Parameter description:
L all: Performs aggregate function operations on all values. All is the default setting.
L distinct: Specify sum to return the sum of unique values.
L expression: constant, column, or function, or any combination of arithmetic, bitwise, string, and other operators. Expression is an expression of exact or approximate numeric data type classification (except BIT data type. Aggregate functions and subqueries are not allowed.
The usage of the sum function is described as follows:
(1) fields with indexes can accelerate the operation of Aggregate functions.
(2) The sum function can be used only for fields of int, smallint, tinyint, decimal, numeric, float, real, money, and smallmoney data types.
(3) when using the sum function, SQL Server treats smallint or tinyint In the result set as Int.
(4) when using the sum function, SQL Server will ignore null values (null), that is, these null values are not calculated during calculation.
The following two sum functions are used to calculate the total sales quantity and total amount respectively, and the calculation result is displayed.
The procedure is as follows:
(1) Select "start"> "All Programs"> "Microsoft SQL Server"> "query analyzer" in the operating system to open the query analyzer.
(2) In the query analyzer toolbar, select the database to connect to. Here, select "Sales Management System ".
(3) write the following code in the code editing area.
The SQL statement is as follows:
Use Sales Management System
Select * from sales table
Select sum (Quantity) as total number of products sold, sum (amount) as total amount of goods sold from sales table