PHP SQL Server – Aggregate Functions

PHP SQL Server – Aggregate Functions

PHP SQL Server Blog

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.

1. Aggregate functions are built in sql server functions.
2. Aggregate functions are applied to sets of records rather than to a single record.
3. Aggregate functions performs a computation on a set of values rather than on a single value.
4. Aggregate functions uses to summarize data.
5. 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 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;


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


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]


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]


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]


          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


         Returns the maximum value from expression. Max ignores any NULL values.

The syntax: MAX( [ALL | DISTINCT] <expression> )

select MAX(QUANTITY) from dbo.[Order Details]


        Returns the smallest value from expression. Min ignores any NULL values.

The syntax: MIN( [ALL | DISTINCT] <expression> )

select MIN(QUANTITY) from dbo.[Order Details]


        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]


        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]


        Returns the standard deviation for the population of all values in expression. Stdevp ignores any NULLvalues.

The syntax: STDEVP( <expression> )

    select STDEVP(QUANTITY) from dbo.[Order Details]


        Returns the variance of all values in expression. Var ignores any NULL values.

The syntax: VAR( <expression> )

    select VAR(QUANTITY) from dbo.[Order Details]



        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]


Hope this article helped you all, Thank you.


PHP SQL Server Blog

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">