InfluxQL selector functions

Use selector functions to assess, select, and return values in your data. Selector functions return one or more rows with the selected values from each InfluxQL group.

Each selector function below covers syntax, including parameters to pass to the function, and examples of how to use the function. Examples use NOAA water sample data.

BOTTOM()

Returns the smallest N field values. BOTTOM() supports int64 and float64 field value data types.

Note: BOTTOM() returns the field value with the earliest timestamp if there’s a tie between two or more values for the smallest value.

Syntax

  1. SELECT BOTTOM(<field_key>[,<tag_key(s)>],<N> )[,<tag_key(s)>|<field_key(s)>] FROM_clause [WHERE_clause] [GROUP_BY_clause] [ORDER_BY_clause] [LIMIT_clause] [OFFSET_clause] [SLIMIT_clause] [SOFFSET_clause]

BOTTOM(field_key,N)
Returns the smallest N field values associated with the field key.

BOTTOM(field_key,tag_key,N)
Returns the smallest field value for N tag values of the tag key. Add a comma between multiple tag keys: tag_key,tag_key.

BOTTOM((field_key,N),tag_key,field_key)
Returns the smallest N field values associated with the field key in the parentheses and the relevant tag and/or field. Add a comma between multiple tag or field keys: tag_key,tag_key,field_key,field_key.

Examples

Select the bottom three field values associated with a field key

Return the smallest three field values in the water_level field key and in the h2o_feet measurement.

  1. SELECT BOTTOM("water_level",3) FROM "h2o_feet"

name: h2o_feet

timebottom
2019-08-29T14:30:00Z-0.610
2019-08-29T14:36:00Z-0.591
2019-08-30T15:18:00Z-0.594

Select the bottom field value associated with a field key for two tags

Return the smallest field values in the water_level field key for two tag values associated with the location tag key.

  1. SELECT BOTTOM("water_level","location",2) FROM "h2o_feet"

name: h2o_feet

timebottomlocation
2019-08-29T10:36:00Z-0.243santa_monica
2019-08-29T14:30:00Z-0.610coyote_creek

Select the bottom four field values associated with a field key and the relevant tags and fields

Return the smallest four field values in the water_level field key and the relevant values of the location tag key and the level description field key.

  1. SELECT BOTTOM("water_level",4),"location","level description" FROM "h2o_feet"

name: h2o_feet

timebottomlocationlevel description
2019-08-29T14:24:00Z-0.587coyote_creekbelow 3 feet
2019-08-29T14:30:00Z-0.610coyote_creekbelow 3 feet
2019-08-29T14:36:00Z-0.591coyote_creekbelow 3 feet
2019-08-30T15:18:00Z-0.594coyote_creekbelow 3 feet

Select the bottom three field values associated with a field key and include several clauses

Return the smallest three values in the water_level field key for each 24-minute interval between 2019-08-18T00:00:00Z and 2019-08-18T00:54:00Z with results in descending timestamp order.

  1. SELECT BOTTOM("water_level",3),"location" FROM "h2o_feet" WHERE time >= '2019-08-18T00:00:00Z' AND time <= '2019-08-18T00:54:00Z' GROUP BY time(24m) ORDER BY time DESC

name: h2o_feet

timebottomlocation
2019-08-18T00:54:00Z2.172santa_monica
2019-08-18T00:54:00Z7.510coyote_creek
2019-08-18T00:48:00Z2.087santa_monica
2019-08-18T00:42:00Z2.093santa_monica
2019-08-18T00:36:00Z2.1261441420santa_monica
2019-08-18T00:24:00Z2.264santa_monica
2019-08-18T00:18:00Z2.329santa_monica
2019-08-18T00:12:00Z2.343santa_monica
2019-08-18T00:00:00Z2.352santa_monica

Notice that the GROUP BY time() clause does not override the points’ original timestamps. See Issue 1 in the section below for a more detailed explanation of that behavior.

Common issues with BOTTOM()

BOTTOM() with a GROUP BY time() clause

Queries with BOTTOM() and a GROUP BY time() clause return the specified number of points per GROUP BY time() interval. For most GROUP BY time() queries, the returned timestamps mark the start of the GROUP BY time() interval. GROUP BY time() queries with the BOTTOM() function behave differently; they maintain the timestamp of the original data point.

