keyValues() function
The keyValues()
function returns a table with the input table’s group key plus two columns,
_key
and _value
, that correspond to unique column and value pairs for specific
columns in each input table.
keyValues(keyColumns: ["usage_idle", "usage_user"])
Parameters
keyColumns
A list of columns from which values are extracted.
All columns indicated must be of the same type.
Each input table must have all of the columns listed by the keyColumns
parameter.
tables
Input data.
Default is piped-forward data (<-
).
Examples
The following examples use sample.data()
to simulate data queried from InfluxDB and illustrate how keys()
transforms data.
Get key values from explicitly defined columns
import "influxdata/influxdb/sample"
data = sample.data(set: "airSensor")
|> filter(fn: (r) => r.sensor_id == "TLM0100")
data
|> keyValues(keyColumns: ["sensor_id", "_field"])
Get key values from all group key columns
- Use
keys()
andfindColumn()
to extract an array of group key columns and store it as a variable. - Use
keyValues()
and provide the group key column array variable to thekeyColumns
parameter.
import "influxdata/influxdb/sample"
data = sample.data(set: "airSensor")
|> filter(fn: (r) => r.sensor_id == "TLM0100")
keyColumns = data
|> keys()
|> findColumn(fn: (key) => true, column: "_value")
// Returns [_field, _measurement, sensor_id]
data
|> keyValues(keyColumns: keyColumns)
Was this page helpful?
Thank you for your feedback!
Support and feedback
Thank you for being part of our community! We welcome and encourage your feedback and bug reports for Flux and this documentation. To find support, use the following resources:
InfluxDB Cloud customers can contact InfluxData Support.