SQL Server uses the sum function

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.