Situatie
The following illustrates the syntax of an aggregate function:
1
|
aggregate_function_name(DISTINCT | ALL expression)
|
In this syntax;
- First, specify the name of an aggregate function that you want to use such as
AVG
,SUM
, andMAX
. - Second, use
DISTINCT
if you want only distinct values are considered in the calculation orALL
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.
AVG example
The following statement use the AVG()
function to return the average list price of all products in the products table:
1
2
3
4
|
SELECT
AVG(list_price) avg_product_price
FROM
production.products;
|
The following shows the output:
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:
1
2
3
4
5
|
SELECT
CAST(ROUND(AVG(list_price),2) AS DEC(10,2))
avg_product_price
FROM
production.products;
|
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:
1
2
3
4
5
6
|
SELECT
COUNT(*) product_count
FROM
production.products
WHERE
list_price > 500;
|
The following shows the output:
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:
1
2
3
4
|
SELECT
MAX(list_price) max_list_price
FROM
production.products;
|
The following picture shows the output:
MIN example
Similarly, the following statement uses the MIN() function to return the lowest list price of all products:
1
2
3
4
|
SELECT
MIN(list_price) min_list_price
FROM
production.products;
|
The output is:
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:
1
2
3
4
5
6
7
8
9
|
SELECT
product_id,
SUM(quantity) stock_count
FROM
production.stocks
GROUP BY
product_id
ORDER BY
stock_count DESC;
|
Here is the output:
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:
1
2
3
4
|
SELECT
CAST(ROUND(STDEV(list_price),2) as DEC(10,2)) stdev_list_price
FROM
production.products;
|
In this tutorial, you have learned about the SQL Server aggregate functions and how to use them to calculate aggregates.
Leave A Comment?