Documentation

SQL selector functions

SQL selector functions are designed to work with time series data. They behave similarly to aggregate functions in that they take a collection of data and return a single value. However, selectors are unique in that they return a struct that contains a time value in addition to the computed value.

How do selector functions work?

Each selector function returns an Arrow struct (similar to a JSON object) representing a single time and value from the specified column in the each group. What time and value get returned depend on the logic in the selector function. For example, selector_first returns the value of specified column in the first row of the group. selector_max returns the maximum value of the specified column in the group.

Selector struct schema

The struct returned from a selector function has two properties:

  • time: time value in the selected row
  • value: value of the specified column in the selected row
{time: 2023-01-01T00:00:00Z, value: 72.1}
  • Copy
  • Fill window

Selector functions in use

In your SELECT statement, execute a selector function and use bracket notation to reference properties of the returned struct to populate the column value:

SELECT
  selector_first(temp, time)['time'] AS time,
  selector_first(temp, time)['value'] AS temp,
  room
FROM home
GROUP BY room
  • Copy
  • Fill window

Selector functions

selector_min

Returns the smallest value of a selected column and a timestamp.

selector_min(expression, timestamp)
  • Copy
  • Fill window
Arguments
  • expression: Expression to operate on. Can be a constant, column, or function, and any combination of string or arithmetic operators.
  • timestamp: Time expression. Can be a constant, column, or function.

View selector_min query example

selector_max

Returns the largest value of a selected column and a timestamp.

selector_max(expression, timestamp)
  • Copy
  • Fill window
Arguments
  • expression: Expression to operate on. Can be a constant, column, or function, and any combination of string or arithmetic operators.
  • timestamp: Time expression. Can be a constant, column, or function.

View selector_max query example

selector_first

Returns the first value ordered by time ascending.

selector_first(expression, timestamp)
  • Copy
  • Fill window
Arguments
  • expression: Expression to operate on. Can be a constant, column, or function, and any combination of string or arithmetic operators.
  • timestamp: Time expression. Can be a constant, column, or function.

View selector_first query example

selector_last

Returns the last value ordered by time ascending.

selector_last(expression, timestamp)
  • Copy
  • Fill window
Arguments
  • expression: Expression to operate on. Can be a constant, column, or function, and any combination of string or arithmetic operators.
  • timestamp: Time expression. Can be a constant, column, or function.

View selector_last query example


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:

InfluxDB Cloud Serverless