Professional Documents
Culture Documents
Search SQLServerPedia
Wiki
Pulse
Blog
Project Lucy
Podcasts
Contributors
deterministic.
sqlserv erpedia.com/w iki/Built-in_F unctions_-_A ggregate_F unctions 1/49
09/03/13
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
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
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
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
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
6/49
09/03/13
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
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 .
09/03/13
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.
09/03/13
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
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 .
11/49
09/03/13
0 7 .
0 8 .
0 9 .
1 0 .
1 1 .
12/49
09/03/13
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
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 .
14/49
09/03/13
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 .
15/49
09/03/13
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
0 5 . 0 6 . 0 7 . 0 8 . 0 9 . 1 0 .
17/49
09/03/13
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 .
09/03/13
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 .
09/03/13
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
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
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.
09/03/13
7 .
The next example uses DISTINCT keyword to return the sum of distinct values: 1 . 2 . 3 . 4 . 5 . 6 .
7 .
09/03/13
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
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
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
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 .
09/03/13
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
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 .
29/49
09/03/13
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.
09/03/13
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
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
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
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 .
09/03/13
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
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
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
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
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
5 .
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
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
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
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
0 2 . 0 3 . 0 4 .
0 5 .
44/49
09/03/13
0 6 . 0 7 .
0 8 .
0 9 . 1 0 . 1 1 .
1 2 .
1 3 .
1 4 .
1 5 .
45/49
09/03/13
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 .
46/49
09/03/13
Note that a grouping row is generated for each class, each class and style combination and the grand total of all sales.
Function
REPLAC STUFF
sqlserv erpedia.com/w iki/Built-in_F unctions_-_A ggregate_F unctions 47/49
CHARIN
09/03/13
Function
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
like
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
49/49