SQL Server aggregate function syntax

Configurare noua (How To)

Situatie

The following illustrates the syntax of an aggregate function:

In this syntax;

  • First, specify the name of an aggregate function that you want to use such as AVGSUM, and MAX.
  • Second, use DISTINCT if you want only distinct values are considered in the calculation or ALL if all values are considered in the calculation. By default, ALL is used if you don’t specify any modifier.
  • Third, the expression can be a column of a table or an expression that consists of multiple columns with arithmetic operators.

Solutie

Pasi de urmat

SQL Server aggregate function examples

We will use the products table from the sample database for the demonstration.

products

AVG example

The following statement use the AVG() function to return the average list price of all products in the products table:

The following shows the output:

SQL Server Aggregate Functions - AVG function

Because the list price in USD, it should have two decimal places at most. Therefore, you need to round the result to a number with two decimal places. To do this, you use the ROUND and CAST functions as shown in the following query:

SQL Server Aggregate Functions - AVG function example

First, the ROUND function returns the rounded average list price. And then the CAST function converts the result to a decimal number with two decimal places.

COUNT example

The following statement uses the COUNT() function to return the number of products whose price is greater than 500:

The following shows the output:

SQL Server Aggregate Functions - COUNT function example

In this example:

  • First, the WHERE clause gets products whose list price is greater than 500.
  • Second, the COUNT function returns the number of products with list prices greater than 500.

MAX example

The following statement uses the MAX() function to return the highest list price of all products:

The following picture shows the output:

SQL Server Aggregate Functions - MAX function example

MIN example

Similarly, the following statement uses the MIN() function to return the lowest list price of all products:

The output is:

SQL Server Aggregate Functions - MIN function example

SUM example

To demonstrate the SUM() function, we will use the stocks table from the sample database.

The following statement uses the SUM() function to calculate the total stock by product id in all warehouses:

Here is the output:

SQL Server Aggregate Functions - SUM function example

Here is how the statement works:

  • First, the GROUP BY clause summarized the rows by product id into groups.
  • Second, the SUM() function calculated the sum of quantity for each group.

STDEV example

The following statement uses the STDEV() function to calculate the statistical standard deviation of all list prices:

In this tutorial, you have learned about the SQL Server aggregate functions and how to use them to calculate aggregates.

Tip solutie

Permanent

Voteaza

(5 din 15 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?