min_by
Use the min_by() function to return the value of one expression corresponding to the minimum value of another expression within a group.
Syntax
min_by(<return_expression>, <sort_expression>)Parameters
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 minimum. The row with the lowest value of this expression determines which return_expression value is returned.
Returns
Type: Same as return_expression type
Description: The min_by() function returns the value of return_expression from the row where sort_expression has its minimum 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:
min_by()is an aggregation function and must be used with thecompstage or within a grouping context.Null Handling: Rows where
sort_expressionisnullare typically ignored when determining the minimum.Tie Breaking: If multiple rows share the same minimum
sort_expressionvalue, the function returns thereturn_expressionvalue from one of those rows (the specific row is not guaranteed).Relationship to max_by(): The
min_by()function is the counterpart ofmax_by(), which returns the value corresponding to the maximum.Common Use Cases: This function is used to find associated values at the point of minimum, such as finding the user with the lowest score, the timestamp of the earliest event, or the source of the smallest transaction.
Examples
Example 1: Find the event with the lowest numeric value
Goal: Retrieve the event ID associated with the minimum numeric value in the dataset.
XQL code:
Explanation: The min_by() function finds the row with the lowest numeric_value and returns the corresponding event_id. This is useful for identifying which event had the minimum value.
Output:
104
Example 2: Find the earliest event details per category
Goal: For each event category, find the event ID of the earliest event.
XQL code:
Explanation: This query groups records by event_type and for each group, returns the event_id of the row with the earliest _time value. This identifies the first event in each category.
Output:
login
101
logout
103
error
105
Example 3: Find the source with the shortest duration
Goal: Identify which source host had the shortest duration event.
XQL code:
Explanation: The min_by() function finds the row with the lowest duration_seconds and returns the corresponding source_name, identifying which source experienced the shortest event.
Output:
server-01
Related articles
Datasets:
sample_xql_rawXQL recipes: XQL Recipes
Was this helpful?
