Back to Influxdb

Aggregates

content/shared/influxql-v3-reference/functions/aggregates.md

latest20.3 KB
Original Source

Use aggregate functions to assess, aggregate, and return values in your data. Aggregate functions return one row containing the aggregate values from each InfluxQL group.

Examples use the sample data set provided in the Get started with InfluxDB tutorial.

COUNT()

Returns the number of non-null field values.

sql
COUNT(field_expression)

Arguments

  • field_expression: Expression to identify one or more fields to operate on. Can be a field key, constant, regular expression, or wildcard (*). Supports all field types.

Examples

{{< expand-wrapper >}} {{% expand "Count the number of non-null values in a field" %}}

sql
SELECT COUNT(temp) FROM home

{{% influxql/table-meta %}} name: home {{% /influxql/table-meta %}}

timecount
1970-01-01T00:00:00Z26

{{% /expand %}}

{{% expand "Count the number of non-null values in each field" %}}

sql
SELECT COUNT(*) FROM home

{{% influxql/table-meta %}} name: home {{% /influxql/table-meta %}}

timecount_cocount_humcount_temp
1970-01-01T00:00:00Z262626

{{% /expand %}}

{{% expand "Count the number of non-null values in fields where the field key matches a regular expression" %}}

sql
SELECT COUNT(/^[th]/) FROM home

{{% influxql/table-meta %}} name: home {{% /influxql/table-meta %}}

timecount_humcount_temp
1970-01-01T00:00:00Z2626

{{% /expand %}}

{{% expand "Count distinct values for a field" %}}

InfluxQL supports nesting DISTINCT() in COUNT().

sql
SELECT COUNT(DISTINCT(co)) FROM home

{{% influxql/table-meta %}} name: home {{% /influxql/table-meta %}}

timecount
1970-01-01T00:00:00Z12

{{% /expand %}}

{{% expand "Count the number of non-null field values within time windows (grouped by time)" %}}

{{% influxdb/custom-timestamps %}}

sql
SELECT
  COUNT(temp)
FROM home
WHERE
  room = 'Kitchen'
  AND time >= '2022-01-01T08:00:00Z'
  AND time <= '2022-01-01T20:00:00Z'
GROUP BY time(6h)

{{% influxql/table-meta %}} name: home {{% /influxql/table-meta %}}

timecount
2022-01-01T06:00:00Z4
2022-01-01T12:00:00Z6
2022-01-01T18:00:00Z3

{{% /influxdb/custom-timestamps %}}

{{% /expand %}} {{< /expand-wrapper >}}

DISTINCT()

Returns the list of unique field values.

sql
DISTINCT(field_key)

Arguments

  • field_key: Field key to return distinct values from. Supports all field types.

Notable behaviors

  • InfluxQL supports nesting DISTINCT() with COUNT().

Examples

{{< expand-wrapper >}} {{% expand "List the distinct field values" %}}

sql
SELECT DISTINCT(co) FROM home

{{% influxql/table-meta %}} name: home {{% /influxql/table-meta %}}

timedistinct
1970-01-01T00:00:00Z0
1970-01-01T00:00:00Z1
1970-01-01T00:00:00Z3
1970-01-01T00:00:00Z4
1970-01-01T00:00:00Z7
1970-01-01T00:00:00Z5
1970-01-01T00:00:00Z9
1970-01-01T00:00:00Z18
1970-01-01T00:00:00Z14
1970-01-01T00:00:00Z22
1970-01-01T00:00:00Z17
1970-01-01T00:00:00Z26

{{% /expand %}} {{< /expand-wrapper >}}

INTEGRAL()

Returns the area under the curve for queried field values and converts those results into the summed area per unit of time.

[!Important]

  • INTEGRAL() does not support fill().
  • INTEGRAL() supports int64 and float64 field value data types.
sql
INTEGRAL(field_expression[, unit])

Arguments

  • field_expression: Expression to identify one or more fields to operate on. Can be a field key, constant, regular expression, or wildcard (*).
  • unit: Unit of time to use when calculating the integral. Default is 1s (one second).

Examples

{{< expand-wrapper >}} {{% expand "Calculate the integral for a field" %}}

sql
SELECT
  INTEGRAL(co)
FROM home
WHERE room = 'Kitchen'

{{% influxql/table-meta %}} name: home {{% /influxql/table-meta %}}

timeintegral
1970-01-01T00:00:00Z266400

{{% /expand %}}

{{% expand "Calculate the integral for a field and specify the unit option" %}}

sql
SELECT
  INTEGRAL(co, 1h)
FROM home
WHERE room = 'Kitchen'

{{% influxql/table-meta %}} name: home {{% /influxql/table-meta %}}

timeintegral
1970-01-01T00:00:00Z74

{{% /expand %}}

{{% expand "Calculate the integral for each field and specify the unit option" %}}

Return the area under the curve (in minutes) for the field values associated with each field key that stores numeric values in the h2o_feet measurement. The h2o_feet measurement has on numeric field: water_level.

sql
SELECT
  INTEGRAL(*, 1h)
FROM home
WHERE room = 'Kitchen'

{{% influxql/table-meta %}} name: home {{% /influxql/table-meta %}}

timeintegral_cointegral_humintegral_temp
1970-01-01T00:00:00Z74435272.25

{{% /expand %}}

{{% expand "Calculate the integral for the field keys that matches a regular expression" %}}

sql
SELECT
  INTEGRAL(/^[th]/, 1h)
FROM home
WHERE room = 'Kitchen'

{{% influxql/table-meta %}} name: home {{% /influxql/table-meta %}}

timeintegral_humintegral_temp
1970-01-01T00:00:00Z435272.25

{{% /expand %}}

{{% expand "Calculate the integral for a field grouping by time" %}}

{{% influxdb/custom-timestamps %}}

sql
SELECT
  INTEGRAL(co, 1h)
FROM home
WHERE
  room = 'Kitchen'
  AND time >= '2022-01-01T08:00:00Z'
  AND time <= '2022-01-01T20:00:00Z'
GROUP BY time(6h)

{{% influxql/table-meta %}} name: home {{% /influxql/table-meta %}}

timeintegral
2022-01-01T06:00:00Z0
2022-01-01T12:00:00Z30
2022-01-01T18:00:00Z44

{{% /influxdb/custom-timestamps %}}

{{% /expand %}} {{< /expand-wrapper >}}

MEAN()

Returns the arithmetic mean (average) of field values.

sql
MEAN(field_expression)

Arguments

  • field_expression: Expression to identify one or more fields to operate on. Can be a field key, constant, regular expression, or wildcard (*). Supports numeric fields.

Examples

{{< expand-wrapper >}} {{% expand "Calculate the mean value of a field" %}}

sql
SELECT MEAN(temp) FROM home

{{% influxql/table-meta %}} name: home {{% /influxql/table-meta %}}

timemean
1970-01-01T00:00:00Z22.396153846153844

{{% /expand %}}

{{% expand "Calculate the mean value of each field" %}}

sql
SELECT MEAN(*) FROM home

{{% influxql/table-meta %}} name: home {{% /influxql/table-meta %}}

timemean_comean_hummean_temp
1970-01-01T00:00:00Z5.26923076923076936.1522.396153846153844

{{% /expand %}}

{{% expand "Calculate the mean value of fields where the field key matches a regular expression" %}}

sql
SELECT MEAN(/^[th]/) FROM home

{{% influxql/table-meta %}} name: home {{% /influxql/table-meta %}}

timemean_hummean_temp
1970-01-01T00:00:00Z36.1522.396153846153844

{{% /expand %}}

{{% expand "Calculate the mean value of a field within time windows (grouped by time)" %}}

{{% influxdb/custom-timestamps %}}

sql
SELECT
  MEAN(temp)
FROM home
WHERE
  room = 'Kitchen'
  AND time >= '2022-01-01T08:00:00Z'
  AND time <= '2022-01-01T20:00:00Z'
GROUP BY time(6h)

{{% influxql/table-meta %}} name: home {{% /influxql/table-meta %}}

timemean
2022-01-01T06:00:00Z22.275
2022-01-01T12:00:00Z22.649999999999995
2022-01-01T18:00:00Z23.033333333333335

{{% /influxdb/custom-timestamps %}}

{{% /expand %}} {{< /expand-wrapper >}}

MEDIAN()

Returns the middle value from a sorted list of field values.

sql
MEDIAN(field_expression)

Arguments

  • field_expression: Expression to identify one or more fields to operate on. Can be a field key, constant, regular expression, or wildcard (*). Supports numeric fields.

Notable behaviors

  • MEDIAN() is nearly equivalent to PERCENTILE(field_key, 50), except MEDIAN() returns the average of the two middle field values if the field contains an even number of values.

Examples

{{< expand-wrapper >}} {{% expand "Calculate the median value of a field" %}}

sql
SELECT MEDIAN(temp) FROM home

{{% influxql/table-meta %}} name: home {{% /influxql/table-meta %}}

timemedian
1970-01-01T00:00:00Z22.45

{{% /expand %}}

