Documentation

Operate on columns

This page documents an earlier version of InfluxDB. InfluxDB v2.4 is the latest stable version. View this page in the v2.4 documentation.

Use the following common queries to operate on columns:

These examples use NOAA water sample data.

Find and count unique values in a column

Find and count the number of unique values in a specified column. The following examples find and count unique locations where data was collected.

Find unique values

This query:

  • Uses group() to ungroup data and return results in a single table.
  • Uses keep() and unique() to return unique values in the specified column.
from(bucket: "noaa")
  |> range(start: -30d)
  |> group()
  |> keep(columns: ["location"])
  |> unique(column: "location")

Example results

location
coyote_creek
santa_monica

Count unique values

This query:

  • Uses group() to ungroup data and return results in a single table.
  • Uses keep(), unique(), and then count() to count the number of unique values.
from(bucket: "noaa")
  |> group()
  |> unique(column: "location")
  |> count(column: "location")

Example results

location
2

Recalculate the _values column

To recalculate the _value column, use the with operator in map() to overwrite the existing _value column.

The following query:

  • Uses filter() to filter the average_temperature measurement.
  • Uses map() to convert Fahrenheit temperature values into Celsius.

from(bucket: "noaa")
  |> filter(fn: (r) => r._measurement == "average_temperature")
  |> range(start: -30d)
  |> map(fn: (r) => ({r with _value: (float(v: r._value) - 32.0) * 5.0 / 9.0} ))
_field_measurement_start_stop_timelocation_value
degreesaverage_temperature1920-03-05T22:10:01Z2020-03-05T22:10:01Z2019-08-17T00:00:00Zcoyote_creek27.77777777777778
degreesaverage_temperature1920-03-05T22:10:01Z2020-03-05T22:10:01Z2019-08-17T00:06:00Zcoyote_creek22.77777777777778
degreesaverage_temperature1920-03-05T22:10:01Z2020-03-05T22:10:01Z2019-08-17T00:12:00Zcoyote_creek30
degreesaverage_temperature1920-03-05T22:10:01Z2020-03-05T22:10:01Z2019-08-17T00:18:00Zcoyote_creek31.666666666666668
degreesaverage_temperature1920-03-05T22:10:01Z2020-03-05T22:10:01Z2019-08-17T00:24:00Zcoyote_creek25
degreesaverage_temperature1920-03-05T22:10:01Z2020-03-05T22:10:01Z2019-08-17T00:30:00Zcoyote_creek21.11111111111111
degreesaverage_temperature1920-03-05T22:10:01Z2020-03-05T22:10:01Z2019-08-17T00:36:00Zcoyote_creek28.88888888888889
degreesaverage_temperature1920-03-05T22:10:01Z2020-03-05T22:10:01Z2019-08-17T00:42:00Zcoyote_creek24.444444444444443
degreesaverage_temperature1920-03-05T22:10:01Z2020-03-05T22:10:01Z2019-08-17T00:48:00Zcoyote_creek29.444444444444443
degreesaverage_temperature1920-03-05T22:10:01Z2020-03-05T22:10:01Z2019-08-17T00:54:00Zcoyote_creek26.666666666666668
degreesaverage_temperature1920-03-05T22:10:01Z2020-03-05T22:10:01Z2019-08-17T01:00:00Zcoyote_creek21.11111111111111
•••••••••••••••••••••

Calculate a new column

To use values in a row to calculate and add a new column, use map(). This example below converts temperature from Fahrenheit to Celsius and maps the Celsius value to a new celsius column.

The following query:

  • Uses filter() to filter the average_temperature measurement.
  • Uses map() to create a new column calculated from existing values in each row.
from(bucket: "noaa")
  |> filter(fn: (r) => r._measurement == "average_temperature")
  |> range(start: -30d)
  |> map(fn: (r) => ({r with
      celsius: ((r._value - 32.0) * 5.0 / 9.0)
    })
  )

Example results

_start_stop_field_measurementlocation_time_valuecelsius
1920-03-05T22:10:01Z2020-03-05T22:10:01Zdegreesaverage_temperaturecoyote_creek2019-08-17T00:00:00Z8227.78
1920-03-05T22:10:01Z2020-03-05T22:10:01Zdegreesaverage_temperaturecoyote_creek2019-08-17T00:06:00Z7322.78
1920-03-05T22:10:01Z2020-03-05T22:10:01Zdegreesaverage_temperaturecoyote_creek2019-08-17T00:12:00Z8630.00
1920-03-05T22:10:01Z2020-03-05T22:10:01Zdegreesaverage_temperaturecoyote_creek2019-08-17T00:18:00Z8931.67
1920-03-05T22:10:01Z2020-03-05T22:10:01Zdegreesaverage_temperaturecoyote_creek2019-08-17T00:24:00Z7725.00
1920-03-05T22:10:01Z2020-03-05T22:10:01Zdegreesaverage_temperaturecoyote_creek2019-08-17T00:30:00Z7021.11
1920-03-05T22:10:01Z2020-03-05T22:10:01Zdegreesaverage_temperaturecoyote_creek2019-08-17T00:36:00Z8428.89
1920-03-05T22:10:01Z2020-03-05T22:10:01Zdegreesaverage_temperaturecoyote_creek2019-08-17T00:42:00Z7624.44
1920-03-05T22:10:01Z2020-03-05T22:10:01Zdegreesaverage_temperaturecoyote_creek2019-08-17T00:48:00Z8529.44
1920-03-05T22:10:01Z2020-03-05T22:10:01Zdegreesaverage_temperaturecoyote_creek2019-08-17T00:54:00Z8026.67
••••••••••••••••••••••••

Was this page helpful?

Thank you for your feedback!


Set your InfluxDB URL

Upgrade to InfluxDB Cloud or InfluxDB 2.0!

InfluxDB Cloud and InfluxDB OSS 2.0 ready for production.