Group results across time

You can use New Relic Insights to create NRQL queries that group results across time. For example, you can group results together based on timestamps by separating them into buckets that cover a specified range of dates and times.

When using time functions in NRQL queries, Insights presents the results in UTC. To adjust the results to your time zone, include the WITH TIMEZONE clause in your query.

Facet your NRQL query time range

To create your NRQL query, use a FACET clause with a bucket function that works with a timestamp attribute. Run a standard FACET query, but instead of faceting by an attribute, facet by time. For example:

SELECT count(*) FROM PageView SINCE 1 day ago FACET monthOf(account_created)

To perform multiple functions within the same query, use NRQL's multi-facet capability:

SELECT count(*) FROM PageView SINCE 1 day ago FACET dayOf(account_created), monthOf(account_created)
Time-based functions Description
yearOf(attr) Returns the year of a timestamp.
quarterOf(attr)

Returns the quarter of the year. The returned value includes both the quarter and the year.

Example: Q1 2014

monthOf(attr)

Returns the month and year of the timestamp.

Example: July 2014

weekOf(attr)

Returns the week the timestamp occurred by naming the month and day of that week's Monday.

Example: Week of January 15.

weekdayOf(attr) Returns the day of the week of the timestamp. The returned value loops back at the end of the week, allowing you to look at trends by weekday over time.
dateOf(attr)

Returns the date of the timestamp. The returned value includes month, day and year.

Example: July 15, 2014

dayOfMonthOf(attr) Returns the numeric date within a single month of the timestamp, a value from 1 to 31. The returned value does not include the month.
hourOf(attr) Returns the hour of the timestamp.
Group results by month

To group all results based on the month, use the monthOf function. In this example, the NRQL query includes a function (count(*)), an event type (PageView), a time frame (SINCE 1 day ago) and a time facet (monthOf(attribute)).

SELECT count(*) FROM PageView SINCE 1 day ago FACET monthOf(account_created)

Running the query returns the following table of results:

Insights cohort query results

insights.newrelic.com: In this NRQL query results example, the second-largest number of page views over the last day came from customers whose accounts were created during October.

Other grouping examples with FACET clause

You can run NRQL queries to group your data in other ways, not just time. For additional examples, see the NRQL FACET documentation.

For more information, check out the New Relic University tutorial Cohort Analysis Queries. Or, go directly to the full online course Writing NRQL Queries.

For more help

Recommendations for learning more: