• Log in

Nested aggregation: make ordered computations in a single query

With nested aggregation, you can complete a NRQL query, then make additional computations using the results of that query. Nested aggregation provides NRQL capabilities similar to the class of SQL subqueries or subselects where the subquery is in the FROM clause of the outer query.

Tip

This feature is different from our subquery feature, which allows for subqueries in SELECT and WHERE clauses.

Answer complex questions with a single query

Nested aggregation can help you to answer questions like these without building multiple queries:

  • How can I count the requests per minute for my application, then get the maximum requests per minute for the last hour?
  • How can I compute the average CPU usage of all my servers or hosts, and list only the ones with usage over 90%?
  • From all my user sessions, how can I figure out what percentage bounced immediately?

For an example of how to use nested aggregation with an app's error rate query to get percentage data and more, watch this YouTube video (approx. 3:10 minutes).

Nested aggregation query structure and clauses

Every NRQL query must begin with a SELECT statement or a FROM clause. A nested aggregation query uses both a SELECT statement and a FROM clause and applies them to a whole query or queries contained within parentheses.

A complete and properly formatted nested aggregation query will look something like this:

SELECT function(attribute)
FROM (
SELECT function(attribute)
FROM dataType
WHERE attribute
TIMESERIES integer units
)

A few other details about query and clause behavior:

  • Nested queries can be more than two levels.
  • Both TIMESERIES and FACET can be applied to any part of the nested query, and don’t need to be identical at all levels.
  • SINCE, UNTIL, and COMPARE WITH clauses apply to the entire query and can only be used at the outermost level.

Nested aggregation query examples

Here are some examples of nested queries.

Copyright © 2022 New Relic Inc.

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.