{{% expand "Calculate the median value of each field" %}}

sql
SELECT MEDIAN(*) FROM home

{{% influxql/table-meta %}} name: home {{% /influxql/table-meta %}}

timemedian_comedian_hummedian_temp
1970-01-01T00:00:00Z136.0522.45

{{% /expand %}}

{{% expand "Calculate the median value of fields where the field key matches a regular expression" %}}

sql
SELECT MEDIAN(/^[th]/) FROM home

{{% influxql/table-meta %}} name: home {{% /influxql/table-meta %}}

timemedian_hummedian_temp
1970-01-01T00:00:00Z36.0522.45

{{% /expand %}}

{{% expand "Calculate the median value of a field within time windows (grouped by time)" %}}

{{% influxdb/custom-timestamps %}}

sql
SELECT
  MEDIAN(temp)
FROM home
WHERE
  room = 'Kitchen'
  AND time >= '2022-01-01T08:00:00Z'
  AND time <= '2022-01-01T20:00:00Z'
GROUP BY time(6h)

{{% influxql/table-meta %}} name: home {{% /influxql/table-meta %}}

timemedian
2022-01-01T06:00:00Z22.549999999999997
2022-01-01T12:00:00Z22.7
2022-01-01T18:00:00Z23.1

{{% /influxdb/custom-timestamps %}}

{{% /expand %}} {{< /expand-wrapper >}}

MODE()

Returns the most frequent value in a list of field values.

sql
MODE(field_expression)

Arguments

  • field_expression: Expression to identify one or more fields to operate on. Can be a field key, constant, regular expression, or wildcard (*). Supports all field types.

Notable behaviors

  • MODE() returns the field value with the earliest timestamp if there's a tie between two or more values for the maximum number of occurrences.

Examples

{{< expand-wrapper >}}

{{% expand "Calculate the mode value of a field" %}}

sql
SELECT MODE(co) FROM home

{{% influxql/table-meta %}} name: home {{% /influxql/table-meta %}}

timemode
1970-01-01T00:00:00Z0

{{% /expand %}}

{{% expand "Calculate the mode value of each field" %}}

sql
SELECT MODE(*) FROM home

{{% influxql/table-meta %}} name: home {{% /influxql/table-meta %}}

timemode_comode_hummode_temp
1970-01-01T00:00:00Z03622.7

{{% /expand %}}

{{% expand "Calculate the mode of field keys that match a regular expression" %}}

sql
SELECT MODE(/^[th]/) FROM home

{{% influxql/table-meta %}} name: home {{% /influxql/table-meta %}}

timemode_hummode_temp
1970-01-01T00:00:00Z3622.7

{{% /expand %}}

{{% expand "Calculate the mode a field within time windows (grouped by time)" %}}

{{% influxdb/custom-timestamps %}}

sql
SELECT
  MODE(co)
FROM home
WHERE
  room = 'Kitchen'
  AND time >= '2022-01-01T08:00:00Z'
  AND time <= '2022-01-01T20:00:00Z'
GROUP BY time(6h)

{{% influxql/table-meta %}} name: home {{% /influxql/table-meta %}}

timemode
2022-01-01T06:00:00Z0
2022-01-01T12:00:00Z1
2022-01-01T18:00:00Z18

{{% /influxdb/custom-timestamps %}}

{{% /expand %}} {{< /expand-wrapper >}}

SPREAD()

Returns the difference between the minimum and maximum field values.

sql
SPREAD(field_expression)

Arguments

  • field_expression: Expression to identify one or more fields to operate on. Can be a field key, constant, regular expression, or wildcard (*). Supports numeric fields.

Examples

{{< expand-wrapper >}}

{{% expand "Calculate the spread of a field" %}}

sql
SELECT SPREAD(temp) FROM home

{{% influxql/table-meta %}} name: home {{% /influxql/table-meta %}}

timespread
1970-01-01T00:00:00Z2.3000000000000007

{{% /expand %}}

{{% expand "Calculate the spread of each field" %}}

sql
SELECT SPREAD(*) FROM home

{{% influxql/table-meta %}} name: home {{% /influxql/table-meta %}}

timespread_cospread_humspread_temp
1970-01-01T00:00:00Z2612.3000000000000007

{{% /expand %}}

{{% expand "Calculate the spread of field keys that match a regular expression" %}}

sql
SELECT SPREAD(/^[th]/) FROM home

{{% influxql/table-meta %}} name: home {{% /influxql/table-meta %}}

timespread_humspread_temp
1970-01-01T00:00:00Z12.3000000000000007

{{% /expand %}}

