You are on page 1of 49

09/03/13

Built-in F unctions - A ggregate F unctions - S Q LS erv erP edia

Search SQLServerPedia

Wiki

Pulse

Blog

Project Lucy

Podcasts

Twitter

Contributors

Builtin Functions Log in Aggregate Functions


See Also: Main_Page Transact SQL Coding Techniques Reusable Coding Techniques Functions SQL Server 2005 BuiltIns Aggregate functions return a single value summarizing a given data set. All aggregate functions are

deterministic.
sqlserv erpedia.com/w iki/Built-in_F unctions_-_A ggregate_F unctions 1/49

09/03/13

Built-in F unctions - A ggregate F unctions - S Q LS erv erP edia

NOTE: AVG, SUM, STDEV, STDEVP, VAR and VARP functions cannot operate on BIT data types; they can operate on all other numeric data types.

Contents
[hide] 1 2 3 4 5 6 7 8 9 10 11 12 13 14

COUNT Function
sqlserv erpedia.com/w iki/Built-in_F unctions_-_A ggregate_F unctions 2/49

09/03/13

Built-in F unctions - A ggregate F unctions - S Q LS erv erP edia

The COUNT function returns a count of rows based on certain criteria. The syntax is: 1 .

Keyword ALL is optional and is assumed by default. If you specify a "*" as the criterion, COUNT returns the total number of rows in a table; for
sqlserv erpedia.com/w iki/Built-in_F unctions_-_A ggregate_F unctions 3/49

09/03/13

Built-in F unctions - A ggregate F unctions - S Q LS erv erP edia

example, the following query counts rows in the FactFinance table of Adventure Works DW database: 1 . 2 . 3 . 4 . 5 .

6 .

If you join multiple tables then COUNT(*) returns the number of rows satisfying the join criterion, as in the following:
sqlserv erpedia.com/w iki/Built-in_F unctions_-_A ggregate_F unctions 4/49

09/03/13

Built-in F unctions - A ggregate F unctions - S Q LS erv erP edia

1 . 2 .

3 .

COUNT(*) cannot be used with DISTINCT; nor can you specify any other parameter this variation of the function automatically counts every single row in a single or multiple joined tables. Unlike all other aggregate functions, COUNT does not
sqlserv erpedia.com/w iki/Built-in_F unctions_-_A ggregate_F unctions 5/49

09/03/13

Built-in F unctions - A ggregate F unctions - S Q LS erv erP edia

