彙總函式 (Transact-SQL)Aggregate Functions (Transact-SQL)

適用於: 是SQL Server (從 2008 開始) 是Azure SQL Database 是Azure SQL 資料倉儲 是平行處理資料倉儲 APPLIES TO: yesSQL Server (starting with 2008) yesAzure SQL Database yesAzure SQL Data Warehouse yesParallel Data Warehouse

彙總函式會根據一組值來執行計算,並傳回單一值。An aggregate function performs a calculation on a set of values, and returns a single value. 除了 COUNT 之外,彙總函式會忽略 Null 值。Except for COUNT, aggregate functions ignore null values. 彙總函式經常用來搭配 SELECT 陳述式的 GROUP BY 子句使用。Aggregate functions are often used with the GROUP BY clause of the SELECT statement.

所有彙總函式都具有決定性。All aggregate functions are deterministic. 換句話說,使用一組特定輸入值呼叫時,彙總函式會在每次呼叫時傳回相同的值。In other words, aggregate functions return the same value each time that they are called, when called with a specific set of input values. 如需函式確定性的詳細資訊,請參閱決定性和非決定性函式See Deterministic and Nondeterministic Functions for more information about function determinism. OVER 子句可能會遵循所有彙總函式,但 GROUPING 和 GROUPING_ID 函式除外。The OVER clause may follow all aggregate functions, except the GROUPING or GROUPING_ID functions.

只有在下列情況下,才能使用彙總函式作為運算式:Use aggregate functions as expressions only in the following situations:

  • SELECT 陳述式的選取清單 (子查詢或外部查詢)。The select list of a SELECT statement (either a subquery or an outer query).
  • HAVING 子句。A HAVING clause.

Transact-SQLTransact-SQL 提供下列彙總函式:provides the following aggregate functions:

APPROX_COUNT_DISTINCTAPPROX_COUNT_DISTINCT MINMIN
AVGAVG STDEVSTDEV
CHECKSUM_AGGCHECKSUM_AGG STDEVPSTDEVP
COUNTCOUNT STRING_AGGSTRING_AGG
COUNT_BIGCOUNT_BIG SUMSUM
GROUPINGGROUPING VARVAR
GROUPING_IDGROUPING_ID VARPVARP
MAXMAX

另請參閱See also

內建函數 (Transact-SQL)Built-in Functions (Transact-SQL)
OVER 子句 (Transact-SQL)OVER Clause (Transact-SQL)