Example

The query below returns two points per 18-minute GROUP BY time() interval. Notice that the returned timestamps are the points’ original timestamps; they are not forced to match the start of the GROUP BY time() intervals.

  1. SELECT BOTTOM("water_level",2) FROM "h2o_feet" WHERE time >= '2019-08-18T00:00:00Z' AND time <= '2019-08-18T00:30:00Z' AND "location" = 'santa_monica' GROUP BY time(18m)

name: h2o_feet

timebottom
2019-08-18T00:00:00Z2.064
2019-08-18T00:12:00Z2.028
2019-08-18T00:24:00Z2.041
2019-08-18T00:30:00Z2.051

Notice that the first two rows contain the smallest values from the first time interval and the last two rows contains the smallest values for the second time interval.

BOTTOM() and a tag key with fewer than N tag values

Queries with the syntax SELECT BOTTOM(<field_key>,<tag_key>,<N>) can return fewer points than expected. If the tag key has X tag values, the query specifies N values, and X is smaller than N, then the query returns X points.

Example

The query below asks for the smallest field values of water_level for three tag values of the location tag key. Because the location tag key has two tag values (santa_monica and coyote_creek), the query returns two points instead of three.

  1. SELECT BOTTOM("water_level","location",3) FROM "h2o_feet"

name: h2o_feet

timebottomlocation
2019-08-29T10:36:00Z-0.243santa_monica
2019-08-29T14:30:00Z-0.610coyote_creek

FIRST()

Returns the field valuewith the oldest timestamp.

Syntax

  1. SELECT FIRST(<field_key>)[,<tag_key(s)>|<field_key(s)>] FROM_clause [WHERE_clause] [GROUP_BY_clause] [ORDER_BY_clause] [LIMIT_clause] [OFFSET_clause] [SLIMIT_clause] [SOFFSET_clause]

FIRST(field_key)
Returns the oldest field value (determined by timestamp) associated with the field key.

FIRST(/regular_expression/)
Returns the oldest field value (determined by timestamp) associated with each field key that matches the regular expression.

FIRST(*)
Returns the oldest field value (determined by timestamp) associated with each field key in the measurement.

FIRST(field_key),tag_key(s),field_key(s)
Returns the oldest field value (determined by timestamp) associated with the field key in the parentheses and the relevant tag and/or field.

FIRST() supports all field value data types.

Examples

Select the first field value associated with a field key

Return the oldest field value (determined by timestamp) associated with the level description field key and in the h2o_feet measurement.

  1. SELECT FIRST("level description") FROM "h2o_feet"

name: h2o_feet

timefirst
2019-08-17T00:00:00Zbetween 6 and 9 feet

Select the first field value associated with each field key in a measurement

Return the oldest field value (determined by timestamp) for each field key in the h2o_feet measurement. The h2o_feet measurement has two field keys: level description and water_level.

  1. SELECT FIRST(*) FROM "h2o_feet"

name: h2o_feet

timefirst_level descriptionfirst_water_level
1970-01-01T00:00:00Zbetween 6 and 9 feet8.120

Select the first field value associated with each field key that matches a regular expression

Return the oldest field value for each field key that includes the word level in the h2o_feet measurement.

  1. SELECT FIRST(/level/) FROM "h2o_feet"

name: h2o_feet

timefirst_level descriptionfirst_water_level
1970-01-01T00:00:00Zbetween 6 and 9 feet8.120

Select the first value associated with a field key and the relevant tags and fields

Return the oldest field value (determined by timestamp) in the level description field key and the relevant values of the location tag key and the water_level field key.

  1. SELECT FIRST("level description"),"location","water_level" FROM "h2o_feet"

name: h2o_feet

timefirstlocationwater_level
2019-08-17T00:00:00Zbetween 6 and 9 feetcoyote_creek8.120

Select the first field value associated with a field key and include several clauses

