Aggregate functions
Aggregate functions in InfluxQL compute a single value from a set of field values. Use them in a SELECT statement to summarize time series data across a measurement, a field key, or a pattern of field keys.
| Function | Returns |
|---|---|
| COUNT() | Number of non-null field values |
| DISTINCT() | Unique field values |
| INTEGRAL() | Area under the field value curve (integral) |
| MEAN() | Arithmetic mean of field values |
| MEDIAN() | Median of field values |
| MODE() | Most frequently occurring field value |
| SPREAD() | Difference between the maximum and minimum field values |
| STDDEV() | Standard deviation of field values |
| SUM() | Sum of field values |
COUNT()
Returns the number of non-null field values.
Syntax
SELECT COUNT([*|<field_key>|/<regular_expression>/]) [INTO_clause] FROM_clause [WHERE_clause] [GROUP_BY_clause] [ORDER_BY_clause] [LIMIT_clause] [OFFSET_clause] [SLIMIT_clause] [SOFFSET_clause]Nested syntax
SELECT COUNT(DISTINCT([*|<field_key>|/<regular_expression>/])) [...]Parameter variants
| Syntax | Description |
|---|---|
COUNT(field_key) | Counts non-null values for the specified field key |
COUNT(/regular_expression/) | Counts non-null values for each field key matching the regular expression |
COUNT(*) | Counts non-null values for every field key in the measurement |
COUNT() accepts field values of all data types. DISTINCT() can be nested inside COUNT().
Usage notes
When no data exists in a time interval, most InfluxQL functions return null. COUNT() returns 0 instead. If you use fill(<fill_option>) in the same query, the fill() function replaces that 0 with the specified fill value.
Count non-null values for a specific field key
SELECT COUNT("water_level") FROM "h2o_feet"
name: h2o_feet
time count
---- -----
1970-01-01T00:00:00Z 15258Count non-null values for all field keys in a measurement
SELECT COUNT(*) FROM "h2o_feet"
name: h2o_feet
time count_level description count_water_level
---- ---------------------- -----------------
1970-01-01T00:00:00Z 15258 15258The h2o_feet measurement contains two field keys: level description and water_level.
Count non-null values for field keys matching a regular expression
SELECT COUNT(/water/) FROM "h2o_feet"
name: h2o_feet
time count_water_level
---- -----------------
1970-01-01T00:00:00Z 15258Returns counts for every field key containing water in h2o_feet.
Count non-null values with multiple clauses
SELECT COUNT("water_level") FROM "h2o_feet" WHERE time >='2015-08-17T23:48:00Z' AND time <='2015-08-18T00:54:00Z' GROUP BY time(12m),* fill(200) LIMIT 7 SLIMIT 1
name: h2o_feet
tags: location=coyote_creek
time count
---- -----
2015-08-17T23:48:00Z 200
2015-08-18T00:00:00Z 2
2015-08-18T00:12:00Z 2
2015-08-18T00:24:00Z 2
2015-08-18T00:36:00Z 2
2015-08-18T00:48:00Z 2Results are grouped by 12-minute intervals and by tag. For time intervals with no data, fill(200) substitutes 200. The query returns a maximum of 7 points from 1 series.
Count unique values for a specific field key
SELECT COUNT(DISTINCT("level description")) FROM "h2o_feet"
name: h2o_feet
time count
---- -----
1970-01-01T00:00:00Z 4COUNT(DISTINCT()) returns the number of unique values for the level description field key.
fill() and COUNT() behavior
When a time interval has no data, COUNT() returns 0 rather than null. If fill() is present, it replaces that 0:
> SELECT COUNT("water_level") FROM "h2o_feet" WHERE time >='2015-09-18T21:24:00Z' AND time <='2015-09-18T21:54:00Z' GROUP BY time(12m)
name: h2o_feet
time count
---- -----
2015-09-18T21:24:00Z 2
2015-09-18T21:36:00Z 2
2015-09-18T21:48:00Z 0
> SELECT COUNT("water_level") FROM "h2o_feet" WHERE time >='2015-09-18T21:24:00Z' AND time <='2015-09-18T21:54:00Z' GROUP BY time(12m) fill(800000)
name: h2o_feet
time count
---- -----
2015-09-18T21:24:00Z 2
2015-09-18T21:36:00Z 2
2015-09-18T21:48:00Z 800000The first query returns 0 for the last interval because no data was reported. The second query uses fill(800000), which replaces that 0 with 800000.
DISTINCT()
Returns the unique field values for the specified field key.
Syntax
SELECT DISTINCT([*|<field_key>|/<regular_expression>/]) FROM_clause [WHERE_clause] [GROUP_BY_clause] [ORDER_BY_clause] [LIMIT_clause] [OFFSET_clause] [SLIMIT_clause] [SOFFSET_clause]Nested syntax
SELECT COUNT(DISTINCT([*|<field_key>|/<regular_expression>/])) [...]Parameter variants
| Syntax | Description |
|---|---|
DISTINCT(field_key) | Returns unique values for the specified field key |
DISTINCT(/regular_expression/) | Returns unique values for each field key matching the regular expression |
DISTINCT(*) | Returns unique values for every field key in the measurement |
DISTINCT() accepts field values of all data types. It can be nested inside COUNT().
Usage notes
Using DISTINCT() with an INTO clause may overwrite points in the destination measurement. DISTINCT() often returns multiple values sharing the same timestamp. TSDB for InfluxDB® treats points in the same series with the same timestamp as duplicates and keeps only the most recent point. See DISTINCT() and the INTO clause for details.
Return unique values for a specific field key
SELECT DISTINCT("level description") FROM "h2o_feet"
name: h2o_feet
time distinct
---- --------
1970-01-01T00:00:00Z between 6 and 9 feet
1970-01-01T00:00:00Z below 3 feet
1970-01-01T00:00:00Z between 3 and 6 feet
1970-01-01T00:00:00Z at or greater than 9 feetReturn unique values for all field keys in a measurement
SELECT DISTINCT(*) FROM "h2o_feet"
name: h2o_feet
time distinct_level description distinct_water_level
---- ------------------------- --------------------
1970-01-01T00:00:00Z between 6 and 9 feet 8.12
1970-01-01T00:00:00Z between 3 and 6 feet 8.005
1970-01-01T00:00:00Z at or greater than 9 feet 7.887
1970-01-01T00:00:00Z below 3 feet 7.762
[...]The h2o_feet measurement contains two field keys: level description and water_level.
Return unique values for field keys matching a regular expression
SELECT DISTINCT(/description/) FROM "h2o_feet"
name: h2o_feet
time distinct_level description
---- -------------------------
1970-01-01T00:00:00Z below 3 feet
1970-01-01T00:00:00Z between 6 and 9 feet
1970-01-01T00:00:00Z between 3 and 6 feet
1970-01-01T00:00:00Z at or greater than 9 feetReturns unique values for every field key containing description in h2o_feet.
Return unique values with multiple clauses
> SELECT DISTINCT("level description") FROM "h2o_feet" WHERE time >='2015-08-17T23:48:00Z' AND time <='2015-08-18T00:54:00Z' GROUP BY time(12m),* SLIMIT 1
name: h2o_feet
tags: location=coyote_creek
time distinct
---- --------
2015-08-18T00:00:00Z between 6 and 9 feet
2015-08-18T00:12:00Z between 6 and 9 feet
2015-08-18T00:24:00Z between 6 and 9 feet
2015-08-18T00:36:00Z between 6 and 9 feet
2015-08-18T00:48:00Z between 6 and 9 feetResults are grouped by 12-minute intervals and by tag. The query returns results from 1 series only.
Count unique values for a specific field key
SELECT COUNT(DISTINCT("level description")) FROM "h2o_feet"
name: h2o_feet
time count
---- -----
1970-01-01T00:00:00Z 4DISTINCT() and the INTO clause
When DISTINCT() is combined with INTO, TSDB for InfluxDB® may silently overwrite points. Because DISTINCT() often returns several results with the same timestamp, any points in the same series sharing a timestamp are treated as duplicates — only the latest point is kept.
The example below illustrates this: the initial DISTINCT() query returns 4 values, all with the same timestamp. Writing them to a new measurement with INTO consolidates them into a single point.
SELECT DISTINCT("level description") FROM "h2o_feet"
name: h2o_feet
time distinct
---- --------
1970-01-01T00:00:00Z below 3 feet
1970-01-01T00:00:00Z between 6 and 9 feet
1970-01-01T00:00:00Z between 3 and 6 feet
1970-01-01T00:00:00Z at or greater than 9 feet
SELECT DISTINCT("level description") INTO "distincts" FROM "h2o_feet"
name: result
time written
---- -------
1970-01-01T00:00:00Z 4
SELECT * FROM "distincts"
name: distincts
time distinct
---- --------
1970-01-01T00:00:00Z at or greater than 9 feetOnly one point survives because the four duplicate timestamps in the same series are merged into the most recently written value.
INTEGRAL()
Returns the area under the field value curve — that is, the integral of field values over time.
Syntax
SELECT INTEGRAL([*|<field_key>|/<regular_expression>/][,<unit>]) [INTO_clause] FROM_clause [WHERE_clause] [GROUP_BY_clause] [ORDER_BY_clause] [LIMIT_clause] [OFFSET_clause] [SLIMIT_clause] [SOFFSET_clause]Parameter variants
| Syntax | Description |
|---|---|
INTEGRAL(field_key) | Computes the integral for the specified field key |
INTEGRAL(/regular_expression/) | Computes the integral for each field key matching the regular expression |
INTEGRAL(*) | Computes the integral for every field key in the measurement |
The optional unit parameter sets the time granularity for the calculation. It is an integer followed by a time unit (for example, 1m for one minute). If omitted, the default is 1s.
INTEGRAL() supports INT64 and FLOAT64 field values only. It cannot be nested inside fill().
Calculate the integral with the default unit (1 second)
SELECT INTEGRAL("water_level") FROM "h2o_feet" WHERE "location"='santa_monica' AND time >='2015-08-18T00:00:00Z' AND time <='2015-08-18T00:30:00Z'
name: h2o_feet
time integral
---- --------
1970-01-01T00:00:00Z 3732.66Calculate the integral with a 1-minute unit
SELECT INTEGRAL("water_level",1m) FROM "h2o_feet" WHERE "location"='santa_monica' AND time >='2015-08-18T00:00:00Z' AND time <='2015-08-18T00:30:00Z'
name: h2o_feet
time integral
---- --------
1970-01-01T00:00:00Z 62.211Changing the unit from 1s to 1m scales the result by a factor of 60.
Calculate the integral for all numeric field keys with a 1-minute unit
SELECT INTEGRAL(*,1m) FROM "h2o_feet" WHERE "location"='santa_monica' AND time >='2015-08-18T00:00:00Z' AND time <='2015-08-18T00:30:00Z'
name: h2o_feet
time integral_water_level
---- --------------------
1970-01-01T00:00:00Z 62.211h2o_feet contains one numeric field key, water_level, so only one column appears.
Calculate the integral for field keys matching a regular expression with a 1-minute unit
SELECT INTEGRAL(/water/,1m) FROM "h2o_feet" WHERE "location"='santa_monica' AND time >='2015-08-18T00:00:00Z' AND time <='2015-08-18T00:30:00Z'
name: h2o_feet
time integral_water_level
---- --------------------
1970-01-01T00:00:00Z 62.211Calculate the integral with multiple clauses
SELECT INTEGRAL("water_level",1m) FROM "h2o_feet" WHERE "location"='santa_monica' AND time >='2015-08-18T00:00:00Z' AND time <='2015-08-18T00:30:00Z' GROUP BY time(12m) LIMIT 1
name: h2o_feet
time integral
---- --------
2015-08-18T00:00:00Z 24.972Results are grouped by 12-minute intervals. The query returns a maximum of 1 point.
MEAN()
Returns the arithmetic mean of the specified field values.
Syntax
SELECT MEAN([*|<field_key>|/<regular_expression>/]) [INTO_clause] FROM_clause [WHERE_clause] [GROUP_BY_clause] [ORDER_BY_clause] [LIMIT_clause] [OFFSET_clause] [SLIMIT_clause] [SOFFSET_clause]Parameter variants
| Syntax | Description |
|---|---|
MEAN(field_key) | Returns the mean for the specified field key |
MEAN(/regular_expression/) | Returns the mean for each field key matching the regular expression |
MEAN(*) | Returns the mean for every field key in the measurement |
MEAN() supports INT64 and FLOAT64 field values.
Calculate the mean for a specific field key
SELECT MEAN("water_level") FROM "h2o_feet"
name: h2o_feet
time mean
---- ----
1970-01-01T00:00:00Z 4.442107025822522Calculate the mean for all numeric field keys in a measurement
SELECT MEAN(*) FROM "h2o_feet"
name: h2o_feet
time mean_water_level
---- ----------------
1970-01-01T00:00:00Z 4.442107025822522h2o_feet contains one numeric field key, water_level.
Calculate the mean for field keys matching a regular expression
SELECT MEAN(/water/) FROM "h2o_feet"
name: h2o_feet
time mean_water_level
---- ----------------
1970-01-01T00:00:00Z 4.442107025822523Calculate the mean with multiple clauses
SELECT MEAN("water_level") FROM "h2o_feet" WHERE time >='2015-08-17T23:48:00Z' AND time <='2015-08-18T00:54:00Z' GROUP BY time(12m),* fill(9.01) LIMIT 7 SLIMIT 1
name: h2o_feet
tags: location=coyote_creek
time mean
---- ----
2015-08-17T23:48:00Z 9.01
2015-08-18T00:00:00Z 8.0625
2015-08-18T00:12:00Z 7.8245
2015-08-18T00:24:00Z 7.5675
2015-08-18T00:36:00Z 7.303
2015-08-18T00:48:00Z 7.046Results are grouped by 12-minute intervals and by tag. For time intervals with no data, fill(9.01) substitutes 9.01. The query returns a maximum of 7 points from 1 series.
MEDIAN()
Returns the median value from a sorted set of field values.
Syntax
SELECT MEDIAN([*|<field_key>|/<regular_expression>/]) [INTO_clause] FROM_clause [WHERE_clause] [GROUP_BY_clause] [ORDER_BY_clause] [LIMIT_clause] [OFFSET_clause] [SLIMIT_clause] [SOFFSET_clause]Parameter variants
| Syntax | Description |
|---|---|
MEDIAN(field_key) | Returns the median for the specified field key |
MEDIAN(/regular_expression/) | Returns the median for each field key matching the regular expression |
MEDIAN(*) | Returns the median for every field key in the measurement |
MEDIAN() supports INT64 and FLOAT64 field values.
Usage notes
MEDIAN()behaves likePERCENTILE(field_key, 50)in most cases. The difference: when the field key has an even number of values,MEDIAN()returns the average of the two middle values rather than one of them.
Calculate the median for a specific field key
SELECT MEDIAN("water_level") FROM "h2o_feet"
name: h2o_feet
time median
---- ------
1970-01-01T00:00:00Z 4.124Calculate the median for all numeric field keys in a measurement
SELECT MEDIAN(*) FROM "h2o_feet"
name: h2o_feet
time median_water_level
---- ------------------
1970-01-01T00:00:00Z 4.124h2o_feet contains one numeric field key, water_level.
Calculate the median for field keys matching a regular expression
SELECT MEDIAN(/water/) FROM "h2o_feet"
name: h2o_feet
time median_water_level
---- ------------------
1970-01-01T00:00:00Z 4.124Calculate the median with multiple clauses
SELECT MEDIAN("water_level") FROM "h2o_feet" WHERE time >='2015-08-17T23:48:00Z' AND time <='2015-08-18T00:54:00Z' GROUP BY time(12m),* fill(700) LIMIT 7 SLIMIT 1 SOFFSET 1
name: h2o_feet
tags: location=santa_monica
time median
---- ------
2015-08-17T23:48:00Z 700
2015-08-18T00:00:00Z 2.09
2015-08-18T00:12:00Z 2.077
2015-08-18T00:24:00Z 2.0460000000000003
2015-08-18T00:36:00Z 2.0620000000000003
2015-08-18T00:48:00Z 700Results are grouped by 12-minute intervals and by tag. For time intervals with no data, fill(700) substitutes 700. The query returns a maximum of 7 points from 1 series, skipping the first series (SOFFSET 1).
MODE()
Returns the most frequently occurring field value.
Syntax
SELECT MODE([*|<field_key>|/<regular_expression>/]) [INTO_clause] FROM_clause [WHERE_clause] [GROUP_BY_clause] [ORDER_BY_clause] [LIMIT_clause] [OFFSET_clause] [SLIMIT_clause] [SOFFSET_clause]Parameter variants
| Syntax | Description |
|---|---|
MODE(field_key) | Returns the most frequent value for the specified field key |
MODE(/regular_expression/) | Returns the most frequent value for each field key matching the regular expression |
MODE(*) | Returns the most frequent value for every field key in the measurement |
MODE() accepts field values of all data types.
Usage notes
If two or more values tie for the highest frequency, MODE() returns the value with the earliest timestamp.Return the most frequent value for a specific field key
SELECT MODE("level description") FROM "h2o_feet"
name: h2o_feet
time mode
---- ----
1970-01-01T00:00:00Z between 3 and 6 feetReturn the most frequent value for all field keys in a measurement
SELECT MODE(*) FROM "h2o_feet"
name: h2o_feet
time mode_level description mode_water_level
---- ---------------------- ----------------
1970-01-01T00:00:00Z between 3 and 6 feet 2.69The h2o_feet measurement contains two field keys: level description and water_level.
Return the most frequent value for field keys matching a regular expression
SELECT MODE(/water/) FROM "h2o_feet"
name: h2o_feet
time mode_water_level
---- ----------------
1970-01-01T00:00:00Z 2.69Return the most frequent value with multiple clauses
SELECT MODE("level description") FROM "h2o_feet" WHERE time >='2015-08-17T23:48:00Z' AND time <='2015-08-18T00:54:00Z' GROUP BY time(12m),* LIMIT 3 SLIMIT 1 SOFFSET 1
name: h2o_feet
tags: location=santa_monica
time mode
---- ----
2015-08-17T23:48:00Z
2015-08-18T00:00:00Z below 3 feet
2015-08-18T00:12:00Z below 3 feetResults are grouped by 12-minute intervals and by tag. The query returns a maximum of 3 points from 1 series, skipping the first series (SOFFSET 1).
SPREAD()
Returns the difference between the maximum and minimum field values.
Syntax
SELECT SPREAD([*|<field_key>|/<regular_expression>/]) [INTO_clause] FROM_clause [WHERE_clause] [GROUP_BY_clause] [ORDER_BY_clause] [LIMIT_clause] [OFFSET_clause] [SLIMIT_clause] [SOFFSET_clause]Parameter variants
| Syntax | Description |
|---|---|
SPREAD(field_key) | Returns the spread for the specified field key |
SPREAD(/regular_expression/) | Returns the spread for each field key matching the regular expression |
SPREAD(*) | Returns the spread for every field key in the measurement |
SPREAD() supports INT64 and FLOAT64 field values.
Calculate the spread for a specific field key
SELECT SPREAD("water_level") FROM "h2o_feet"
name: h2o_feet
time spread
---- ------
1970-01-01T00:00:00Z 10.574Calculate the spread for all numeric field keys in a measurement
SELECT SPREAD(*) FROM "h2o_feet"
name: h2o_feet
time spread_water_level
---- ------------------
1970-01-01T00:00:00Z 10.574h2o_feet contains one numeric field key, water_level.
Calculate the spread for field keys matching a regular expression
SELECT SPREAD(/water/) FROM "h2o_feet"
name: h2o_feet
time spread_water_level
---- ------------------
1970-01-01T00:00:00Z 10.574Calculate the spread with multiple clauses
SELECT SPREAD("water_level") FROM "h2o_feet" WHERE time >='2015-08-17T23:48:00Z' AND time <='2015-08-18T00:54:00Z' GROUP BY time(12m),* fill(18) LIMIT 3 SLIMIT 1 SOFFSET 1
name: h2o_feet
tags: location=santa_monica
time spread
---- ------
2015-08-17T23:48:00Z 18
2015-08-18T00:00:00Z 0.052000000000000046
2015-08-18T00:12:00Z 0.09799999999999986Results are grouped by 12-minute intervals and by tag. For time intervals with no data, fill(18) substitutes 18. The query returns a maximum of 3 points from 1 series, skipping the first series (SOFFSET 1).
STDDEV()
Returns the standard deviation of the specified field values.
Syntax
SELECT STDDEV([*|<field_key>|/<regular_expression>/]) [INTO_clause] FROM_clause [WHERE_clause] [GROUP_BY_clause] [ORDER_BY_clause] [LIMIT_clause] [OFFSET_clause] [SLIMIT_clause] [SOFFSET_clause]Parameter variants
| Syntax | Description |
|---|---|
STDDEV(field_key) | Returns the standard deviation for the specified field key |
STDDEV(/regular_expression/) | Returns the standard deviation for each field key matching the regular expression |
STDDEV(*) | Returns the standard deviation for every field key in the measurement |
STDDEV() supports INT64 or FLOAT64 field values.
Calculate the standard deviation for a specific field key
SELECT STDDEV("water_level") FROM "h2o_feet"
name: h2o_feet
time stddev
---- ------
1970-01-01T00:00:00Z 2.279144584196141Calculate the standard deviation for all numeric field keys in a measurement
SELECT STDDEV(*) FROM "h2o_feet"
name: h2o_feet
time stddev_water_level
---- ------------------
1970-01-01T00:00:00Z 2.279144584196141h2o_feet contains one numeric field key, water_level.
Calculate the standard deviation for field keys matching a regular expression
SELECT STDDEV(/water/) FROM "h2o_feet"
name: h2o_feet
time stddev_water_level
---- ------------------
1970-01-01T00:00:00Z 2.279144584196141Calculate the standard deviation with multiple clauses
SELECT STDDEV("water_level") FROM "h2o_feet" WHERE time >='2015-08-17T23:48:00Z' AND time <='2015-08-18T00:54:00Z' GROUP BY time(12m),* fill(18000) LIMIT 2 SLIMIT 1 SOFFSET 1
name: h2o_feet
tags: location=santa_monica
time stddev
---- ------
2015-08-17T23:48:00Z 18000
2015-08-18T00:00:00Z 0.03676955262170051Results are grouped by 12-minute intervals and by tag. For time intervals with no data, fill(18000) substitutes 18000. The query returns a maximum of 2 points from 1 series, skipping the first series (SOFFSET 1).
SUM()
Returns the sum of the specified field values.
Syntax
SELECT SUM([*|<field_key>|/<regular_expression>/]) [INTO_clause] FROM_clause [WHERE_clause] [GROUP_BY_clause] [ORDER_BY_clause] [LIMIT_clause] [OFFSET_clause] [SLIMIT_clause] [SOFFSET_clause]Parameter variants
| Syntax | Description |
|---|---|
SUM(field_key) | Returns the sum for the specified field key |
SUM(/regular_expression/) | Returns the sum for each field key matching the regular expression |
SUM(*) | Returns the sum for every field key in the measurement |
SUM() supports INT64 and FLOAT64 field values.
Calculate the sum for a specific field key
SELECT SUM("water_level") FROM "h2o_feet"
name: h2o_feet
time sum
---- ---
1970-01-01T00:00:00Z 67777.66900000004Calculate the sum for all numeric field keys in a measurement
SELECT SUM(*) FROM "h2o_feet"
name: h2o_feet
time sum_water_level
---- ---------------
1970-01-01T00:00:00Z 67777.66900000004h2o_feet contains one numeric field key, water_level.
Calculate the sum for field keys matching a regular expression
SELECT SUM(/water/) FROM "h2o_feet"
name: h2o_feet
time sum_water_level
---- ---------------
1970-01-01T00:00:00Z 67777.66900000004Calculate the sum with multiple clauses
SELECT SUM("water_level") FROM "h2o_feet" WHERE time >='2015-08-17T23:48:00Z' AND time <='2015-08-18T00:54:00Z' GROUP BY time(12m),* fill(18000) LIMIT 4 SLIMIT 1
name: h2o_feet
tags: location=coyote_creek
time sum
---- ---
2015-08-17T23:48:00Z 18000
2015-08-18T00:00:00Z 16.125
2015-08-18T00:12:00Z 15.649
2015-08-18T00:24:00Z 15.135Results are grouped by 12-minute intervals and by tag. For time intervals with no data, fill(18000) substitutes 18000. The query returns a maximum of 4 points from 1 series.