Aggregate Functions in SQL Server 2008


What are Aggregate Functions in SQL Server? This article helps you to explore various Aggregate Functions in SQL Server.

Introduction
   
   
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, CHECKSUM, CHECKSUM_AGG, COUNT, COUNT_BIG, GROUPING, MAX, MIN, SUM, STDEV, STDEVP, VAR, VARP
.
   
   AVG

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
      
         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
      
        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
   
          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
     
     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

      
    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

         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

        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

        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

        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

        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

        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

        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.

Up Next
    Ebook Download
    View all
    Learn
    View all