Returns the oldest field value (determined by timestamp) in the water_level field key in the time range between 2019-08-17T23:48:00Z and 2019-08-18T00:54:00Z and groups results into 12-minute time intervals and per tag. Then fill empty time intervals with 9.01, and it limit the number of points and series returned to four and one.

  1. SELECT FIRST("water_level") FROM "h2o_feet" WHERE time >= '2019-08-17T23:48:00Z' AND time <= '2019-08-18T00:54:00Z' GROUP BY time(12m),* fill(9.01) LIMIT 4 SLIMIT 1

name: h2o_feet tags: location=coyote_creek

timefirst
2019-08-17T23:48:00Z8.635
2019-08-18T00:00:00Z8.504
2019-08-18T00:12:00Z8.320
2019-08-18T00:24:00Z8.130

Notice that the GROUP BY time() clause overrides the points’ original timestamps. The timestamps in the results indicate the the start of each 12-minute time interval; the first point in the results covers the time interval between 2019-08-17T23:48:00Z and just before 2019-08-18T00:00:00Z and the last point in the results covers the time interval between 2019-08-18T00:24:00Z and just before 2019-08-18T00:36:00Z.

LAST()

Returns the field value with the most recent timestamp.

Syntax

  1. SELECT LAST(<field_key>)[,<tag_key(s)>|<field_keys(s)>] FROM_clause [WHERE_clause] [GROUP_BY_clause] [ORDER_BY_clause] [LIMIT_clause] [OFFSET_clause] [SLIMIT_clause] [SOFFSET_clause]

LAST(field_key)
Returns the newest field value (determined by timestamp) associated with the field key.

LAST(/regular_expression/)
Returns the newest field value (determined by timestamp) associated with each field key that matches the regular expression.

LAST(*)
Returns the newest field value (determined by timestamp) associated with each field key in the measurement.

LAST(field_key),tag_key(s),field_key(s)
Returns the newest field value (determined by timestamp) associated with the field key in the parentheses and the relevant tag and/or field.

LAST() supports all field value data types.

Examples

Select the last field values associated with a field key

Return the newest field value (determined by timestamp) associated with the level description field key and in the h2o_feet measurement.

  1. SELECT LAST("level description") FROM "h2o_feet"

name: h2o_feet

timelast
2019-09-17T21:42:00Zbetween 3 and 6 feet

Select the last field values associated with each field key in a measurement

Return the newest field value (determined by timestamp) for each field key in the h2o_feet measurement. The h2o_feet measurement has two field keys: level description and water_level.

  1. SELECT LAST(*) FROM "h2o_feet"

name: h2o_feet

timelast_level descriptionlast_water_level
1970-01-01T00:00:00Zbetween 3 and 6 feet4.938

Select the last field value associated with each field key that matches a regular expression

Return the newest field value for each field key that includes the word level in the h2o_feet measurement.

  1. SELECT LAST(/level/) FROM "h2o_feet"

name: h2o_feet

timelast_level descriptionlast_water_level
1970-01-01T00:00:00Zbetween 3 and 6 feet4.938

Select the last field value associated with a field key and the relevant tags and fields

Return the newest field value (determined by timestamp) in the level description field key and the relevant values of the location tag key and the water_level field key.

  1. SELECT LAST("level description"),"location","water_level" FROM "h2o_feet"

name: h2o_feet

timelastlocationwater_level
2019-09-17T21:42:00Zbetween 3 and 6 feetsanta_monica4.938

Select the last field value associated with a field key and include several clauses

Return the newest field value (determined by timestamp) in the water_level field key in the time range between 2019-08-17T23:48:00Z and 2019-08-18T00:54:00Z and groups results into 12-minute time intervals and per tag. Then fill empty time intervals with 9.01, and it limit the number of points and series returned to four and one.

  1. SELECT LAST("water_level") FROM "h2o_feet" WHERE time >= '2019-08-17T23:48:00Z' AND time <= '2019-08-18T00:54:00Z' GROUP BY time(12m),* fill(9.01) LIMIT 4 SLIMIT 1

name: h2o_feet tags: location=coyote_creek

timelast
2019-08-17T23:48:00Z8.570
2019-08-18T00:00:00Z8.419
2019-08-18T00:12:00Z8.225
2019-08-18T00:24:00Z8.012

