You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
MAX ([ ALL ] expression) OVER ( <partition_by_clause> [ <order_by_clause> ] )
36
-
```
37
-
26
+
Returns the maximum of all values of the specified expression in a group. Can be followed by the [OVER](../queries/select-over-clause-transact-sql.md) clause.
27
+
28
+
> [!NOTE]
29
+
> To get the maximum of all values of multiple expressions inline, see [Logical functions - GREATEST](logical-functions-greatest-transact-sql.md).
MAX ( [ ALL ] expression) OVER ( [ <partition_by_clause> ] [ <order_by_clause> ] )
45
+
```
46
+
38
47
## Arguments
39
-
**ALL**
40
-
Applies the aggregate function to all values. ALL is the default.
41
-
42
-
DISTINCT
43
-
Specifies that each unique value is considered. DISTINCT is not meaningful with MAX and is available for ISO compatibility only.
44
-
45
-
*expression*
46
-
Is a constant, column name, or function, and any combination of arithmetic, bitwise, and string operators. MAX can be used with **numeric**, **character**, **uniqueidentifier**, and **datetime** columns, but not with **bit** columns. Aggregate functions and subqueries are not permitted.
47
-
48
-
For more information, see [Expressions (Transact-SQL)](../../t-sql/language-elements/expressions-transact-sql.md).
49
-
50
-
OVER **(**_partition\_by\_clause_[_order\_by\_clause_]**)**
51
-
*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. *_partition\_by\_clause_* is required. For more information, see [OVER Clause (Transact-SQL)](../../t-sql/queries/select-over-clause-transact-sql.md).
52
-
53
-
## Return Types
54
-
Returns a value same as *expression*.
55
-
56
-
## Remarks
57
-
MAX ignores any null values.
58
-
59
-
MAX returns NULL when there is no row to select.
60
-
61
-
For character columns, MAX finds the highest value in the collating sequence.
62
-
63
-
MAX 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](../../relational-databases/user-defined-functions/deterministic-and-nondeterministic-functions.md).
64
-
65
-
## Examples
66
-
67
-
### A. Simple example
68
-
The following example returns the highest (maximum) tax rate in the [!INCLUDE[ssSampleDBnormal](../../includes/sssampledbnormal-md.md)] database.
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 [!INCLUDE[ssSampleDBnormal](../../includes/sssampledbnormal-md.md)] database.
88
-
89
-
```sql
90
-
SELECT DISTINCT Name
91
-
, MIN(Rate) OVER (PARTITION BY edh.DepartmentID) AS MinSalary
92
-
, MAX(Rate) OVER (PARTITION BY edh.DepartmentID) AS MaxSalary
93
-
, AVG(Rate) OVER (PARTITION BY edh.DepartmentID) AS AvgSalary
94
-
,COUNT(edh.BusinessEntityID) OVER (PARTITION BY edh.DepartmentID) AS EmployeesPerDept
Research and Development 40.8654 50.4808 43.6731 4
122
-
Sales 23.0769 72.1154 29.9719 18
123
-
Shipping and Receiving 9.00 19.2308 10.8718 6
124
-
Tool Design 8.62 29.8462 23.5054 6
125
-
126
-
(16 row(s) affected)
127
-
```
128
-
129
-
### C. Using MAX with character data
130
-
The following example returns the database name that sorts as the last name alphabetically. The example uses `WHERE database_id < 5`, to consider only the system databases.
Applies the aggregate function to all values. `ALL` is the default.
52
+
53
+
#### DISTINCT
54
+
55
+
Specifies that each unique value is considered. `DISTINCT` isn't meaningful with `MAX`, and is available for ISO compatibility only.
56
+
57
+
#### *expression*
58
+
59
+
A constant, column name, or function, and any combination of arithmetic, bitwise, and string operators. `MAX` can be used with **numeric**, **char**, **nchar**, **varchar**, **nvarchar**, **uniqueidentifier**, or **datetime** columns, but not with **bit** columns. Aggregate functions and subqueries aren't permitted.
60
+
61
+
For more information, see [Expressions](../language-elements/expressions-transact-sql.md).
62
+
63
+
#### OVER ( [*partition_by_clause*][*order_by_clause*] )
64
+
65
+
*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.
66
+
67
+
*order_by_clause* determines the logical order in which the operation is performed.
68
+
69
+
For more information, see [SELECT - OVER clause](../queries/select-over-clause-transact-sql.md).
70
+
71
+
## Return types
72
+
73
+
Returns a value same as *expression*.
74
+
75
+
## Remarks
76
+
77
+
`MAX` ignores any null values.
78
+
79
+
`MAX` returns `NULL` when there's no row to select.
80
+
81
+
For character columns, `MAX` finds the highest value in the collating sequence.
82
+
83
+
`MAX` is a deterministic function when used without the `OVER` and `ORDER BY` clauses. It's nondeterministic when specified with the `OVER` and `ORDER BY` clauses. For more information, see [Deterministic and nondeterministic functions](../../relational-databases/user-defined-functions/deterministic-and-nondeterministic-functions.md).
84
+
85
+
`MAX` operates on a set of rows. To obtain the maximum value for a set of values inline, see [Logical functions - GREATEST](logical-functions-greatest-transact-sql.md).
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.
112
+
113
+
```sql
114
+
SELECT DISTINCT Name,
115
+
MIN(Rate) OVER (PARTITION BY edh.DepartmentID) AS MinSalary,
116
+
MAX(Rate) OVER (PARTITION BY edh.DepartmentID) AS MaxSalary,
117
+
AVG(Rate) OVER (PARTITION BY edh.DepartmentID) AS AvgSalary,
118
+
COUNT(edh.BusinessEntityID) OVER (PARTITION BY edh.DepartmentID) AS EmployeesPerDept
Research and Development 40.8654 50.4808 43.6731 4
146
+
Sales 23.0769 72.1154 29.9719 18
147
+
Shipping and Receiving 9.00 19.2308 10.8718 6
148
+
Tool Design 8.62 29.8462 23.5054 6
149
+
```
150
+
151
+
### C. Use MAX with character data
152
+
153
+
The following example returns the database name that sorts using the database name alphabetically. The example uses `WHERE database_id < 5`, to select the system databases.
0 commit comments