For the complete documentation index, see llms.txt. Markdown versions of documentation pages are available by appending .md to page URLs.
Skip to content

max_by

Use the max_by() function to return the value of one expression corresponding to the maximum value of another expression within a group.

Syntax

max_by(<return_expression>, <sort_expression>)

Parameters

Name
Type
Required
Description

return_expression

any

Yes

The expression whose value is returned. This is the value you want to retrieve.

sort_expression

integer, float, string, timestamp

Yes

The expression used to determine the maximum. The row with the highest value of this expression determines which return_expression value is returned.

Returns

Type: Same as return_expression type

Description: The max_by() function returns the value of return_expression from the row where sort_expression has its maximum value within the group. If the input is null, the function may skip null values.

Usage notes

  • Input type: XQL doesn't support NaN or infinite values as input and these value types also can not be returned.

  • Aggregation Function: max_by() is an aggregation function and must be used with the comp stage or within a grouping context.

  • Null Handling: Rows where sort_expression is null are typically ignored when determining the maximum.

  • Tie Breaking: If multiple rows share the same maximum sort_expression value, the function returns the return_expression value from one of those rows (the specific row is not guaranteed).

  • Relationship to min_by(): The max_by() function is the counterpart of min_by(), which returns the value corresponding to the minimum.

  • Common Use Cases: This function is used to find associated values at the point of maximum, such as finding the user with the highest score, the timestamp of the latest event, or the source of the largest transaction.

Examples

Example 1: Find the event with the highest numeric value

Goal: Retrieve the event ID associated with the maximum numeric value in the dataset.

XQL code:

Explanation: The max_by() function finds the row with the highest numeric_value and returns the corresponding event_id. This is useful for identifying which event had the peak value.

Output:

MAX_EVENT

107

Example 2: Find the latest event details per category

Goal: For each event category, find the event ID of the most recent event.

XQL code:

Explanation: This query groups records by event_type and for each group, returns the event_id of the row with the latest _time value. This identifies the most recent event in each category.

Output:

EVENT_TYPE
LATEST_EVENT

login

115

logout

112

error

108

Example 3: Find the source with the highest duration

Goal: Identify which source host had the longest duration event.

XQL code:

Explanation: The max_by() function finds the row with the highest duration_seconds and returns the corresponding source_name, identifying which source experienced the longest event.

Output:

LONGEST_SOURCE

server-03

Last updated

Was this helpful?