Notice that the GROUP BY time() clause overrides the points’ original timestamps. The timestamps in the results indicate the the start of each 12-minute time interval; the first point in the results covers the time interval between 2019-08-17T23:48:00Z and just before 2019-08-18T00:00:00Z and the last point in the results covers the time interval between 2019-08-18T00:24:00Z and just before 2019-08-18T00:36:00Z.

MAX()

Returns the greatest field value.

Syntax

  1. SELECT MAX(<field_key>)[,<tag_key(s)>|<field__key(s)>] FROM_clause [WHERE_clause] [GROUP_BY_clause] [ORDER_BY_clause] [LIMIT_clause] [OFFSET_clause] [SLIMIT_clause] [SOFFSET_clause]

MAX(field_key)
Returns the greatest field value associated with the field key.

MAX(/regular_expression/)
Returns the greatest field value associated with each field key that matches the regular expression.

MAX(*)
Returns the greatest field value associated with each field key in the measurement.

MAX(field_key),tag_key(s),field_key(s)
Returns the greatest field value associated with the field key in the parentheses and the relevant tag and/or field.

MAX() supports int64 and float64 field value data types.

Examples

Select the maximum field value associated with a field key

Return the greatest field value in the water_level field key and in the h2o_feet measurement.

  1. SELECT MAX("water_level") FROM "h2o_feet"

name: h2o_feet

timemax
2019-08-28T07:24:00Z9.964

Select the maximum field value associated with each field key in a measurement

Return the greatest field value for each field key that stores numeric values in the h2o_feet measurement. The h2o_feet measurement has one numeric field: water_level.

  1. SELECT MAX(*) FROM "h2o_feet"

name: h2o_feet

timemax_water_level
2019-08-28T07:24:00Z9.964

Select the maximum field value associated with each field key that matches a regular expression

Return the greatest field value for each field key that stores numeric values and includes the word water in the h2o_feet measurement.

  1. SELECT MAX(/level/) FROM "h2o_feet"

name: h2o_feet

timemax_water_level
2019-08-28T07:24:00Z9.964

Select the maximum field value associated with a field key and the relevant tags and fields

Return the greatest field value in the water_level field key and the relevant values of the location tag key and the level description field key.

  1. SELECT MAX("water_level"),"location","level description" FROM "h2o_feet"

name: h2o_feet

timemaxlocationlevel description
2019-08-28T07:24:00Z9.964coyote_creekat or greater than 9 feet

Select the maximum field value associated with a field key and include several clauses

Return the greatest field value in the water_level field key in the time range between 2019-08-17T23:48:00Z and 2019-08-18T00:54:00Z and groups results into 12-minute time intervals and per tag. Then fill empty time intervals with 9.01, and it limit the number of points and series returned to four and one.

  1. SELECT MAX("water_level") FROM "h2o_feet" WHERE time >= '2019-08-17T23:48:00Z' AND time <= '2019-08-18T00:54:00Z' GROUP BY time(12m),* fill(9.01) LIMIT 4 SLIMIT 1

name: h2o_feet tags: location=coyote_creek

timemax
2019-08-17T23:48:00Z8.635
2019-08-18T00:00:00Z8.504
2019-08-18T00:12:00Z8.320
2019-08-18T00:24:00Z8.130

Notice that the GROUP BY time() clause overrides the points’ original timestamps. The timestamps in the results indicate the the start of each 12-minute time interval; the first point in the results covers the time interval between 2019-08-17T23:48:00Z and just before 2019-08-18T00:00:00Z and the last point in the results covers the time interval between 2019-08-18T00:24:00Z and just before 2019-08-18T00:36:00Z.

MIN()

Returns the lowest field value.

Syntax

  1. SELECT MIN(<field_key>)[,<tag_key(s)>|<field_key(s)>] FROM_clause [WHERE_clause] [GROUP_BY_clause] [ORDER_BY_clause] [LIMIT_clause] [OFFSET_clause] [SLIMIT_clause] [SOFFSET_clause]

MIN(field_key)
Returns the lowest field value associated with the field key.

MIN(/regular_expression/)
Returns the lowest field value associated with each field key that matches the regular expression.

