Documentation

Query first and last values

This page documents an earlier version of InfluxDB OSS. InfluxDB OSS v2 is the latest stable version. See the equivalent InfluxDB v2 documentation: Query first and last values.

Use the first() or last() functions to return the first or last record in an input table.

data
  |> first()

// OR

data
  |> last()
  • Copy
  • Fill window

By default, InfluxDB returns results sorted by time, however you can use the sort() function to change how results are sorted. first() and last() respect the sort order of input data and return records based on the order they are received in.

first

first() returns the first non-null record in an input table.

Given the following input:

_time_value
2020-01-01T00:01:00Z1.0
2020-01-01T00:02:00Z1.0
2020-01-01T00:03:00Z2.0
2020-01-01T00:04:00Z3.0

The following function returns:

|> first()
  • Copy
  • Fill window
_time_value
2020-01-01T00:01:00Z1.0

last

last() returns the last non-null record in an input table.

Given the following input:

_time_value
2020-01-01T00:01:00Z1.0
2020-01-01T00:02:00Z1.0
2020-01-01T00:03:00Z2.0
2020-01-01T00:04:00Z3.0

The following function returns:

|> last()
  • Copy
  • Fill window
_time_value
2020-01-01T00:04:00Z3.0

Use first() or last() with aggregateWindow()

Use first() and last() with aggregateWindow() to select the first or last records in time-based groups. aggregateWindow() segments data into windows of time, aggregates data in each window into a single point using aggregate or selector functions, and then removes the time-based segmentation.

Given the following input:

_time_value
2020-01-01T00:00:00Z10
2020-01-01T00:00:15Z12
2020-01-01T00:00:45Z9
2020-01-01T00:01:05Z9
2020-01-01T00:01:10Z15
2020-01-01T00:02:30Z11

The following function returns:

|> aggregateWindow(
  every: 1h,
  fn: first
)
  • Copy
  • Fill window
_time_value
2020-01-01T00:00:59Z10
2020-01-01T00:01:59Z9
2020-01-01T00:02:59Z11
|> aggregateWindow(
  every: 1h,
  fn: last
)
  • Copy
  • Fill window
_time_value
2020-01-01T00:00:59Z9
2020-01-01T00:01:59Z15
2020-01-01T00:02:59Z11

Was this page helpful?

Thank you for your feedback!


The future of Flux

Flux is going into maintenance mode. You can continue using it as you currently are without any changes to your code.

Read more

InfluxDB 3 Core and Enterprise are now in Beta

InfluxDB 3 Core and Enterprise are now available for beta testing, available under MIT or Apache 2 license.

InfluxDB 3 Core is a high-speed, recent-data engine that collects and processes data in real-time, while persisting it to local disk or object storage. InfluxDB 3 Enterprise is a commercial product that builds on Core’s foundation, adding high availability, read replicas, enhanced security, and data compaction for faster queries. A free tier of InfluxDB 3 Enterprise will also be available for at-home, non-commercial use for hobbyists to get the full historical time series database set of capabilities.

For more information, check out: