| by Arround The Web | No comments

SQL Server STDEV Function

In this post, we will learn how to use the STDEV() function in the SQL Server to calculate the standard deviation of a set of values.

Let us explore!

SQL Server Stdev() Function Syntax and Parameters

The following shows the syntax of the stdev() function:

STDEV ( [ ALL | DISTINCT ] expression )

The function arguments are expressed in the following:

  1. ALL – This parameter allows the function to be applied to all provided values. By default, the function is applied to ALL.
  2. DISTINCT – If specified, this function is only applied to unique values.
  3. Expression – Refers to a numeric expression. The value of this parameter cannot be an aggregate function or a subquery.

The function returns a floating point value, denoting the standard deviation for the given set of values.

Example Usage:

The following examples demonstrate how to use the stdev() function in SQL Server:

Example 1: Using the Stdev Function

The following illustrations show the stdev function’s usage on an SQL Server table. The original table is as shown:

We can calculate the standard deviation of the values in the price column as shown in the following query:

SELECT stdev(price) AS std FROM PRODUCTS P;

This should return the resulting standard deviation as follows:

std               |
------------------+
1026.9104843447374|

As mentioned, the function calculates the standard deviation of all the values in the provided column.

Suppose we have a table with duplicate values as shown in the following:

If we calculate the standard deviation of the previous table, use the following command:

SELECT stdev(price) AS std FROM PRODUCTS P;

The resulting standard deviation value is as follows:

std              |
-----------------+
993.4328361796786|

We can exclude the duplicate values as shown in the following:

SELECT stdev(DISTINCT price) AS std FROM PRODUCTS P;

The resulting value is as follows:

std               |
------------------+
1026.9104843447374|

Conclusion

In this post, you learned how to use the stdev() function in the SQL Server to calculate the standard deviation for a given set of values.

Thanks for reading!

Share Button

Source: linuxhint.com

Leave a Reply