MIN(*)
Returns the lowest field value associated with each field key in the measurement.

MIN(field_key),tag_key(s),field_key(s)
Returns the lowest field value associated with the field key in the parentheses and the relevant tag and/or field.

MIN() supports int64 and float64 field value data types.

Examples

Select the minimum field value associated with a field key

Return the lowest field value in the water_level field key and in the h2o_feet measurement.

  1. SELECT MIN("water_level") FROM "h2o_feet"

name: h2o_feet

timemin
2019-08-28T14:30:00Z-0.610

Select the minimum field value associated with each field key in a measurement

Return the lowest field value for each field key that stores numeric values in the h2o_feet measurement. The h2o_feet measurement has one numeric field: water_level.

  1. SELECT MIN(*) FROM "h2o_feet"

name: h2o_feet

timemin_water_level
2019-08-28T14:30:00Z-0.610

Select the minimum field value associated with each field key that matches a regular expression

Return the lowest field value for each numeric field with water in the field key in the h2o_feet measurement.

  1. SELECT MIN(/level/) FROM "h2o_feet"

name: h2o_feet

timemin_water_level
2019-08-28T14:30:00Z-0.610

Select the minimum field value associated with a field key and the relevant tags and fields

Return the lowest field value in the water_level field key and the relevant values of the location tag key and the level description field key.

  1. SELECT MIN("water_level"),"location","level description" FROM "h2o_feet"

name: h2o_feet

timeminlocationlevel description
2019-08-28T14:30:00Z-0.610coyote_creekbelow 3 feet

Select the minimum field value associated with a field key and include several clauses

Return the lowest field value in the water_level field key in the time range between 2019-08-17T23:48:00Z and 2019-08-18T00:54:00Z and groups results into 12-minute time intervals and per tag. Then fill empty time intervals with 9.01, and it limit the number of points and series returned to four and one.

  1. SELECT MIN("water_level") FROM "h2o_feet" WHERE time >= '2019-08-17T23:48:00Z' AND time <= '2019-08-18T00:54:00Z' GROUP BY time(12m),* fill(9.01) LIMIT 4 SLIMIT 1

name: h2o_feet tags: location=coyote_creek

timemin
2019-08-17T23:48:00Z8.570
2019-08-18T00:00:00Z8.419
2019-08-18T00:12:00Z8.225
2019-08-18T00:24:00Z8.012

Notice that the GROUP BY time() clause overrides the points’ original timestamps. The timestamps in the results indicate the the start of each 12-minute time interval; the first point in the results covers the time interval between 2019-08-17T23:48:00Z and just before 2019-08-18T00:00:00Z and the last point in the results covers the time interval between 2019-08-18T00:24:00Z and just before 2019-08-18T00:36:00Z.

PERCENTILE()

Returns the Nth percentile field value.

Syntax

  1. SELECT PERCENTILE(<field_key>, <N>)[,<tag_key(s)>|<field_key(s)>] FROM_clause [WHERE_clause] [GROUP_BY_clause] [ORDER_BY_clause] [LIMIT_clause] [OFFSET_clause] [SLIMIT_clause] [SOFFSET_clause]

PERCENTILE(field_key,N)
Returns the Nth percentile field value associated with the field key.

PERCENTILE(/regular_expression/,N)
Returns the Nth percentile field value associated with each field key that matches the regular expression.

PERCENTILE(*,N)
Returns the Nth percentile field value associated with each field key in the measurement.

PERCENTILE(field_key,N),tag_key(s),field_key(s)
Returns the Nth percentile field value associated with the field key in the parentheses and the relevant tag and/or field.

N must be an integer or floating point number between 0 and 100, inclusive. PERCENTILE() supports int64 and float64 field value data types.

Examples

Select the fifth percentile field value associated with a field key

Return the field value that is larger than five percent of the field values in the water_level field key and in the h2o_feet measurement.

  1. SELECT PERCENTILE("water_level",5) FROM "h2o_feet"

name: h2o_feet

timepercentile
2019-09-01T17:54:00Z1.122

Select the fifth percentile field value associated with each field key in a measurement

