What are Aggregate Functions in SQL Server? This article helps you to explore various Aggregate Functions in SQL Server.
What are Aggregate Functions?
I can give different definitions.
- Aggregate functions are built in sql server functions.
- Aggregate functions are applied to sets of records rather than to a single record.
- Aggregate functions performs a computation on a set of values rather than on a single value.
- Aggregate functions uses to summarize data.
- Aggregate functions perform a calculation on a set of values and return a single value.
Getting Started
The information in multiple records are processed in a particular manner and then displayed in a single record answer.
Aggregate functions are often used in conjuction with GROUP BY clause.
Aggregate functions cannot be nested. The expression cannot be a subquery.
The list of built in Aggregate functions are:
AVG, CHECKSUM, CHECKSUM_AGG, COUNT, COUNT_BIG, GROUPING, MAX, MIN, SUM, STDEV, STDEVP, VAR, VARP.
AVG in SQL Server
AVG returns the average of the values in expression. The expression must contain numeric values. Null values are ignored.
The syntax: AVG ([ ALL | DISTINCT ] <expression>)
- select orderid, avg(UnitPrice) UnitPrice from dbo.[Order Details] group by orderid;
CHECKSUM in SQL Server
This is a basic hash algorithm usually used to detect changes or consistency in data.
"A digit representing the sum of the correct digits in a piece of stored or transmitted digital data, against which later comparisons can be made to detect errors in the data."
The syntax: CHECKSUM(<expression>, [ ... n] | *)
- SELECT CHECKSUM(orderid, UnitPrice, quantity), orderid, UnitPrice, quantity FROM dbo.[Order Details] WHERE orderid = 10248
CHECKSUM_AGG in SQL Server
The same as CHECKSUM, but the primary difference is that CHECKSUM is oriented around rows, whereas CHECKSUM_AGG is oriented around columns.
The syntax: CHECKSUM( [ALL | DISTINCT] <expression> )
- SELECT CHECKSUM_AGG(CAST(UnitPrice AS int)) FROM dbo.[Order Details]
- update dbo.[Order Details] set UnitPrice = 15
- where orderid = 10248 and ProductID = 11
- SELECT CHECKSUM_AGG(CAST(UnitPrice AS int)) FROM dbo.[Order Details]
COUNT in SQL Server
Returns the number of items in expression. The data type returned is of type int.
The syntax: COUNT( [ALL | DISTINCT] <expression> | * )
- select COUNT(*), AVG(UnitPrice) from dbo.[Order Details]
COUNT_BIG in SQL Server
Returns the number of items in a group. The data type returned is of type bigint.
The syntax: COUNT( [ALL | DISTINCT] <expression> | * )
- select COUNT_BIG(*), AVG(UnitPrice) from dbo.[Order Details]
GROUPING in SQL Server
MSDN : Is an aggregate function that causes an additional column to be output with a value of 1 when the row is added by either the CUBE or ROLLUP operator, or 0 when the row is not the result of CUBE or ROLLUP.
Function adds an extra column to the output of a SELECT statement.
The syntax: GROUPING(<column_name> )
- select orderid, sum(UnitPrice) UnitPrice, GROUPING(orderid) 'orderid'
- from dbo.[Order Details] WHERE orderid = 10248
- GROUP BY orderid WITH cube
MAX in SQL Server
Returns the maximum value from expression. Max ignores any NULL values.
The syntax: MAX( [ALL | DISTINCT] <expression> )
- select MAX(QUANTITY) from dbo.[Order Details]
MIN in SQL Server
Returns the smallest value from expression. Min ignores any NULL values.
The syntax: MIN( [ALL | DISTINCT] <expression> )
- select MIN(QUANTITY) from dbo.[Order Details]
SUM in SQL Server
Returns the total of all values in expression. Sum ignores any NULL values.
The syntax: SUM( [ALL | DISTINCT] <expression> )
- select SUM(QUANTITY) from dbo.[Order Details]
STDEV in SQL Server
Returns the standard deviation of all values in expression. Stdev ignores any NULL values.
The syntax: STDEV( <expression> )
- select STDEV(QUANTITY) from dbo.[Order Details]
STDEVP in SQL Server
Returns the standard deviation for the population of all values in expression. Stdevp ignores any NULL values.
The syntax: STDEVP( <expression> )
- select STDEVP(QUANTITY) from dbo.[Order Details]
VAR in SQL Server
Returns the variance of all values in expression. Var ignores any NULL values.
The syntax: VAR( <expression> )
- select VAR(QUANTITY) from dbo.[Order Details]
VARP in SQL Server
Returns the variance for the population of all values in expression. Varp ignores any NULL values.
The syntax: VARP( <expression> )
- select VARP(QUANTITY) from dbo.[Order Details]
Summary
Hope this article helped you all, Thank you.