ignore NULL values. If you need to find the count of unique items within a column in a table use COUNT (DISTINCT syntax. For example, the following query counts unique organization keys within the FactFinance table: 1 . 2 . 3 . 4 . 5 .

column_name

sqlserv erpedia.com/w iki/Built-in_F unctions_-_A ggregate_F unctions

6/49

09/03/13

Built-in F unctions - A ggregate F unctions - S Q LS erv erP edia

6 .

COUNT_ Function
The COUNT_BIG function is identical to the COUNT function, but returns a BIGINT data type, whereas COUNT returns an INT. The upper limit for INT data type is 2 billion; therefore if you anticipate counting more than two billion rows use the COUNT_BIG
sqlserv erpedia.com/w iki/Built-in_F unctions_-_A ggregate_F unctions 7/49

09/03/13

Built-in F unctions - A ggregate F unctions - S Q LS erv erP edia

function. Any attempt to use the COUNT function when counting over 2 billion rows returns an error.

MAX Function
The MAX function returns the biggest value within a given set. The syntax is: 1 .

The ALL keyword is optional and is the default


sqlserv erpedia.com/w iki/Built-in_F unctions_-_A ggregate_F unctions 8/49

09/03/13

Built-in F unctions - A ggregate F unctions - S Q LS erv erP edia

unless DISTINCT is specified. The DISTINCT keyword specifies that each unique value should be considered. This keyword really has no use with MAX function since it returns a single value; however the keyword is supported for ANSI compatibility.

For example, the following returns the greatest amount from


sqlserv erpedia.com/w iki/Built-in_F unctions_-_A ggregate_F unctions 9/49

09/03/13

Built-in F unctions - A ggregate F unctions - S Q LS erv erP edia

the FactFinance table: 1 . 2 . 3 . 4 . 5 .

6 .

Note that the parameter of MAX can be any valid expression, including string columns, as in the following: 1 . 2 . 3 . "Women's Tights, S" is
sqlserv erpedia.com/w iki/Built-in_F unctions_-_A ggregate_F unctions 10/49

09/03/13

Built-in F unctions - A ggregate F unctions - S Q LS erv erP edia

returned because it is the last value in the ordered list of product names, not because it is the longest product name. You can combine MAX function with LEN function to return the products with the longest names, as follows: 0 1 . 0 2 . 0 3 . 0 4 .

alphabetically

0 5 . 0 6 .

sqlserv erpedia.com/w iki/Built-in_F unctions_-_A ggregate_F unctions

11/49

09/03/13

Built-in F unctions - A ggregate F unctions - S Q LS erv erP edia

0 7 .

0 8 .

0 9 .

1 0 .

1 1 .

sqlserv erpedia.com/w iki/Built-in_F unctions_-_A ggregate_F unctions

12/49

09/03/13

Built-in F unctions - A ggregate F unctions - S Q LS erv erP edia

MIN Function
The MIN function returns the smallest value within a given set. The syntax is: 1 .

The ALL keyword is optional and is the default unless DISTINCT is specified. The DISTINCT keyword specifies that each unique value should be considered. This keyword really
sqlserv erpedia.com/w iki/Built-in_F unctions_-_A ggregate_F unctions 13/49

09/03/13

Built-in F unctions - A ggregate F unctions - S Q LS erv erP edia

has no use with MIN function since it returns a single value; however the keyword is supported for ANSI compatibility.

For example, the following returns the least amount from the FactFinance table: 1 . 2 . 3 . 4 . 5 .

sqlserv erpedia.com/w iki/Built-in_F unctions_-_A ggregate_F unctions

14/49

09/03/13

Built-in F unctions - A ggregate F unctions - S Q LS erv erP edia

6 .

Note that the parameter of MIN can be any valid expression, including string columns, as in the following: 1 . 2 . 3 . 4 . 5 . 6 .

sqlserv erpedia.com/w iki/Built-in_F unctions_-_A ggregate_F unctions

15/49

09/03/13

Built-in F unctions - A ggregate F unctions - S Q LS erv erP edia

7 .

Note that "Adjustable Race" is returned because it is the first value in the ordered list of product names, not because it is the shortest product name. You can combine MIN function with LEN function to
sqlserv erpedia.com/w iki/Built-in_F unctions_-_A ggregate_F unctions 16/49

alphabetically

09/03/13

Built-in F unctions - A ggregate F unctions - S Q LS erv erP edia

return the products with the shortest names, as follows: 0 1 . 0 2 . 0 3 . 0 4 .

0 5 . 0 6 . 0 7 . 0 8 . 0 9 . 1 0 .

sqlserv erpedia.com/w iki/Built-in_F unctions_-_A ggregate_F unctions

17/49

09/03/13

Built-in F unctions - A ggregate F unctions - S Q LS erv erP edia

1 1 .

AVG Function
The AVG function returns the average of the values within a column. Unlike MIN and MAX, AVG can only accept a numeric expression as a parameter. The syntax is: 1 .

ALL keyword is optional and is assumed


sqlserv erpedia.com/w iki/Built-in_F unctions_-_A ggregate_F unctions 18/49

09/03/13

Built-in F unctions - A ggregate F unctions - S Q LS erv erP edia

by default. You can specify the DISTINCT keyword if you want to take into account only the unique occurrence of each value. The following query returns the average amount from FactFinance table: 1 . 2 . 3 . 4 . 5 . 6 .

7 .

The next example


sqlserv erpedia.com/w iki/Built-in_F unctions_-_A ggregate_F unctions 19/49

09/03/13

Built-in F unctions - A ggregate F unctions - S Q LS erv erP edia

uses DISTINCT keyword to return the average of distinct values: 1 . 2 . 3 . 4 . 5 . 6 .

7 .

The AVG function returns the same (or a similar) data type as the group of values it accepts. So if you pass an integer, expect
sqlserv erpedia.com/w iki/Built-in_F unctions_-_A ggregate_F unctions 20/49

09/03/13

Built-in F unctions - A ggregate F unctions - S Q LS erv erP edia

an integer back; if you examine float values with the AVG function you will get a float value back.

SUM Function
The SUM function returns the sum of all or unique values. Unlike MIN and MAX, SUM can only accept a numeric expression as a
sqlserv erpedia.com/w iki/Built-in_F unctions_-_A ggregate_F unctions 21/49

09/03/13

Built-in F unctions - A ggregate F unctions - S Q LS erv erP edia

parameter. The syntax is: 1 .

ALL keyword is optional and is assumed by default. You can specify the DISTINCT keyword if you want to take into account only the unique occurrence of each value.

The following query returns the sum of amounts


sqlserv erpedia.com/w iki/Built-in_F unctions_-_A ggregate_F unctions 22/49

09/03/13

Built-in F unctions - A ggregate F unctions - S Q LS erv erP edia

from FactFinance table: 1 . 2 . 3 . 4 . 5 . 6 .

7 .

The next example uses DISTINCT keyword to return the sum of distinct values: 1 . 2 . 3 . 4 . 5 . 6 .

7 .

The SUM function returns the


sqlserv erpedia.com/w iki/Built-in_F unctions_-_A ggregate_F unctions 23/49

09/03/13

Built-in F unctions - A ggregate F unctions - S Q LS erv erP edia

same (or a similar) data type as the group of values it accepts. So if you pass an integer, expect an integer back; if you examine float values with the SUM function you will get a float value back.

STDEV Function
The STDEV function calculates the standard
sqlserv erpedia.com/w iki/Built-in_F unctions_-_A ggregate_F unctions 24/49

09/03/13

Built-in F unctions - A ggregate F unctions - S Q LS erv erP edia

deviation for all items in the SELECT statement. This function can only be used with numeric columns. The syntax is: 1 .

ALL keyword is optional and is assumed by default. You can specify the DISTINCT keyword if you want to take into account only
sqlserv erpedia.com/w iki/Built-in_F unctions_-_A ggregate_F unctions 25/49

09/03/13

Built-in F unctions - A ggregate F unctions - S Q LS erv erP edia

the unique occurrence of each value. The STDEV function always returns a FLOAT data type value.

The following example returns the standard deviation of amounts from FactFinance table: 1 . 2 . 3 . 4 . 5 . 6 .

7 .
sqlserv erpedia.com/w iki/Built-in_F unctions_-_A ggregate_F unctions 26/49

09/03/13

Built-in F unctions - A ggregate F unctions - S Q LS erv erP edia

STDEVP Function
The STDEVP function calculates the standard deviation for the population of items in the SELECT statement. This function can only be used with numeric columns. The syntax is: 1 .

ALL keyword is optional and is assumed by default. You


sqlserv erpedia.com/w iki/Built-in_F unctions_-_A ggregate_F unctions 27/49

09/03/13

Built-in F unctions - A ggregate F unctions - S Q LS erv erP edia

can specify the DISTINCT keyword if you want to take into account only the unique occurrence of each value. The STDEVP function always returns a FLOAT data type value.

The following example returns the standard deviation of population for amounts in the FactFinance table: 1 . 2 . 3 .
sqlserv erpedia.com/w iki/Built-in_F unctions_-_A ggregate_F unctions 28/49

09/03/13

Built-in F unctions - A ggregate F unctions - S Q LS erv erP edia

4 . 5 . 6 .

7 .

VAR Function
The VAR function calculates the statistical variance of all values in the SELECT statement. This function can only be used with numeric columns. The syntax is: 1 .

sqlserv erpedia.com/w iki/Built-in_F unctions_-_A ggregate_F unctions

29/49

09/03/13

Built-in F unctions - A ggregate F unctions - S Q LS erv erP edia

ALL keyword is optional and is assumed by default. You can specify the DISTINCT keyword if you want to take into account only the unique occurrence of each value. The VAR function always returns a FLOAT data type value.

The following example returns the


sqlserv erpedia.com/w iki/Built-in_F unctions_-_A ggregate_F unctions 30/49

09/03/13

Built-in F unctions - A ggregate F unctions - S Q LS erv erP edia

variance of amounts from FactFinance table: 1 .

2 . 3 . 4 .

5 .

VARP Function
The VARP function calculates the statistical variance for the population of values in the SELECT statement. This function can only
sqlserv erpedia.com/w iki/Built-in_F unctions_-_A ggregate_F unctions 31/49

09/03/13

Built-in F unctions - A ggregate F unctions - S Q LS erv erP edia

be used with numeric columns. The syntax is: 1 .

ALL keyword is optional and is assumed by default. You can specify the DISTINCT keyword if you want to take into account only the unique occurrence of each value. The VARP function always returns a
sqlserv erpedia.com/w iki/Built-in_F unctions_-_A ggregate_F unctions 32/49

09/03/13

Built-in F unctions - A ggregate F unctions - S Q LS erv erP edia

FLOAT data type value.

The following example returns the variance for the population of amounts from the FactFinance table: 1 .

2 . 3 . 4 .

5 .

CHECKS Function
The function can be
sqlserv erpedia.com/w iki/Built-in_F unctions_-_A ggregate_F unctions 33/49

CHECKSUM_

09/03/13

Built-in F unctions - A ggregate F unctions - S Q LS erv erP edia

used to detect data changes in a table. This function can only work on integer data type; otherwise the functionality is very similar to other aggregate functions like SUM or AVG. The syntax is: 1 .

ALL keyword is optional and is assumed by default.


sqlserv erpedia.com/w iki/Built-in_F unctions_-_A ggregate_F unctions 34/49

09/03/13

Built-in F unctions - A ggregate F unctions - S Q LS erv erP edia

You can specify the DISTINCT keyword if you want to take into account only the unique occurrence of each value. The function always returns a INT data type value.

CHECKSUM_

The following query returns the checksum of amount column in FactFinance table; then it updates some rows
sqlserv erpedia.com/w iki/Built-in_F unctions_-_A ggregate_F unctions 35/49

09/03/13

Built-in F unctions - A ggregate F unctions - S Q LS erv erP edia

in the table; the second checksum is different from the first indicating that the data values have changed: 1 .

2 .

3 .

Results: 1 . 2 .

3 .
sqlserv erpedia.com/w iki/Built-in_F unctions_-_A ggregate_F unctions 36/49

09/03/13

Built-in F unctions - A ggregate F unctions - S Q LS erv erP edia

4 . 5 . 6 .

7 .

CHECKS Function
The CHECKSUM function returns the checksum value computed over a row in a given table; alternatively it can return the checksum of the specified list of values. This function is intended for building hash
sqlserv erpedia.com/w iki/Built-in_F unctions_-_A ggregate_F unctions 37/49

09/03/13

Built-in F unctions - A ggregate F unctions - S Q LS erv erP edia

indexes and always returns an INT data type value. The syntax is: 1 .

The flavor examines all columns of a given table; it returns an error if the table contains TEXT, NTEXT or IMAGE data type columns. If you specify expressions instead
sqlserv erpedia.com/w iki/Built-in_F unctions_-_A ggregate_F unctions 38/49

CHECKSUM(*

09/03/13

Built-in F unctions - A ggregate F unctions - S Q LS erv erP edia

of a table then expressions must be of any data type other than TEXT, NTEXT, IMAGE, CURSOR or sql_variant containing one of the preceding data types.

The following example returns a CHECKSUM of the first row in the DimProduct table: 1 .

2 . 3 . 4 .
sqlserv erpedia.com/w iki/Built-in_F unctions_-_A ggregate_F unctions 39/49

09/03/13

Built-in F unctions - A ggregate F unctions - S Q LS erv erP edia

5 .

The next example returns a CHECKSUM of given values: 1 .

2 . 3 . 4 .

5 .

To add a hash index to a table you need to add a computed column using CHECKSUM
sqlserv erpedia.com/w iki/Built-in_F unctions_-_A ggregate_F unctions 40/49

09/03/13

Built-in F unctions - A ggregate F unctions - S Q LS erv erP edia

function and then create an index on the computed column, as in the following: 1 .

2 . 3 .

GROUPIN Function
The GROUPING function returns a value of 1 if the row is added to the result set by either ROLLUP or CUBE clause
sqlserv erpedia.com/w iki/Built-in_F unctions_-_A ggregate_F unctions 41/49

09/03/13

Built-in F unctions - A ggregate F unctions - S Q LS erv erP edia

of the SELECT statement. CUBE and ROLLUP extensions allow generating very simple textual reports in a query window. Majority of business reports will use more sophisticated reporting tools than the Query Analyzer or SQL Server Management Studio, therefore CUBE and ROLLUP extensions, as well as the GROUPING function have
sqlserv erpedia.com/w iki/Built-in_F unctions_-_A ggregate_F unctions 42/49

09/03/13

Built-in F unctions - A ggregate F unctions - S Q LS erv erP edia

limited usage. The syntax of the GROUPING function is: 1 .

The column is a column referred to in the GROUP BY statement.

The following query returns sum of amounts per organization and total of all organization amounts combined as a separate "grouping row": 0 1 .
sqlserv erpedia.com/w iki/Built-in_F unctions_-_A ggregate_F unctions 43/49

09/03/13

Built-in F unctions - A ggregate F unctions - S Q LS erv erP edia

0 2 . 0 3 . 0 4 .

0 5 .

sqlserv erpedia.com/w iki/Built-in_F unctions_-_A ggregate_F unctions

44/49

09/03/13

Built-in F unctions - A ggregate F unctions - S Q LS erv erP edia

0 6 . 0 7 .

0 8 .

0 9 . 1 0 . 1 1 .

1 2 .

1 3 .

1 4 .

1 5 .

sqlserv erpedia.com/w iki/Built-in_F unctions_-_A ggregate_F unctions

45/49

09/03/13

Built-in F unctions - A ggregate F unctions - S Q LS erv erP edia

The ROLLUP extension and the GROUPING function are more useful when the report is grouping records based on multiple columns. For example, the following query groups the output by product class, style and English name: 1 .

sqlserv erpedia.com/w iki/Built-in_F unctions_-_A ggregate_F unctions

46/49

09/03/13

Built-in F unctions - A ggregate F unctions - S Q LS erv erP edia

Note that a grouping row is generated for each class, each class and style combination and the grand total of all sales.

More SQL Server Function


SQL Server 2005 String like LTRIM,

Function

REPLAC STUFF
sqlserv erpedia.com/w iki/Built-in_F unctions_-_A ggregate_F unctions 47/49

CHARIN

09/03/13

Built-in F unctions - A ggregate F unctions - S Q LS erv erP edia

SQL Server 2005 Cursor like

Function

@@FET SQL Server 2005 Date & Time like

CURSO

Function

DATEPA

DATEDIF SQL Server 2005 Mathem like RAND, FLOOR SQL Server 2005 Text and Image like

GETUTC

Function

CEILING

Function

PATINDE SQL Server 2005 System sqlserv erpedia.com/w iki/Built-in_F unctions_-_A ggregate_F unctions 48/49

TEXTPT

Function

09/03/13

Built-in F unctions - A ggregate F unctions - S Q LS erv erP edia

like

COALES ERROR SQL Server 2005

ISNUME

Configur like

Function

@@MAX SQL Server 2000 if you can only develop against SQL Server 2000, this page the list of

@@SER

Function

contains

functions by 2000.

supporte

Powered by MediaWiki | 2012 Dell Inc., ALL RIGHTS RESERVED. | Privacy Policy | Terms of Use | Contact Us | Follow Us on Twitter

sqlserv erpedia.com/w iki/Built-in_F unctions_-_A ggregate_F unctions

49/49

You might also like