Return the field value that is larger than five percent of the field values in each field key that stores numeric values in the h2o_feet measurement. The h2o_feet measurement has one numeric field: water_level.

  1. SELECT PERCENTILE(*,5) FROM "h2o_feet"

name: h2o_feet

timepercentile_water_level
2019-09-01T17:54:00Z1.122

Select fifth percentile field value associated with each field key that matches a regular expression

Return the field value that is larger than five percent of the field values in each numeric field with water in the field key.

  1. SELECT PERCENTILE(/level/,5) FROM "h2o_feet"

name: h2o_feet

timepercentile_water_level
2019-09-01T17:54:00Z1.122

Select the fifth percentile field values associated with a field key and the relevant tags and fields

Return the field value that is larger than five percent of the field values in the water_level field key and the relevant values of the location tag key and the level description field key.

  1. SELECT PERCENTILE("water_level",5),"location","level description" FROM "h2o_feet"

name: h2o_feet

timepercentilelocationlevel description
2019-08-24T10:18:00Z1.122coyote_creekbelow 3 feet

Select the twentieth percentile field value associated with a field key and include several clauses

Return the field value that is larger than 20 percent of the values in the water_level field in the time range between 2019-08-17T23:48:00Z and 2019-08-18T00:54:00Z and group results into 24-minute intervals. Then fill empty time intervals with 15 and limit the number of points returned to two.

  1. SELECT PERCENTILE("water_level",20) FROM "h2o_feet" WHERE time >= '2019-08-17T23:48:00Z' AND time <= '2019-08-18T00:54:00Z' GROUP BY time(24m) fill(15) LIMIT 2

name: h2o_feet

timepercentile
2019-08-17T23:36:00Z2.398
2019-08-18T00:00:00Z2.343

Notice that the GROUP BY time() clause overrides the points’ original timestamps. The timestamps in the results indicate the the start of each 24-minute time interval; the first point in the results covers the time interval between 2019-08-17T23:36:00Z and just before 2019-08-18T00:00:00Z and the last point in the results covers the time interval between 2019-08-18T00:00:00Z and just before 2019-08-18T00:24:00Z.

Common issues with PERCENTILE()

PERCENTILE() compared to other InfluxQL functions

  • PERCENTILE(<field_key>,100) is equivalent to MAX().
  • PERCENTILE(<field_key>, 50) is nearly equivalent to MEDIAN(), except the MEDIAN() function returns the average of the two middle values if the field key contains an even number of field values.
  • PERCENTILE(<field_key>,0) is not equivalent to MIN(). This is a known issue.

SAMPLE()

Returns a random sample of N field values. SAMPLE() uses reservoir sampling to generate the random points.

Syntax

  1. SELECT SAMPLE(<field_key>, <N>)[,<tag_key(s)>|<field_key(s)>] FROM_clause [WHERE_clause] [GROUP_BY_clause] [ORDER_BY_clause] [LIMIT_clause] [OFFSET_clause] [SLIMIT_clause] [SOFFSET_clause]

SAMPLE(field_key,N)
Returns N randomly selected field values associated with the field key.

SAMPLE(/regular_expression/,N)
Returns N randomly selected field values associated with each field key that matches the regular expression.

SAMPLE(*,N)
Returns N randomly selected field values associated with each field key in the measurement.

SAMPLE(field_key,N),tag_key(s),field_key(s)
Returns N randomly selected field values associated with the field key in the parentheses and the relevant tag and/or field.

N must be an integer. SAMPLE() supports all field value data types.

Examples

Select a sample of the field values associated with a field key

Return two randomly selected points from the water_level field key and in the h2o_feet measurement.

  1. SELECT SAMPLE("water_level",2) FROM "h2o_feet"

name: h2o_feet

timesample
2019-08-22T03:42:00Z7.218
2019-08-28T20:18:00Z2.848

Select a sample of the field values associated with each field key in a measurement

Return two randomly selected points for each field key in the h2o_feet measurement. The h2o_feet measurement has two field keys: level description and water_level.

  1. SELECT SAMPLE(*,2) FROM "h2o_feet"

name: h2o_feet

