Querying with Flux
Outcome: three questions asked of the
weather bucket three different ways —
raw points, windowed averages, and
one-row summaries — with the arithmetic
checkable against the fixture.
Transcript:
influxdb-lab/transcripts/t03-flux-queries.txt.
The shape of every query
Flux is a pipe. Data flows from a
bucket, through functions joined by
|> (read it as then), and out as
tables. Four functions do most of the
work in this guide:
from(bucket: "weather")— the source.range(start: -1h)— the slice of time. Relative times (-1h,-7d) count back from now. Almost every query needs a range: without one, Flux assumes the last thirty days and your small question scans a month.filter(fn: (r) => ...)— keep the rows where the test is true.ris the row;r._field,r._measurementand any tag (r.station) are its columns.keep,sort,group— trim, order, and partition the tables.
Results are tables: one per series
(a measurement, one set of tags, one
field), with standard columns _time,
_value, _field, _measurement plus
one column per tag. Over the HTTP API
the same answers arrive as annotated
CSV (chapter 8).
Question 1: the raw points
One station, one field, in time order:
from(bucket: "weather")
|> range(start: -1h)
|> filter(fn: (r) => r._measurement == "weather"
and r._field == "temperature"
and r.station == "rooftop-1")
|> sort(columns: ["_time"])
|> keep(columns: ["_time", "_value"])
2026-11-01T02:26:00Z 18.1
2026-11-01T02:36:00Z 18.4
2026-11-01T02:46:00Z 18.9
2026-11-01T02:56:00Z 19.6
2026-11-01T03:06:00Z 20.3
2026-11-01T03:16:00Z 21.0
The fixture, read back exactly. Note the times are UTC — InfluxDB stores and answers in UTC, and any local-time manners are your display layer's job.
Question 2: averages over windows
The question time-series databases
exist to answer. aggregateWindow
chops the range into windows, folds
each window's points into one value,
and stamps the result with the
window's end time:
from(bucket: "weather")
|> range(start: -1h)
|> filter(fn: (r) => r._field == "temperature")
|> aggregateWindow(every: 20m, fn: mean, createEmpty: false)
garden-1 02:40 17.35
garden-1 03:00 18.15
garden-1 03:16 19.45 ← the partial window, cut off at "now"
rooftop-1 02:40 18.25
rooftop-1 03:00 19.25
rooftop-1 03:16 20.65
Check two of them against the fixture:
(18.1 + 18.4) ÷ 2 = 18.25; (20.3 + 21.0)
÷ 2 = 20.65. The means are honest. The
third row in each table is the lesson
this guide promised in chapter 2: its
timestamp is the query's own stop time
(03:16 and some seconds), because the
last window is still open — the range
cut it mid-window, and the partial
result is stamped where it was cut.
createEmpty: false simply says do
not invent rows for windows with no
points: without it, gaps in your data
become explicit empty windows, which is
sometimes exactly what a graph wants.
fn: accepts the whole summarising
family — max, min, sum, last,
count — and chapter 9 puts this same
function inside a task.
Question 3: one row per series
Drop the windows and summarise whole
tables instead. group first, so each
series summarises separately:
from(bucket: "weather")
|> range(start: -1h)
|> group(columns: ["station", "_field"])
|> mean()
garden-1 humidity 60.0
garden-1 temperature 18.3166…
rooftop-1 humidity 53.5
rooftop-1 temperature 19.3833…
The same shape with max() answers
how hot did it get: 21.0 on the roof,
19.8 in the garden — the fixture's own
maxima. Between range, filter,
aggregateWindow and these summary
functions, you hold the grammar of
nearly every real Flux question. The
language goes much further — joins,
moving windows, histograms — and this
guide's honest boundary is that it
teaches the grammar it ran, and names
the rest as documented territory in
chapter 13.