top
Use the top stage to identify and return the most frequently occurring (or highest-sum) elements for a given field. The stage provides approximate counts and percentages, making it scalable for large datasets.
Syntax
top <integer> <field> [by <field1> ,<field2>...] [top_count as <column name>, top_percent as <column name>]Parameters
integer
integer
No
Represents the number of top elements to return. If omitted, it defaults to up to 10 elements.
field
string
Yes
The field for which to find the top elements.
by field1, ...
string
No
An optional clause to group rows based on specified fields before identifying top elements within each group.
top_count as ...
string
No
Optional alias to rename the default TOP_COUNT result column.
top_percent as ...
string
No
Optional alias to rename the default TOP_PERCENT result column.
Returns
The top stage returns a result set containing the specified fields, along with TOP_COUNT (the approximate count of the value) and TOP_PERCENT (the percentage of the value relative to the total).
Usage notes
The
topstage produces approximate results, which are optimized for memory usage and time, particularly beneficial when dealing with vast amounts of data.Early Filtering: Apply
filterstages as early as possible in your query to reduce the dataset size before thetopstage processes the records.Precise Field Selection: Utilize the
fieldsstage immediately after initial filtering to select only the necessary columns. This minimizes the data footprint passed totopand subsequent stages, improving overall query performance.Timeframe Management: Always use the smallest practical
timeframeto limit data scanning.
Examples
Example 1: Basic top stage example
Goal: Identifies the top 3 most frequent event descriptions by count.
XQL code:
Explanation: The query calculates the approximate frequency of each unique event_description and returns the top 3 based on that frequency. The query also provides the percentage of each in the total.
Output:
"User login successful"
1
~10.0%
"File access attempt"
1
~10.0%
"Network connection established"
1
~10.0%
Example 2: Top stage with by clause
Goal: Finds the top 1 event description for each 'is_successful' status.
XQL code:
Explanation: The by is_successful clause groups the records first by their is_successful status, and then top 1 event_description is applied independently to each of these groups.
Output:
true
"User login successful"
1
~16.7%
false
"File access attempt"
1
~33.3%
Example 3: Top stage with custom column names
Goal: Shows the top successful/unsuccessful statuses with custom count/percentage names.
XQL code:
Explanation: The query identifies the frequencies of true and false in the is_successful field. The top_count as StatusCount and top_percent as StatusPercentage clauses rename the output columns as specified, enhancing readability.
Output:
true
7
70.0%
false
3
30.0%
Related articles
Functions:
approx_topDatasets:
xdr_data
Last updated
Was this helpful?