timesample_level descriptionsample_water_level
2019-08-23T17:30:00Zbelow 3 feet
2019-09-08T19:18:00Z8.379
2019-09-09T03:54:00Zbetween 6 and 9 feet
2019-09-16T04:48:00Z1.437

Select a sample of the field values associated with each field key that matches a regular expression

Return two randomly selected points for each field key that includes the word level in the h2o_feet measurement.

  1. SELECT SAMPLE(/level/,2) FROM "h2o_feet"

name: h2o_feet

timesample_level descriptionsample_water_level
2019-08-19T20:24:00Z4.951
2019-08-26T06:30:00Zbelow 3 feet
2019-09-10T09:06:00Z1.312
2019-09-16T21:00:00Zbetween 3 and 6 feet

Select a sample of the field values associated with a field key and the relevant tags and fields

Return two randomly selected points from the water_level field key and the relevant values of the location tag and the level description field.

  1. SELECT SAMPLE("water_level",2),"location","level description" FROM "h2o_feet"

name: h2o_feet

timesamplelocationlevel description
2019-08-31T04:30:00Z4.954santa_monicabetween 3 and 6 feet
2019-09-13T01:24:00Z3.389santa_monicabetween 3 and 6 feet

Select a sample of the field values associated with a field key and include several clauses

Return one randomly selected point from the water_level field key in the time range between 2019-08-18T00:00:00Z and 2019-08-18T00:30:00Z and group results into 18-minute intervals.

  1. SELECT SAMPLE("water_level",1) FROM "h2o_feet" WHERE time >= '2019-08-18T00:00:00Z' AND time <= '2019-08-18T00:30:00Z' AND "location" = 'santa_monica' GROUP BY time(18m)

name: h2o_feet

timesample
2019-08-18T00:12:00Z2.343
2019-08-18T00:24:00Z2.264

Notice that the GROUP BY time() clause does not override the points’ original timestamps. See Issue 1 in the section below for a more detailed explanation of that behavior.

Common issues with SAMPLE()

SAMPLE() with a GROUP BY time() clause

Queries with SAMPLE() and a GROUP BY time() clause return the specified number of points (N) per GROUP BY time() interval. For most GROUP BY time() queries, the returned timestamps mark the start of the GROUP BY time() interval. GROUP BY time() queries with the SAMPLE() function behave differently; they maintain the timestamp of the original data point.

Example

The query below returns two randomly selected points per 18-minute GROUP BY time() interval. Notice that the returned timestamps are the points’ original timestamps; they are not forced to match the start of the GROUP BY time() intervals.

  1. SELECT SAMPLE("water_level",2) FROM "h2o_feet" WHERE time >= '2019-08-18T00:00:00Z' AND time <= '2019-08-18T00:30:00Z' AND "location" = 'santa_monica' GROUP BY time(18m)

name: h2o_feet

timesample
2019-08-18T00:06:00Z2.116
2019-08-18T00:12:00Z2.028
2019-08-18T00:18:00Z2.126
2019-08-18T00:30:00Z2.051

Notice that the first two rows are randomly-selected points from the first time interval and the last two rows are randomly-selected points from the second time interval.

TOP()

Returns the greatest N field values.

Syntax

  1. SELECT TOP( <field_key>[,<tag_key(s)>],<N> )[,<tag_key(s)>|<field_key(s)>] FROM_clause [WHERE_clause] [GROUP_BY_clause] [ORDER_BY_clause] [LIMIT_clause] [OFFSET_clause] [SLIMIT_clause] [SOFFSET_clause]

TOP(field_key,N)
Returns the greatest N field values associated with the field key.

TOP(field_key,tag_key(s),N)
Returns the greatest field value for N tag values of the tag key.

TOP(field_key,N),tag_key(s),field_key(s)
Returns the greatest N field values associated with the field key in the parentheses and the relevant tag and/or field.

TOP() supports int64 and float64 field value data types.

Note: TOP() returns the field value with the earliest timestamp if there’s a tie between two or more values for the greatest value.

Examples

Select the top three field values associated with a field key

Return the greatest three field values in the water_level field key and in the h2o_feet measurement.

  1. SELECT TOP("water_level",3) FROM "h2o_feet"

