MAX (Transact-SQL)MAX (Transact-SQL)

適用於: 是SQL Server 是Azure SQL Database 是Azure SQL 資料倉儲 是平行處理資料倉儲 APPLIES TO: yesSQL Server yesAzure SQL Database yesAzure SQL Data Warehouse yesParallel Data Warehouse

傳回運算式中的最大值。Returns the maximum value in the expression.

主題連結圖示 Transact-SQL 語法慣例Topic link icon Transact-SQL Syntax Conventions


-- Aggregation Function Syntax  
MAX( [ ALL | DISTINCT ] expression )  
-- Analytic Function Syntax  
MAX ([ ALL ] expression) OVER ( [ <partition_by_clause> ] [ <order_by_clause> ] )  


將彙總函式套用至所有值。Applies the aggregate function to all values. ALL 是預設值。ALL is the default.

指定要考量每個唯一值。Specifies that each unique value is considered. DISTINCT 對 MAX 沒意義,只是為了與 ISO 相容。DISTINCT is not meaningful with MAX and is available for ISO compatibility only.

這是一個常數、資料行名稱或函數,或算術、位元和字串運算子的任何組合。Is a constant, column name, or function, and any combination of arithmetic, bitwise, and string operators. MAX 可以搭配 numericcharacteruniqueidentifier,或 datetime 資料行使用,但無法搭配 bit 資料行使用。MAX can be used with numeric, character, uniqueidentifier, and datetime columns, but not with bit columns. 不允許彙總函式和子查詢。Aggregate functions and subqueries are not permitted.

如需詳細資訊,請參閱運算式 (Transact-SQL)For more information, see Expressions (Transact-SQL).

OVER ( [ partition_by_clause ] order_by_clause )OVER ( [ partition_by_clause ] order_by_clause)
partition_by_clause 會將 FROM 子句產生的結果集分割成函數所要套用的分割區。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 可決定執行作業的邏輯順序。order_by_clause determines the logical order in which the operation is performed. order_by_clause 為必要項目。order_by_clause is required. 如需詳細資訊,請參閱 OVER 子句 (Transact-SQL)For more information, see OVER Clause (Transact-SQL).

傳回類型Return Types

傳回與 expression 相同的值。Returns a value same as expression.


MAX 會忽略任何 Null 值。MAX ignores any null values.

沒有可選取的資料列時,MAX 會傳回 NULL。MAX returns NULL when there is no row to select.

針對字元資料行,MAX 會以定序順序尋找最高的值。For character columns, MAX finds the highest value in the collating sequence.

MAX 未搭配 OVER 和 ORDER BY 子句使用時,是具決定性函數。MAX is a deterministic function when used without the OVER and ORDER BY clauses. 使用 OVER 和 ORDER BY 子句指定時,則不具決定性。It is nondeterministic when specified with the OVER and ORDER BY clauses. 如需詳細資訊,請參閱 決定性與非決定性函數For more information, see Deterministic and Nondeterministic Functions.


A.A. 簡單範例Simple example

下列範例會傳回 AdventureWorks2012AdventureWorks2012 資料庫中的最高 (最大) 稅率。The following example returns the highest (maximum) tax rate in the AdventureWorks2012AdventureWorks2012 database.

FROM Sales.SalesTaxRate;  

以下為結果集:Here is the result set.

Warning, null value eliminated from aggregate.  
(1 row(s) affected)  

B.B. 使用 OVER 子句Using the OVER clause

下列範例會將 MIN、MAX、AVG 和 COUNT 函式搭配 OVER 子句一起使用,提供 AdventureWorks2012AdventureWorks2012 資料庫之 HumanResources.Department 資料表中各部門的彙總值。The following example uses the MIN, MAX, AVG, and COUNT functions with the OVER clause to provide aggregated values for each department in the HumanResources.Department table in the AdventureWorks2012AdventureWorks2012 database.

       , MIN(Rate) OVER (PARTITION BY edh.DepartmentID) AS MinSalary  
       , MAX(Rate) OVER (PARTITION BY edh.DepartmentID) AS MaxSalary  
       , AVG(Rate) OVER (PARTITION BY edh.DepartmentID) AS AvgSalary  
       ,COUNT(edh.BusinessEntityID) OVER (PARTITION BY edh.DepartmentID) AS EmployeesPerDept  
FROM HumanResources.EmployeePayHistory AS eph  
JOIN HumanResources.EmployeeDepartmentHistory AS edh  
     ON eph.BusinessEntityID = edh.BusinessEntityID  
JOIN HumanResources.Department AS d  
 ON d.DepartmentID = edh.DepartmentID  
WHERE edh.EndDate IS NULL  
ORDER BY Name;  

以下為結果集:Here is the result set.

Name                          MinSalary             MaxSalary             AvgSalary             EmployeesPerDept  
----------------------------- --------------------- --------------------- --------------------- ----------------  
Document Control              10.25                 17.7885               14.3884               5  
Engineering                   32.6923               63.4615               40.1442               6  
Executive                     39.06                 125.50                68.3034               4  
Facilities and Maintenance    9.25                  24.0385               13.0316               7  
Finance                       13.4615               43.2692               23.935                10  
Human Resources               13.9423               27.1394               18.0248               6  
Information Services          27.4038               50.4808               34.1586               10  
Marketing                     13.4615               37.50                 18.4318               11  
Production                    6.50                  84.1346               13.5537               195  
Production Control            8.62                  24.5192               16.7746               8  
Purchasing                    9.86                  30.00                 18.0202               14  
Quality Assurance             10.5769               28.8462               15.4647               6  
Research and Development      40.8654               50.4808               43.6731               4  
Sales                         23.0769               72.1154               29.9719               18  
Shipping and Receiving        9.00                  19.2308               10.8718               6  
Tool Design                   8.62                  29.8462               23.5054               6  
 (16 row(s) affected)  

C.C. 搭配字元資料使用 MAXUsing MAX with character data

下列範例會傳回根據字母順序排序名稱後,最後一個資料庫的名稱。The following example returns the database name that sorts as the last name alphabetically. 此範例使用 WHERE database_id < 5,以僅考慮系統資料庫。The example uses WHERE database_id < 5, to consider only the system databases.

SELECT MAX(name) FROM sys.databases WHERE database_id < 5;

最後一個系統資料庫為 tempdbThe last system database is tempdb.

另請參閱See Also

彙總函數 (Transact-SQL) Aggregate Functions (Transact-SQL)
OVER 子句 (Transact-SQL)OVER Clause (Transact-SQL)