Skip to content

Latest commit

 

History

History
119 lines (89 loc) · 4.98 KB

stdev-transact-sql.md

File metadata and controls

119 lines (89 loc) · 4.98 KB
titledescriptionauthorms.authorms.datems.servicems.subservicems.topicf1_keywordshelpviewer_keywordsdev_langsmonikerRange
STDEV (Transact-SQL)
STDEV (Transact-SQL)
MikeRayMSFT
mikeray
03/13/2017
sql
t-sql
reference
STDEV_TSQL
STDEV
expressions [SQL Server], statistical standard deviation
STDEV function [Transact-SQL]
statistical standard deviation
TSQL
>= aps-pdw-2016 || = azuresqldb-current || = azure-sqldw-latest || >= sql-server-2016 || >= sql-server-linux-2017 || = azuresqldb-mi-current||=fabric

STDEV (Transact-SQL)

[!INCLUDE sql-asdb-asdbmi-asa-pdw-fabricse-fabricdw]

Returns the statistical standard deviation of all values in the specified expression.

:::image type="icon" source="../../includes/media/topic-link-icon.svg" border="false"::: Transact-SQL syntax conventions

Syntax

-- Aggregate Function Syntax STDEV ( [ ALL | DISTINCT ] expression ) -- Analytic Function Syntax STDEV ([ ALL ] expression) OVER ( [ partition_by_clause ] order_by_clause) 

Arguments

ALL
Applies the function to all values. ALL is the default.

DISTINCT
Specifies that each unique value is considered.

expression
Is a numeric expression. Aggregate functions and subqueries are not permitted. expression is an expression of the exact numeric or approximate numeric data type category, except for the bit data type.

OVER ( [ partition_by_clause ] order_by_clause)
partition_by_clause divides the result set produced by the FROM clause into partitions to which the function is applied. If not specified, the function treats all rows of the query result set as a single group. order_by_clause determines the logical order in which the operation is performed. order_by_clause is required. For more information, see OVER Clause (Transact-SQL).

Return Types

float

Remarks

If STDEV is used on all items in a SELECT statement, each value in the result set is included in the calculation. STDEV can be used with numeric columns only. Null values are ignored.

STDEV is a deterministic function when used without the OVER and ORDER BY clauses. It is nondeterministic when specified with the OVER and ORDER BY clauses. For more information, see Deterministic and Nondeterministic Functions.

Examples

A: Using STDEV

The following example returns the standard deviation for all bonus values in the SalesPerson table in the [!INCLUDEssSampleDBnormal] database.

SELECT STDEV(Bonus) FROMSales.SalesPerson; GO 

Examples: [!INCLUDEssazuresynapse-md] and [!INCLUDEssPDW]

B: Using STDEV

The following example returns the standard deviation of the sales quota values in the table dbo.FactSalesQuota. The first column contains the standard deviation of all distinct values and the second column contains the standard deviation of all values including any duplicates values.

-- Uses AdventureWorks SELECT STDEV(DISTINCT SalesAmountQuota)AS Distinct_Values, STDEV(SalesAmountQuota) AS All_Values FROMdbo.FactSalesQuota; 

[!INCLUDEssResult]

Distinct_Values All_Values ---------------- ---------------- 398974.27 398450.57 

C. Using STDEV with OVER

The following example returns the standard deviation of the sales quota values for each quarter in a calendar year. Notice that the ORDER BY in the OVER clause orders the STDEV and the ORDER BY of the SELECT statement orders the result set.

-- Uses AdventureWorks SELECT CalendarYear AS Year, CalendarQuarter AS Quarter, SalesAmountQuota AS SalesQuota, STDEV(SalesAmountQuota) OVER (ORDER BY CalendarYear, CalendarQuarter) AS StdDeviation FROMdbo.FactSalesQuotaWHERE EmployeeKey =272AND CalendarYear =2002ORDER BY CalendarQuarter; 

[!INCLUDEssResult]

Year Quarter SalesQuota StdDeviation ---- ------- ---------------------- ------------------- 2002 1 91000.0000 null 2002 2 140000.0000 34648.23 2002 3 70000.0000 35921.21 2002 4 154000.0000 39752.36 

See Also

Aggregate Functions (Transact-SQL)
OVER Clause (Transact-SQL)

close