name: h2o_feet

timetop
2019-08-28T07:18:00Z9.957
2019-08-28T07:24:00Z9.964
2019-08-28T07:30:00Z9.954

Select the top field value associated with a field key for two tags

Return the greatest field values in the water_level field key for two tag values associated with the location tag key.

  1. SELECT TOP("water_level","location",2) FROM "h2o_feet"

name: h2o_feet

timetoplocation
2019-08-28T03:54:00Z7.205santa_monica
2019-08-28T07:24:00Z9.964coyote_creek

Select the top four field values associated with a field key and the relevant tags and fields

Return the greatest four field values in the water_level field key and the relevant values of the location tag key and the level description field key.

  1. SELECT TOP("water_level",4),"location","level description" FROM "h2o_feet"

name: h2o_feet

timetoplocationlevel description
2019-08-28T07:18:00Z9.957coyote_creekat or greater than 9 feet
2019-08-28T07:24:00Z9.964coyote_creekat or greater than 9 feet
2019-08-28T07:30:00Z9.954coyote_creekat or greater than 9 feet
2019-08-28T07:36:00Z9.941coyote_creekat or greater than 9 feet

Select the top three field values associated with a field key and include several clauses

Return the greatest three values in the water_level field key for each 24-minute interval between 2019-08-18T00:00:00Z and 2019-08-18T00:54:00Z with results in descending timestamp order.

  1. SELECT TOP("water_level",3),"location" FROM "h2o_feet" WHERE time >= '2019-08-18T00:00:00Z' AND time <= '2019-08-18T00:54:00Z' GROUP BY time(24m) ORDER BY time DESC

name: h2o_feet

timetoplocation
2019-08-18T00:54:00Z6.982coyote_creek
2019-08-18T00:54:00Z2.054santa_monica
2019-08-18T00:48:00Z7.110coyote_creek
2019-08-18T00:36:00Z7.372coyote_creek
2019-08-18T00:30:00Z7.500coyote_creek
2019-08-18T00:24:00Z7.635coyote_creek
2019-08-18T00:12:00Z7.887coyote_creek
2019-08-18T00:06:00Z8.005coyote_creek
2019-08-18T00:00:00Z8.120coyote_creek

Notice that the GROUP BY time() clause does not override the points’ original timestamps. See Issue 1 in the section below for a more detailed explanation of that behavior.

Common issues with TOP()

TOP() with a GROUP BY time() clause

Queries with TOP() and a GROUP BY time() clause return the specified number of points per GROUP BY time() interval. For most GROUP BY time() queries, the returned timestamps mark the start of the GROUP BY time() interval. GROUP BY time() queries with the TOP() function behave differently; they maintain the timestamp of the original data point.

Example

The query below returns two points per 18-minute GROUP BY time() interval. Notice that the returned timestamps are the points’ original timestamps; they are not forced to match the start of the GROUP BY time() intervals.

  1. SELECT TOP("water_level",2) FROM "h2o_feet" WHERE time >= '2019-08-18T00:00:00Z' AND time <= '2019-08-18T00:30:00Z' AND "location" = 'santa_monica' GROUP BY time(18m)

name: h2o_feet

timetop
2019-08-18T00:00:00Z2.064
2019-08-18T00:06:00Z2.116
2019-08-18T00:18:00Z2.126
2019-08-18T00:30:00Z2.051

Notice that the first two rows are the greatest points for the first time interval and the last two rows are the greatest points for the second time interval.

TOP() and a tag key with fewer than N tag values

Queries with the syntax SELECT TOP(<field_key>,<tag_key>,<N>) can return fewer points than expected. If the tag key has X tag values, the query specifies N values, and X is smaller than N, then the query returns X points.

Example

The query below asks for the greatest field values of water_level for three tag values of the location tag key. Because the location tag key has two tag values (santa_monica and coyote_creek), the query returns two points instead of three.

  1. SELECT TOP("water_level","location",3) FROM "h2o_feet"

name: h2o_feet

timetoplocation
2019-08-29T03:54:00Z7.205santa_monica
2019-08-29T07:24:00Z9.964coyote_creek