1

How to write similar query using Flux:

SELECT field_a,field_b from 'measurement' where field_a = 10 and group by field_b
tomerpacific
  • 4,704
  • 13
  • 34
  • 52
Nikhil
  • 21
  • 1
  • 2

2 Answers2

2

You can query several fields using a regex. And you can group by fields if you pivot your result table using the schema.fieldsAsCols() function; that way, the result of the query has columns that have the names of the queried fields. See this query:

import "influxdata/influxdb/schema"
from(bucket: "yourBucket")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "measurement")
  |> filter(fn: (r) => r["_field"] =~ /^(field_a|field_b)$/)
  |> aggregateWindow(every: v.windowPeriod, fn: first, createEmpty: false)
  //|> group()
  //|> sort(columns: ["_time"])
  |> schema.fieldsAsCols()
  |> filter(fn: (r) => r.field_a == 10)  
  |> group(columns: ["field_b"])
  //|> max(column: "field_b")
  |> yield()

Two remarks :

  1. To make sure that you have only one table before you group by field_b, uncomment the lines |> group() and |> sort(columns: ["_time"]). The first ungroups the result which is otherwise splitted into different values of your tags (if you have any). The latter sortes the ungrouped result by the timestamp.
  2. Since there is no aggregation in your initial query, the flux query outputs several result tables depending on the number of different values of field_b. If you are for example interested in the the max of field_a for every group uncomment the line before |> yield().
oidamo
  • 141
  • 5
0

I'm afraid that the InfluxQL above won't work as currently InfluxDB only supports tags and time interval in GROUP BY clause, not the fields. This could be inferred from the syntax of group by clause (for more information refer to InfluxDB documentation).

Nevertheless, if you are grouping by some tag as follows:

SELECT field_a,tag_b from 'measurement' where field_a = 10 and group by tag_b

This is the equivalent Flux query:

from(bucket: "thisIsYourBucketInInfluxDBV2")
// specify start:0 to query from all time. Equivalent to SELECT * from db1. Use just as cautiously.
|> range(start: 0) 
|> filter(fn: (r) => r._measurement == "measurement" and r._field == "field_a" and r._value = 10)
|> filter(fn: (r) => r._value = 10)

Here is a guide for you to migrate your InfluxQL to Flux.

Munin
  • 1,576
  • 2
  • 19