STDEVP (Transact-SQL)

適用於:Microsoft Fabric 中 Microsoft Fabric倉儲中的 SQL ServerAzure SQL 資料庫 Azure SQL 受控執行個體 Azure Synapse AnalyticsAnalytics Platform System (PDW)SQL 分析端點

傳回指定運算式中之所有值的母體統計標準差。

Transact-SQL 語法慣例

Syntax

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

注意

若要檢視 SQL Server 2014 (12.x) 和舊版的 Transact-SQL 語法,請參閱 舊版檔

引數

ALL
將函數套用至所有值。 ALL 是預設值。

DISTINCT
指定要考量每個唯一值。

expression
為一項數值運算式。 不允許彙總函式和子查詢。 expression 為精確數值或近似數值資料類型類別的運算式,但是 bit 資料類型除外。

OVER ( [ partition_by_clause ] order_by_clause)
partition_by_clause 會將 FROM 子句產生的結果集分割成函數所要套用的分割區。 如未指定,此函數會將查詢結果集的所有資料列視為單一群組。 order_by_clause 可決定執行作業的邏輯順序。 order_by_clause 為必要項目。 如需詳細資訊,請參閱 OVER 子句 (Transact-SQL)

傳回型別

float

備註

如果在 SELECT 陳述式的所有項目上使用 STDEVP,結果集中的每個值都會包含在計算中。 STDEVP 只能搭配數值資料行來使用。 會忽略 Null 值。

STDEVP 未搭配 OVER 和 ORDER BY 子句使用時,是具決定性函數。 使用 OVER 和 ORDER BY 子句指定時,則不具決定性。 如需詳細資訊,請參閱 決定性與非決定性函數

範例

A:使用 STDEVP

下列範例會針對 AdventureWorks2022 資料庫中數據表中的所有 SalesPerson 獎金值,傳回母體擴展的標準偏差。

SELECT STDEVP(Bonus)  
FROM Sales.SalesPerson;  
GO  

範例:Azure Synapse Analytics 和 Analytics Platform System (PDW)

B:使用 STDEVP

下列範例會傳回 dbo.FactSalesQuota 資料表中所有銷售配額值的 STDEVP。 第一個資料行包含所有相異值的標準差,而第二個資料行則包含所有值的標準差 (包含任何重複的值)。

-- Uses AdventureWorks  
  
SELECT STDEVP(DISTINCT SalesAmountQuota)AS Distinct_Values, STDEVP(SalesAmountQuota) AS All_Values  
FROM dbo.FactSalesQuota;SELECT STDEVP(DISTINCT Quantity)AS Distinct_Values, STDEVP(Quantity) AS All_Values  
FROM ProductInventory;  

以下為結果集。

Distinct_Values   All_Values  
----------------  ----------------  
397676.79         397226.44

C. 搭配使用 OVER 和 STDEVP

下列範例會傳回日曆年度內每一季銷售配額值的 STDEVP。 請注意,ORDER BY 子句中的 OVER 會排列 STDEVP 的順序,而 ORDER BY 陳述式的 SELECT 會排列結果集的順序。

-- Uses AdventureWorks  
  
SELECT CalendarYear AS Year, CalendarQuarter AS Quarter, SalesAmountQuota AS SalesQuota,  
       STDEVP(SalesAmountQuota) OVER (ORDER BY CalendarYear, CalendarQuarter) AS StdDeviation  
FROM dbo.FactSalesQuota  
WHERE EmployeeKey = 272 AND CalendarYear = 2002  
ORDER BY CalendarQuarter;  

以下為結果集。

Year  Quarter  SalesQuota              StdDeviation  
----  -------  ----------------------  -------------------  
2002  1         91000.0000             0.00  
2002  2        140000.0000             24500.00  
2002  3         70000.0000             29329.55  
2002  4        154000.0000             34426.55

另請參閱

彙總函式 (Transact-SQL)
OVER 子句 (Transact-SQL)