{{% expand "Calculate the spread of a field within time windows (grouped by time)" %}}

{{% influxdb/custom-timestamps %}}

sql
SELECT
  SPREAD(co)
FROM home
WHERE
  room = 'Kitchen'
  AND time >= '2022-01-01T08:00:00Z'
  AND time <= '2022-01-01T20:00:00Z'
GROUP BY time(6h)

{{% influxql/table-meta %}} name: home {{% /influxql/table-meta %}}

timespread
2022-01-01T06:00:00Z0
2022-01-01T12:00:00Z9
2022-01-01T18:00:00Z8

{{% /influxdb/custom-timestamps %}}

{{% /expand %}} {{< /expand-wrapper >}}

STDDEV()

Returns the standard deviation of field values.

sql
STDDEV(field_expression)

Arguments

  • field_expression: Expression to identify one or more fields to operate on. Can be a field key, constant, regular expression, or wildcard (*). Supports numeric fields.

Examples

{{< expand-wrapper >}} {{% expand "Calculate the standard deviation of a field" %}}

sql
SELECT STDDEV(temp) FROM home

{{% influxql/table-meta %}} name: home {{% /influxql/table-meta %}}

timestddev
1970-01-01T00:00:00Z0.5553238833191091

{{% /expand %}}

{{% expand "Calculate the standard deviation of each field" %}}

sql
SELECT STDDEV(*) FROM home

{{% influxql/table-meta %}} name: home {{% /influxql/table-meta %}}

timestddev_costddev_humstddev_temp
1970-01-01T00:00:00Z7.7746135199516760.254950975679639260.5553238833191091

{{% /expand %}}

{{% expand "Calculate the standard deviation of fields where the field key matches a regular expression" %}}

sql
SELECT STDDEV(/^[th]/) FROM home

{{% influxql/table-meta %}} name: home {{% /influxql/table-meta %}}

timestddev_humstddev_temp
1970-01-01T00:00:00Z0.254950975679639260.5553238833191091

{{% /expand %}}

{{% expand "Calculate the standard deviation of a field within time windows (grouped by time)" %}}

{{% influxdb/custom-timestamps %}}

sql
SELECT
  STDDEV(co)
FROM home
WHERE
  room = 'Kitchen'
  AND time >= '2022-01-01T08:00:00Z'
  AND time <= '2022-01-01T20:00:00Z'
GROUP BY time(6h)

{{% influxql/table-meta %}} name: home {{% /influxql/table-meta %}}

timestddev
2022-01-01T06:00:00Z0
2022-01-01T12:00:00Z3.6742346141747673
2022-01-01T18:00:00Z4

{{% /influxdb/custom-timestamps %}}

{{% /expand %}} {{< /expand-wrapper >}}

SUM()

Returns the sum of field values.

sql
SUM(field_expression)

Arguments

  • field_expression: Expression to identify one or more fields to operate on. Can be a field key, constant, regular expression, or wildcard (*). Supports numeric fields.

Examples

{{< expand-wrapper >}} {{% expand "Calculate the sum of values in a field" %}}

sql
SELECT SUM(co) FROM home

{{% influxql/table-meta %}} name: home {{% /influxql/table-meta %}}

timesum
1970-01-01T00:00:00Z137

{{% /expand %}}

{{% expand "Calculate the sum of values in each field" %}}

sql
SELECT SUM(*) FROM home

{{% influxql/table-meta %}} name: home {{% /influxql/table-meta %}}

timesum_cosum_humsum_temp
1970-01-01T00:00:00Z137939.9582.3

{{% /expand %}}

{{% expand "Calculate the sum of values for fields where the field key matches a regular expression" %}}

sql
SELECT SUM(/^[th]/) FROM home

{{% influxql/table-meta %}} name: home {{% /influxql/table-meta %}}

timesum_humsum_temp
1970-01-01T00:00:00Z939.9582.3

{{% /expand %}}

{{% expand "Calculate the sum of values in a field within time windows (grouped by time)" %}}

{{% influxdb/custom-timestamps %}}

sql
SELECT
  SUM(co)
FROM home
WHERE
  room = 'Kitchen'
  AND time >= '2022-01-01T08:00:00Z'
  AND time <= '2022-01-01T20:00:00Z'
GROUP BY time(6h)

{{% influxql/table-meta %}} name: home {{% /influxql/table-meta %}}

timesum
2022-01-01T06:00:00Z0
2022-01-01T12:00:00Z21
2022-01-01T18:00:00Z66

{{% /influxdb/custom-timestamps %}}

{{% /expand %}} {{< /expand-wrapper >}}