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
description: Learn how to use the arg_max() aggregation function to find a row in a table that maximizes the input expression.
4
4
ms.reviewer: alexans
5
5
ms.topic: reference
6
-
ms.date: 11/11/2024
6
+
ms.date: 01/15/2025
7
7
---
8
8
# arg_max() (aggregation function)
9
9
@@ -37,7 +37,9 @@ Returns a row in the table that maximizes the specified expression *ExprToMaximi
37
37
38
38
## Examples
39
39
40
-
Find the maximum latitude of a storm event in each state.
40
+
### Find maximum latitude
41
+
42
+
The following example finds the maximum latitude of a storm event in each state.
41
43
42
44
:::moniker range="azure-data-explorer"
43
45
> [!div class="nextstepaction"]
@@ -49,6 +51,8 @@ StormEvents
49
51
| summarize arg_max(BeginLat, BeginLocation) by State
50
52
```
51
53
54
+
**Output**
55
+
52
56
The results table displays only the first 10 rows.
53
57
54
58
| State | BeginLat | BeginLocation |
@@ -65,9 +69,11 @@ The results table displays only the first 10 rows.
65
69
| TEXAS | 36.4607 | DARROUZETT |
66
70
| ... | ... | ... |
67
71
68
-
Find the last time an event with a direct death happened in each state, showing all the columns.
72
+
### Find last state fatal event
73
+
74
+
The following example finds the last time an event with a direct death happened in each state, showing all the columns.
69
75
70
-
The query first filters the events to only include those where there was at least one direct death. Then the query returns the entire row with the most recent StartTime.
76
+
The query first filters the events to include only those events where there was at least one direct death. Then the query returns the entire row with the most recent `StartTime`.
71
77
72
78
:::moniker range="azure-data-explorer"
73
79
> [!div class="nextstepaction"]
@@ -80,21 +86,25 @@ StormEvents
80
86
| summarize arg_max(StartTime, *) by State
81
87
```
82
88
83
-
The results table displays only the first 10 rows and first 3 columns.
89
+
**Output**
90
+
91
+
The results table displays only the first 10 rows and first three columns.
The arg_max() function differs from the [max() function](max-aggregation-function.md). The arg_max() function allows you to return additional columns along with the maximum value, and [max()](max-aggregation-function.md) only returns the maximum value itself.
138
+
The arg_max() function differs from the [max() function](max-aggregation-function.md). The arg_max() function allows you to return other columns along with the maximum value, and [max()](max-aggregation-function.md) only returns the maximum value itself.
129
139
130
140
### Examples
131
141
132
142
#### arg_max()
133
143
134
144
Find the last time an event with a direct death happened, showing all the columns in the table.
135
145
136
-
The query first filters the events to only include those where there was at least one direct death. Then the query returns the entire row with the most recent (maximum) StartTime.
146
+
The query first filters the events to only include events where there was at least one direct death. Then the query returns the entire row with the most recent (maximum) StartTime.
137
147
138
148
:::moniker range="azure-data-explorer"
139
149
> [!div class="nextstepaction"]
@@ -156,7 +166,7 @@ The results table returns all the columns for the row containing the highest val
156
166
157
167
Find the last time an event with a direct death happened.
158
168
159
-
The query filters events to only include those where there is at least one direct death, and then returns the maximum value for StartTime.
169
+
The query filters events to only include events where there is at least one direct death, and then returns the maximum value for StartTime.
160
170
161
171
:::moniker range="azure-data-explorer"
162
172
> [!div class="nextstepaction"]
@@ -177,7 +187,8 @@ The results table returns the maximum value of StartTime, without returning othe
177
187
178
188
## Related content
179
189
190
+
*[Aggregation function types at a glance](aggregation-functions.md)
> The output column names are generated automatically, based on the arguments to the function. To assign different names to the output columns, use the following syntax: `... | extend (out1, out2) = array_sort_asc(array1,array2)`
68
+
> The output column names are generated automatically, based on the arguments to the function. To assign different names to the output columns, use the following syntax: `... | extend (out1, out2) = array_sort_asc(array1,array2)`.
69
+
70
+
### Sort substrings
63
71
64
-
## Example 2 - Sorting substrings
72
+
The following example sorts a list of names in ascending order. It saves a list of names to a variable, `Names`, which is then splits into an array and sorted in ascending order. The query returns the names in ascending order.
65
73
66
74
:::moniker range="azure-data-explorer"
67
75
> [!div class="nextstepaction"]
68
-
> <ahref="https://dataexplorer.azure.com/clusters/help/databases/Samples?query=H4sIAAAAAAAAA8tJLVHwS8xNLVawVVDyys/I0wlILM3RcU/NL0pP1QnKzEvPV7LmygGqCs4vKklNgaktLilKTiyJTywqSqzUAJPxxUAF8YnFyRrFBTmZJRpglToKSjpKmpoQypqroCgzr0ShKLW4NKcEaAiSkQD+ChdoiAAAAA=="target="_blank">Run the query</a>
76
+
> <ahref="https://dataexplorer.azure.com/clusters/help/databases/Samples?query=H4sIAAAAAAAAA8tJLVHwS8xNLVawVVDyys%2FI0wlILM3R8UrMS9XxTsxXsublygEqCc4vKklNgSksLilKTiyJTywqSqzUAJPxxUAF8YnFyRrFBTmZJRpglToKSjpKmpoQCmhQQVFmXolCUWpxaU4J0BQkMwFYffnyhgAAAA%3D%3D"target="_blank">Run the query</a>
69
77
::: moniker-end
70
78
71
79
```kusto
72
-
let Names = "John,Paul,George,Ringo";
80
+
let Names = "John,Paul,Jane,Kao";
73
81
let SortedNames = strcat_array(array_sort_asc(split(Names, ",")), ",");
74
82
print result = SortedNames
75
83
```
@@ -78,9 +86,11 @@ print result = SortedNames
78
86
79
87
|result|
80
88
|---|
81
-
|George,John,Paul,Ringo|
89
+
|Jane,John,Kao,Paul|
82
90
83
-
## Example 3 - Combining summarize and array_sort_asc
91
+
### Combine summarize and array_sort_asc
92
+
93
+
The following example uses the `summarize` operator and the `array_sort_asc` function to organize and sort commands by user in chronological order.
> If your data may contain `null` values, use [make_list_with_nulls](make-list-with-nulls-aggregation-function.md) instead of [make_list](make-list-aggregation-function.md).
124
+
> If your data might contain `null` values, use [make_list_with_nulls](make-list-with-nulls-aggregation-function.md) instead of [make_list](make-list-aggregation-function.md).
115
125
116
-
##Example 4 - Controlling location of `null` values
126
+
### Control location of `null` values
117
127
118
128
By default, `null` values are put last in the sorted array. However, you can control it explicitly by adding a `bool` value as the last argument to `array_sort_asc()`.
119
129
120
-
Example with default behavior:
130
+
The following example shows the default behavior:
121
131
122
132
:::moniker range="azure-data-explorer"
123
133
> [!div class="nextstepaction"]
124
-
> <ahref="https://dataexplorer.azure.com/clusters/help/databases/Samples?query=H4sIAAAAAAAAAysoyswrUUgsKkqsjC/OLyqJTyxO1kipzEvMzUzWiM4rzcnRUUrKKU1V0lGqTM3JyS8HMtKLUlPzlHRAkrGamgDOvUliQgAAAA=="target="_blank">Run the query</a>
134
+
> <ahref="https://dataexplorer.azure.com/clusters/help/databases/Samples?query=H4sIAAAAAAAAAysoyswrUShKLS7NKbFNLCpKrIwvzi8qiU8sTtZIqcxLzM1M1ojOK83J0VFKyilNVdJRqkzNyckvBzLSi1JT85R0QJKxmpoApOe8zkkAAAA%3D"target="_blank">Run the query</a>
The following example shows nondefault behavior using the `false` parameter, which specifies that nulls are placed at the beginning of the array.
138
148
139
149
:::moniker range="azure-data-explorer"
140
150
> [!div class="nextstepaction"]
141
-
> <ahref="https://dataexplorer.azure.com/clusters/help/databases/Samples?query=H4sIAAAAAAAAAxXJUQqAIAwA0KvIvhR2owhZtkJYMzYlvH319+DdVrUHMqOZvVnP5CXuU+mqJS46RBA2GQwIk0Xa8+E0ZgX8c00YDhLn9ALNIgvjSQAAAA=="target="_blank">Run the query</a>
151
+
> <ahref="https://dataexplorer.azure.com/clusters/help/databases/Samples?query=H4sIAAAAAAAAAxXJUQqAIAwA0KvEvhR2hU4SIctWCGvGpoS3r%2F4evNuKtsnYu7SZzGgkr9YSeQ77ULpKDot2EYRNOgPCYJH6fDiNWQH%2FXCNOB4lzfAFnY7HmUAAAAA%3D%3D"target="_blank">Run the query</a>
> The output column names are generated automatically, based on the arguments to the function. To assign different names to the output columns, use the following syntax: `... | extend (out1, out2) = array_sort_desc(array1,array2)`
70
+
> The output column names are generated automatically, based on the arguments to the function. To assign different names to the output columns, use the following syntax: `... | extend (out1, out2) = array_sort_desc(array1,array2)`.
71
+
72
+
## Sort substrings
65
73
66
-
## Example 2 - Sorting substrings
74
+
The following example sorts a list of names in descending order. It saves a list of names to a variable, `Names`, which is then splits into an array and sorted in descending order. The query returns the names in descending order.
67
75
68
76
:::moniker range="azure-data-explorer"
69
77
> [!div class="nextstepaction"]
70
-
> <ahref="https://dataexplorer.azure.com/clusters/help/databases/Samples?query=H4sIAAAAAAAAA8tJLVHwS8xNLVawVVDyys/I0wlILM3RcU/NL0pP1QnKzEvPV7LmygGqCs4vKklNgaktLilKTiyJTywqSqzUAJPxxUAF8SmpxckaxQU5mSUaYKU6Cko6SpqaEMqaq6AoM69EoSi1uDSnBGgKkpkA+RSmRokAAAA="target="_blank">Run the query</a>
78
+
> <ahref="https://dataexplorer.azure.com/clusters/help/databases/Samples?query=H4sIAAAAAAAAA8tJLVHwS8xNLVawVVDyys%2FI0wlILM3R8UrMS9XxTqzMV7Lm5coBqgnOLypJTYGpLC4pSk4siU8sKkqs1ACT8cVABfEpqcXJGsUFOZklGmClOgpKOkqamhAKaFJBUWZeiUJRanFpTgnQGCRDAeUAGb%2BIAAAA"target="_blank">Run the query</a>
71
79
::: moniker-end
72
80
73
81
```kusto
74
-
let Names = "John,Paul, George, Ringo";
82
+
let Names = "John,Paul,Jane,Kayo";
75
83
let SortedNames = strcat_array(array_sort_desc(split(Names, ",")), ",");
76
84
print result = SortedNames
77
85
```
@@ -80,9 +88,11 @@ print result = SortedNames
80
88
81
89
|result|
82
90
|---|
83
-
|Ringo, Paul,John, George|
91
+
|Paul,Kayo,John,Jane|
84
92
85
-
## Example 3 - Combining summarize and array_sort_desc
93
+
### Combine summarize and array_sort_desc
94
+
95
+
The following example uses the `summarize` operator and the `array_sort_asc` function to organize and sort commands by user in descending chronological order.
> If your data may contain `null` values, use [make_list_with_nulls](make-list-with-nulls-aggregation-function.md) instead of [make_list](make-list-aggregation-function.md).
126
+
> If your data can contain `null` values, use [make_list_with_nulls](make-list-with-nulls-aggregation-function.md) instead of [make_list](make-list-aggregation-function.md).
117
127
118
-
##Example 4 - Controlling location of `null` values
128
+
### Control location of `null` values
119
129
120
-
By default, `null` values are put last in the sorted array. However, you can control it explicitly by adding a `bool` value as the last argument to `array_sort_desc()`.
130
+
By default, `null` values are put last in the sorted array. However, you can control it explicitly by adding a `bool` value as the last argument to `array_sort_asc()`.
121
131
122
-
Example with default behavior:
132
+
The following example shows the default behavior:
123
133
124
134
:::moniker range="azure-data-explorer"
125
135
> [!div class="nextstepaction"]
126
-
> <ahref="https://dataexplorer.azure.com/clusters/help/databases/Samples?query=H4sIAAAAAAAAAysoyswrUUgsKkqsjC/OLyqJT0ktTtZIqcxLzM1M1ojOK83J0VFKyilNVdJRqkzNyckvBzLSi1JT85R0QJKxmpoAGsR2QUMAAAA="target="_blank">Run the query</a>
136
+
> <ahref="https://dataexplorer.azure.com/clusters/help/databases/Samples?query=H4sIAAAAAAAAAysoyswrUShKLS7NKbFNLCpKrIwvzi8qiU9JLU7WSKnMS8zNTNaIzivNydFRSsopTVXSUapMzcnJLwcy0otSU%2FOUdECSsZqaAAa5vexKAAAA"target="_blank">Run the query</a>
The following example shows nondefault behavior using the `false` parameter, which specifies that nulls are placed at the beginning of the array.
140
150
141
151
:::moniker range="azure-data-explorer"
142
152
> [!div class="nextstepaction"]
143
-
> <ahref="https://dataexplorer.azure.com/?query=H4sIAAAAAAAAAxXJUQqAIAwA0KvIvhR2owhZukKYM6YS3r76e/BuKzocmdGKvdmImXvyeSnVkvymUwThkMmAsFikPR8uY1bAP/eA7iTpHF4VdlwBSgAAAA=="target="_blank">Run the query</a>
153
+
> <ahref="https://dataexplorer.azure.com/clusters/help/databases/Samples?query=H4sIAAAAAAAAAxXJUQqAIAwA0KvIvhR2hU4SIaYrhDVjU8LbV38P3q1VulOywX1JqmlGa9pjIcu%2BTElXzX6VwYyw8yBAmMTcng%2BnEgngn1tAdyQ2Ci%2Bihlo3UQAAAA%3D%3D"target="_blank">Run the query</a>
0 commit comments