Operate on columns

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.
  1. from(bucket: "noaa")
  2. |> range(start: -30d)
  3. |> group()
  4. |> keep(columns: ["location"])
  5. |> 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.
  1. from(bucket: "noaa")
  2. |> group()
  3. |> unique(column: "location")
  4. |> 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.
  1. from(bucket: "noaa")
  2. |> filter(fn: (r) => r._measurement == "average_temperature")
  3. |> range(start: -30d)
  4. |> 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.
  1. from(bucket: "noaa")
  2. |> filter(fn: (r) => r._measurement == "average_temperature")
  3. |> range(start: -30d)
  4. |> 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
••••••••••••••••••••••••