Sort order, ascending (ASC) or descending (DESC) is applied per field. If not specified, the ascending (ASC) order is the default.
ORDER BY is supported on fields or aliases specified in the SELECT clause.
Aliases used in the ORDER BY clause should be surrounded with back quotes if they contain spaces or other special characters.
ORDER BY works on single-value metric functions and single-value math expressions. ORDER BY can not be used with percentile or stats.
ORDER BY on nested fields or metric aggregations that operate on nested fields is not supported. A nested field is any field that has a "." in the name, such as segments.errorList.errorType or btdata.estimatedtime.
ORDER BY can be used to order the buckets on a bucketed field in a bucketing function. For example, see the "Series aggregations - Ordering based on the keys of the series buckets." in the table below.
ORDER BY can be used on metric functions that follow bucketing functions, including filtered metric functions. Ordering by the metric value orders the resulting buckets. The metric function must be aliased and referenced by its alias such as:
SELECT transactionName, avg(responseTime) AS averageResponseTime FROM transactions ORDER BY averageResponseTime
Compound orderings can be specified for functions that provide implicit GROUP BYfunctionality.
A single ordering can be defined for the series function, no ordering is allowed for the range function.
ORDER BY can accept math expressions on fields, but not math expressions on metric functions. If the value of a math expression is null it is ordered at the end of the results in ascending order. The following is an invalid query: SELECT transactionName, max(discountValue) / max(cartTotal) AS ratio FROM transactions ORDER BY ratio
ORDER BY Clause Examples
For This Result
Query
An ordering of queries with no aggregations
SELECT field_name FROM event_type ORDER BY field_name ASC
Group By - Ordering by the lexicographic ordering of the terms. The fields included in the ORDER BY clause must be in the same order as in the query itself.
SELECT field_name, count(*) FROMevent_type ORDER BY field_name DESC
Returns a sorted set of timestamp ranges
SELECTseries(eventTimestamp, 10m), count(*) FROM transactions ORDER BY eventTimestamp DESC
Series aggregations - Ordering based on the keys of the series buckets.
SELECTseries(salary, 20000), count(salary) FROM event_typeORDER BY salary DESC
Date series aggregations - Ordering based on the date value of the keys of the series buckets
SELECT series(eventTimestamp, '1h'), count(eventTimestamp) FROM transactions ORDER BY eventTimestamp
Order by an alias.
SELECT responseTime AS RT FROM transactions ORDER BY RT
Order by an aliased math expression.
SELECT discountValue / cartTotal AS ratio FROM transactions ORDER BY ratio