SQL aggregate functions (#4748)

* fix iox ui details (#4660)

* fixed left nav for iox

* updated nav order

* one more nav fix

* added sql data types doc to iox

* removed, need to create separate branch

* IOx get started (#4676)

* WIP iox get started

* WIP iox get started

* WIP iox get started

* WIP iox get-started

* WIP get-started docs

* iox get started setup

* added custom times and datepicker to iox getting started

* finished sample data date picker

* WIP get started querying

* wrapped up new getting started content

* fixed unclosed shortcode

* fixed js bug, updated get started to address PR feedback

* removed influxdbu banner from iox-get-started

* fixed typos

* Migrate data to IOx (#4704)

* WIP iox get started

* WIP iox get started

* WIP iox get started

* WIP iox get-started

* WIP get-started docs

* iox get started setup

* added custom times and datepicker to iox getting started

* finished sample data date picker

* WIP get started querying

* wrapped up new getting started content

* fixed unclosed shortcode

* fixed js bug, updated get started to address PR feedback

* removed influxdbu banner from iox-get-started

* add tsm to iox migration guide

* WIP 1.x iox migration

* WIP iox migration guides

* iox migration landing page content

* updated migration docs to address PR feedback

* one last PR feedback update

* added sql reference for review

* moved reference to sql folder

* removed file

* Schema recommendations for IOx (#4701)

* WIP iox get started

* WIP iox get started

* WIP iox get started

* WIP iox get-started

* WIP get-started docs

* iox get started setup

* added custom times and datepicker to iox getting started

* finished sample data date picker

* WIP get started querying

* wrapped up new getting started content

* fixed unclosed shortcode

* fixed js bug, updated get started to address PR feedback

* removed influxdbu banner from iox-get-started

* schema design recommendations

* add heading color styles

* fixed typos and formatting

* fixed typos

* fixed line protocol descrepencies

* fixed typo

* IOx landing page and notification (#4717)

* updated cloud iox landing page

* added state of the docs notification, removed addition resources from nav

* updated iox page titles

* updated duplicate oss and product data

* add order by doc (#4710)

* add order by doc

* Update content/influxdb/cloud-iox/order-by.md

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* Update content/influxdb/cloud-iox/order-by.md

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* Update content/influxdb/cloud-iox/order-by.md

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* Update content/influxdb/cloud-iox/order-by.md

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* Update content/influxdb/cloud-iox/order-by.md

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* Update content/influxdb/cloud-iox/order-by.md

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* Update content/influxdb/cloud-iox/order-by.md

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* Update content/influxdb/cloud-iox/order-by.md

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* Update content/influxdb/cloud-iox/order-by.md

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* Update content/influxdb/cloud-iox/order-by.md

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* Update content/influxdb/cloud-iox/order-by.md

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* Update content/influxdb/cloud-iox/order-by.md

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* Update content/influxdb/cloud-iox/order-by.md

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* Update content/influxdb/cloud-iox/order-by.md

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* added select doc (#4708)

* added select doc

* Update content/influxdb/cloud-iox/select.md

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* Update content/influxdb/cloud-iox/select.md

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* Update content/influxdb/cloud-iox/select.md

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* Update content/influxdb/cloud-iox/select.md

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* Update content/influxdb/cloud-iox/select.md

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* Update content/influxdb/cloud-iox/select.md

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* Update content/influxdb/cloud-iox/select.md

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* Update content/influxdb/cloud-iox/select.md

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* Update content/influxdb/cloud-iox/select.md

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* Update content/influxdb/cloud-iox/select.md

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* Update content/influxdb/cloud-iox/select.md

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* Update content/influxdb/cloud-iox/select.md

Co-authored-by: Andrew Lamb <andrew@nerdnetworks.org>

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>
Co-authored-by: Andrew Lamb <andrew@nerdnetworks.org>

* having clause (#4713)

* added having clause

* Update content/influxdb/cloud-iox/having.md

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* Update content/influxdb/cloud-iox/having.md

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* Update content/influxdb/cloud-iox/having.md

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* Update content/influxdb/cloud-iox/having.md

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* Update content/influxdb/cloud-iox/having.md

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* Update content/influxdb/cloud-iox/having.md

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* Update content/influxdb/cloud-iox/having.md

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* Apply suggestions from code review

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* added sql-data-types branch and corresponding doc (#4700)

* added sql-data-types branch and corresponding doc

* Update content/influxdb/cloud-iox/sql-data-types.md

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* Update content/influxdb/cloud-iox/sql-data-types.md

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* Update content/influxdb/cloud-iox/sql-data-types.md

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* Update content/influxdb/cloud-iox/sql-data-types.md

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* Update content/influxdb/cloud-iox/sql-data-types.md

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* Update content/influxdb/cloud-iox/sql-data-types.md

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* Update content/influxdb/cloud-iox/sql-data-types.md

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* Update content/influxdb/cloud-iox/sql-data-types.md

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* Update content/influxdb/cloud-iox/sql-data-types.md

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* Update content/influxdb/cloud-iox/sql-data-types.md

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* Update content/influxdb/cloud-iox/sql-data-types.md

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* Update content/influxdb/cloud-iox/sql-data-types.md

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* Update content/influxdb/cloud-iox/sql-data-types.md

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* Update content/influxdb/cloud-iox/sql-data-types.md

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* Update content/influxdb/cloud-iox/sql-data-types.md

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* Update content/influxdb/cloud-iox/sql-data-types.md

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* Update content/influxdb/cloud-iox/sql-data-types.md

Co-authored-by: Andrew Lamb <andrew@nerdnetworks.org>

* Update content/influxdb/cloud-iox/sql-data-types.md

Co-authored-by: Andrew Lamb <andrew@nerdnetworks.org>

* Update content/influxdb/cloud-iox/sql-data-types.md

Co-authored-by: Andrew Lamb <andrew@nerdnetworks.org>

* added interval

* fixed formatting

* more format fixes

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>
Co-authored-by: Andrew Lamb <andrew@nerdnetworks.org>

* add limit doc (#4711)

* add limit doc

* Update content/influxdb/cloud-iox/limit.md

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* Update content/influxdb/cloud-iox/limit.md

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* Update content/influxdb/cloud-iox/limit.md

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* Update content/influxdb/cloud-iox/limit.md

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* Update content/influxdb/cloud-iox/limit.md

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* Update content/influxdb/cloud-iox/limit.md

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* Update content/influxdb/cloud-iox/limit.md

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* Update content/influxdb/cloud-iox/limit.md

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* Update content/influxdb/cloud-iox/limit.md

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* Update content/influxdb/cloud-iox/limit.md

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* Update content/influxdb/cloud-iox/limit.md

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* added group by (#4721)

* added group by

* Update content/influxdb/cloud-iox/group-by.md

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* Update content/influxdb/cloud-iox/group-by.md

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* Update content/influxdb/cloud-iox/group-by.md

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* Update content/influxdb/cloud-iox/group-by.md

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* Update content/influxdb/cloud-iox/group-by.md

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

---------

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* IOx SQL query guides (#4720)

* WIP basic sql query guide

* WIP query guides

* WIP query docs, updated query format

* fleshed out sql aggregate query doc

* updated aggregate query guide, added explore schema guide

* fixed getting started link

* IOx Grafana and Superset documentation (#4723)

* iox grafana and superset documentation

* updates to the superset and grafana docs

* chore(grafana): Rework the documentation for a release instead of from source. (#4724)

* chore(grafana): Rework the documentation for a release instead of from source.

* chore: Typo.

* chore: v0.1.0 will be the first release.

* updates to address PR feedback

* a few minor updates to the grafana doc

* another minor update to grafana

* fixed grafana archive name

---------

Co-authored-by: Brett Buddin <brett@buddin.org>

* rearranged docs

* fix order by description

* updated more sql reference doc descriptions

* Add SQL selector functions (#4725)

* WIP selector functions

* WIP selector fns

* wrapped up sql selector functions

* relocated function docs

* add iox regions doc

* add messaging to guide users to the correct docs (#4728)

* minor changes

* added Flux reference

* updated algolia tagging

* add delete information to iox docs (#4727)

* fixed typos

* Add write content to the IOx docs (#4729)

* ported telegraf write docs to iox

* write content and updated reference

* updated node deps

* added link to selectors reference

* WIP aggregate functions

* WIP aggregate functions

* added approx aggregate functions

* added related links and page navs

---------

Co-authored-by: lwandzura <51929958+lwandzura@users.noreply.github.com>
Co-authored-by: Andrew Lamb <andrew@nerdnetworks.org>
Co-authored-by: Brett Buddin <brett@buddin.org>
pull/4750/head
Scott Anderson 2023-02-07 08:44:41 -07:00 committed by GitHub
parent 1d733e70ca
commit 0e55403af9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 757 additions and 19 deletions

View File

@ -11,6 +11,9 @@ menu:
identifier: query-sql-aggregate
weight: 203
influxdb/cloud-iox/tags: [query, sql]
related:
- /influxdb/cloud-iox/reference/sql/functions/aggregate/
- /influxdb/cloud-iox/reference/sql/functions/selector/
list_code_example: |
##### Aggregate fields by groups
```sql
@ -74,7 +77,7 @@ value of `room`.
Use **aggregate functions** to aggregate values in a specified column for each
group and return a single row per group containing the aggregate value.
[View aggregate functions](#)
[View aggregate functions](/influxdb/cloud-iox/reference/sql/functions/aggregate/)
##### Basic aggregate query
@ -87,7 +90,7 @@ SELECT AVG(co) from home
Use **selector functions** to "select" a value from a specified column.
The available selector functions are designed to work with time series data.
[View selector functions](/influxdb/cloud-iox/reference/sql/functions/selectors/)
[View selector functions](/influxdb/cloud-iox/reference/sql/functions/selector/)
Each selector function returns a Rust _struct_ (similar to a JSON object)
representing a single time and value from the specified column in the each group.

View File

@ -0,0 +1,726 @@
---
title: SQL aggregate functions
list_title: Aggregate functions
description: >
Aggregate data with SQL aggregate functions.
menu:
influxdb_cloud_iox:
name: Aggregate
parent: sql-functions
weight: 301
related:
- /influxdb/cloud-iox/query-data/sql/aggregate-select/
---
SQL aggregate functions aggregate values in a specified column for each
group or SQL partition and return a single row per group containing the
aggregate value.
- [General aggregate functions](#general-aggregate-functions)
- [avg](#avg)
- [count](#count)
- [max](#max)
- [min](#min)
- [sum](#sum)
- [Statistical aggregate functions](#statistical-aggregate-functions)
- [corr](#corr)
- [covar](#covar)
- [covar_pop](#covar_pop)
- [covar_samp](#covar_samp)
- [stddev](#stddev)
- [stddev_pop](#stddev_pop)
- [stddev_samp](#stddev_samp)
- [var](#var)
- [var_pop](#var_pop)
- [var_samp](#var_samp)
- [Approximate aggregate functions](#approximate-aggregate-functions)
- [approx_distinct](#approx_distinct)
- [approx_median](#approx_median)
- [approx_percentile_cont](#approx_percentile_cont)
- [approx_percentile_cont_with_weight](#approx_percentile_cont_with_weight)
---
## General aggregate functions
- [avg](#avg)
- [count](#count)
- [max](#max)
- [min](#min)
- [sum](#sum)
### avg
Returns the average of numeric values in the specified column.
```sql
avg(expression)
```
##### Arguments:
- **expression**: Column to operate on.
##### Aliases:
- `mean`
{{< expand-wrapper >}}
{{% expand "View `avg` query example" %}}
```sql
SELECT
location,
avg(water_level) AS water_level_avg
FROM h2o_feet
GROUP BY location
```
| location | water_level_avg |
| :----------- | -----------------: |
| coyote_creek | 5.359142420303919 |
| santa_monica | 3.5307120942458843 |
{{% /expand %}}
{{< /expand-wrapper >}}
### count
Returns the number of rows in the specified column.
Count includes _null_ values in the total count.
To exclude _null_ values from the total count, include `<column> IS NOT NULL`
in the `WHERE` clause.
```sql
count(expression)
```
##### Arguments:
- **expression**: Column to operate on.
{{< expand-wrapper >}}
{{% expand "View `count` query example" %}}
```sql
SELECT
location,
count(water_level) AS water_level_count
FROM h2o_feet
GROUP BY location
```
| location | water_level_count |
| :----------- | ----------------: |
| coyote_creek | 7604 |
| santa_monica | 7654 |
{{% /expand %}}
{{< /expand-wrapper >}}
### max
Returns the maximum value in the specified column.
_To return both the maximum value and its associated timestamp, use
[`selector_max`](/influxdb/cloud-iox/reference/sql/functions/selector/#selector_max)._
##### Arguments:
- **expression**: Column to operate on.
{{< expand-wrapper >}}
{{% expand "View `max` query example" %}}
```sql
SELECT
location,
max(water_level) AS water_level_max
FROM h2o_feet
GROUP BY location
```
| location | water_level_max |
| :----------- | --------------: |
| santa_monica | 7.205 |
| coyote_creek | 9.964 |
{{% /expand %}}
{{< /expand-wrapper >}}
### min
Returns the minimum value in the specified column.
_To return both the minimum value and its associated timestamp, use
[`selector_max`](/influxdb/cloud-iox/reference/sql/functions/selector/#selector_min)._
##### Arguments:
- **expression**: Column to operate on.
{{< expand-wrapper >}}
{{% expand "View `min` query example" %}}
```sql
SELECT
location,
min(water_level) AS water_level_min
FROM h2o_feet
GROUP BY location
```
| location | water_level_min |
| :----------- | --------------: |
| coyote_creek | -0.61 |
| santa_monica | -0.243 |
{{% /expand %}}
{{< /expand-wrapper >}}
### sum
Returns the sum of all values in the specified column.
##### Arguments:
- **expression**: Column to operate on.
{{< expand-wrapper >}}
{{% expand "View `sum` query example" %}}
```sql
SELECT
location,
sum(water_level) AS water_level_sum
FROM h2o_feet
GROUP BY location
```
| location | water_level_sum |
| :----------- | -----------------: |
| santa_monica | 27024.070369358 |
| coyote_creek | 40750.918963991004 |
{{% /expand %}}
{{< /expand-wrapper >}}
## Statistical aggregate functions
- [corr](#corr)
- [covar](#covar)
- [covar_pop](#covar_pop)
- [covar_samp](#covar_samp)
- [stddev](#stddev)
- [stddev_pop](#stddev_pop)
- [stddev_samp](#stddev_samp)
- [var](#var)
- [var_pop](#var_pop)
- [var_samp](#var_samp)
### corr
Returns the coefficient of correlation between two numeric values.
```sql
corr(expression1, expression2)
```
##### Arguments:
- **expression1**: First column or literal value to operate on.
- **expression2**: Second column or literal value to operate on.
{{< expand-wrapper >}}
{{% expand "View `corr` query example" %}}
_The following example uses the sample data set provided in
[Get started with InfluxDB tutorial](/influxdb/cloud-iox/get-started/write/#construct-line-protocol)._
```sql
SELECT
room,
corr(hum, temp) AS correlation
FROM home
GROUP BY room
```
| room | correlation |
| :---------- | ------------------: |
| Living Room | 0.43665270457835725 |
| Kitchen | 0.6741766954929539 |
{{% /expand %}}
{{< /expand-wrapper >}}
### covar
Returns the covariance of a set of number pairs.
```sql
covar(expression1, expression2)
```
##### Arguments:
- **expression1**: First column or literal value to operate on.
- **expression2**: Second column or literal value to operate on.
{{< expand-wrapper >}}
{{% expand "View `covar` query example" %}}
_The following example uses the sample data set provided in
[Get started with InfluxDB tutorial](/influxdb/cloud-iox/get-started/write/#construct-line-protocol)._
```sql
SELECT
room,
covar(hum, temp) AS covar
FROM home
GROUP BY room
```
##### Arguments:
- **expression1**: First column or literal value to operate on.
- **expression2**: Second column or literal value to operate on.
| room | covar |
| :---------- | ------------------: |
| Living Room | 0.03346153846153959 |
| Kitchen | 0.11134615384615432 |
{{% /expand %}}
{{< /expand-wrapper >}}
### covar_pop
Returns the population covariance of a set of number pairs.
```sql
covar_pop(expression1, expression2)
```
##### Arguments:
- **expression1**: First column or literal value to operate on.
- **expression2**: Second column or literal value to operate on.
{{< expand-wrapper >}}
{{% expand "View `covar_pop` query example" %}}
_The following example uses the sample data set provided in
[Get started with InfluxDB tutorial](/influxdb/cloud-iox/get-started/write/#construct-line-protocol)._
```sql
SELECT
room,
covar_pop(hum, temp) AS covar_pop
FROM home
GROUP BY room
```
| room | covar_pop |
| :---------- | -------------------: |
| Kitchen | 0.10278106508875783 |
| Living Room | 0.030887573964498087 |
{{% /expand %}}
{{< /expand-wrapper >}}
### covar_samp
Returns the sample covariance of a set of number pairs.
```sql
covar_samp(expression1, expression2)
```
##### Arguments:
- **expression1**: First column or literal value to operate on.
- **expression2**: Second column or literal value to operate on.
{{< expand-wrapper >}}
{{% expand "View `covar_samp` query example" %}}
_The following example uses the sample data set provided in
[Get started with InfluxDB tutorial](/influxdb/cloud-iox/get-started/write/#construct-line-protocol)._
```sql
SELECT
room,
covar_samp(hum, temp) AS covar_samp
FROM home
GROUP BY room
```
| room | covar_samp |
| :---------- | ------------------: |
| Kitchen | 0.11134615384615432 |
| Living Room | 0.03346153846153959 |
{{% /expand %}}
{{< /expand-wrapper >}}
### stddev
Returns the standard deviation of a set of numbers.
```sql
stddev(expression)
```
##### Arguments:
- **expression**: Column or literal value to operate on.
{{< expand-wrapper >}}
{{% expand "View `stddev` query example" %}}
_The following example uses the sample data set provided in
[Get started with InfluxDB tutorial](/influxdb/cloud-iox/get-started/write/#construct-line-protocol)._
```sql
SELECT
room,
stddev(co) AS stddev
FROM home
GROUP BY room
```
| room | stddev |
| :---------- | ----------------: |
| Living Room | 5.885662718931967 |
| Kitchen | 9.321879418735037 |
{{% /expand %}}
{{< /expand-wrapper >}}
### stddev_pop
Returns the population standard deviation of a set of numbers.
```sql
stddev_pop(expression)
```
##### Arguments:
- **expression**: Column or literal value to operate on.
{{< expand-wrapper >}}
{{% expand "View `stddev_pop` query example" %}}
_The following example uses the sample data set provided in
[Get started with InfluxDB tutorial](/influxdb/cloud-iox/get-started/write/#construct-line-protocol)._
```sql
SELECT
room,
stddev_pop(co) AS stddev_pop
FROM home
GROUP BY room
```
| room | stddev_pop |
| :---------- | ----------------: |
| Kitchen | 8.956172047894082 |
| Living Room | 5.654761830612032 |
{{% /expand %}}
{{< /expand-wrapper >}}
### stddev_samp
Returns the sample standard deviation of a set of numbers.
```sql
stddev_samp(expression)
```
##### Arguments:
- **expression**: Column or literal value to operate on.
{{< expand-wrapper >}}
{{% expand "View `stddev_samp` query example" %}}
_The following example uses the sample data set provided in
[Get started with InfluxDB tutorial](/influxdb/cloud-iox/get-started/write/#construct-line-protocol)._
```sql
SELECT
room,
stddev_samp(co) AS stddev_samp
FROM home
GROUP BY room
```
| room | stddev_samp |
| :---------- | ----------------: |
| Living Room | 5.885662718931967 |
| Kitchen | 9.321879418735037 |
{{% /expand %}}
{{< /expand-wrapper >}}
---
### var
Returns the statistical variance of a set of numbers.
```sql
var(expression)
```
##### Arguments:
- **expression**: Column or literal value to operate on.
{{< expand-wrapper >}}
{{% expand "View `var` query example" %}}
_The following example uses the sample data set provided in
[Get started with InfluxDB tutorial](/influxdb/cloud-iox/get-started/write/#construct-line-protocol)._
```sql
SELECT
room,
var(co) AS var
FROM home
GROUP BY room
```
| room | var |
| :---------- | ----------------: |
| Living Room | 34.64102564102564 |
| Kitchen | 86.89743589743587 |
{{% /expand %}}
{{< /expand-wrapper >}}
### var_pop
Returns the statistical population variance of a set of numbers.
```sql
var_pop(expression)
```
##### Arguments:
- **expression**: Column or literal value to operate on.
{{< expand-wrapper >}}
{{% expand "View `var_pop` query example" %}}
_The following example uses the sample data set provided in
[Get started with InfluxDB tutorial](/influxdb/cloud-iox/get-started/write/#construct-line-protocol)._
```sql
SELECT
room,
var_pop(co) AS var_pop
FROM home
GROUP BY room
```
| room | var_pop |
| :---------- | -----------------: |
| Living Room | 31.976331360946745 |
| Kitchen | 80.21301775147927 |
{{% /expand %}}
{{< /expand-wrapper >}}
### var_samp
Returns the statistical sample variance of a set of numbers.
```sql
var_samp(expression)
```
##### Arguments:
- **expression**: Column or literal value to operate on.
{{< expand-wrapper >}}
{{% expand "View `var_samp` query example" %}}
_The following example uses the sample data set provided in
[Get started with InfluxDB tutorial](/influxdb/cloud-iox/get-started/write/#construct-line-protocol)._
```sql
SELECT
room,
var_samp(co) AS var_samp
FROM home
GROUP BY room
```
| room | var_samp |
| :---------- | ----------------: |
| Kitchen | 86.89743589743587 |
| Living Room | 34.64102564102564 |
{{% /expand %}}
{{< /expand-wrapper >}}
## Approximate aggregate functions
- [approx_distinct](#approx_distinct)
- [approx_median](#approx_median)
- [approx_percentile_cont](#approx_percentile_cont)
- [approx_percentile_cont_with_weight](#approx_percentile_cont_with_weight)
### approx_distinct
Returns the approximate number of distinct input values calculated using the
HyperLogLog algorithm.
```sql
approx_distinct(expression)
```
##### Arguments
- **expression**: Column or literal value to operate on.
{{< expand-wrapper >}}
{{% expand "View `approx_distinct` query example" %}}
_The following example uses the sample data set provided in
[Get started with InfluxDB tutorial](/influxdb/cloud-iox/get-started/write/#construct-line-protocol)._
```sql
SELECT
room,
approx_distinct(co::string) AS approx_distinct
FROM home
GROUP BY room
```
| room | approx_distinct |
| :---------- | --------------: |
| Living Room | 7 |
| Kitchen | 8 |
{{% /expand %}}
{{< /expand-wrapper >}}
### approx_median
Returns the approximate median (50th percentile) of input values.
It is an alias of `approx_percentile_cont(x, 0.5)`.
```sql
approx_median(expression)
```
##### Arguments
- **expression**: Column or literal value to operate on.
{{< expand-wrapper >}}
{{% expand "View `approx_median` query example" %}}
_The following example uses the sample data set provided in
[Get started with InfluxDB tutorial](/influxdb/cloud-iox/get-started/write/#construct-line-protocol)._
```sql
SELECT
room,
approx_median(temp) AS approx_median
FROM home
GROUP BY room
```
| room | approx_median |
| :---------- | ------------: |
| Kitchen | 22.7 |
| Living Room | 22.3 |
{{% /expand %}}
{{< /expand-wrapper >}}
### approx_percentile_cont
Returns the approximate percentile of input values using the t-digest algorithm.
```sql
approx_percentile_cont(expression, percentile, centroids)
```
##### Arguments
- **expression**: Column or literal value to operate on.
- **percentile**: Percentile to compute. Must be a float value between 0 and 1 (inclusive).
- **centroids**: Number of centroids to use in the t-digest algorithm. _Default is 100_.
If there are this number or fewer unique values, you can expect an exact result.
A higher number of centroids results in a more accurate approximation, but
requires more memory to compute.
{{< expand-wrapper >}}
{{% expand "View `approx_percentile_cont` query example" %}}
_The following example uses the sample data set provided in
[Get started with InfluxDB tutorial](/influxdb/cloud-iox/get-started/write/#construct-line-protocol)._
```sql
SELECT
room,
approx_percentile_cont(temp, 0.99) AS "99th_percentile"
FROM home
GROUP BY room
```
| room | 99th_percentile |
| :---------- | --------------: |
| Kitchen | 23.3 |
| Living Room | 22.8 |
{{% /expand %}}
{{< /expand-wrapper >}}
### approx_percentile_cont_with_weight
Returns the weighted approximate percentile of input values using the
t-digest algorithm.
```sql
approx_percentile_cont_with_weight(expression, weight, percentile)
```
##### Arguments
- **expression**: Column or literal value to operate on.
- **weight**: Column or literal value to use as weight.
- **percentile**: Percentile to compute. Must be a float value between 0 and 1 (inclusive).
{{< expand-wrapper >}}
{{% expand "View `approx_percentile_cont_with_weight` query example" %}}
_The following example uses the sample data set provided in
[Get started with InfluxDB tutorial](/influxdb/cloud-iox/get-started/write/#construct-line-protocol)._
```sql
SELECT
room,
approx_percentile_cont_with_weight(temp, co, 0.99) AS "co_weighted_99th_percentile"
FROM home
GROUP BY room
```
| room | co_weighted_99th_percentile |
| :---------- | --------------------------: |
| Kitchen | 23.3 |
| Living Room | 22.8 |
{{% /expand %}}
{{< /expand-wrapper >}}

View File

@ -5,9 +5,11 @@ description: >
Select data with SQL selector functions.
menu:
influxdb_cloud_iox:
name: Selectors
name: Selector
parent: sql-functions
weight: 220
weight: 302
related:
- /influxdb/cloud-iox/query-data/sql/aggregate-select/
---
SQL selector functions are designed to work with time series data.
@ -16,6 +18,13 @@ data and return a single value.
However, selectors are unique in that they return a _struct_ that contains
a **time value** in addition to the computed value.
- [How do selector functions work?](#how-do-selector-functions-work)
- [Selector functions](#selector-functions)
- [selector_min](#selector_min)
- [selector_max](#selector_max)
- [selector_first](#selector_first)
- [selector_last](#selector_last)
## How do selector functions work?
Each selector function returns a Rust _struct_ (similar to a JSON object)
@ -29,7 +38,7 @@ For example, `selector_first` returns the value of specified column in the first
The struct returned from a selector function has two properties:
- **time**: `time` value in the selected row
- **value**: value of the specified column in the selected row
- **expression**: value of the specified column in the selected row
```js
{time: 2023-01-01T00:00:00Z, value: 72.1}
@ -59,15 +68,15 @@ GROUP BY room
### selector_min
`selector_min()` function returns the smallest value of a selected column and a timestamp.
Returns the smallest value of a selected column and a timestamp.
##### Arguments:
- **value**: Column to operate on or a literal value.
- **expression**: Column to operate on or a literal value.
- **timestamp**: Time column or timestamp literal.
```sql
selector_min(<value>, <timestamp>)
selector_min(expression, timestamp)
```
{{< expand-wrapper >}}
@ -89,15 +98,15 @@ FROM h2o_feet
### selector_max
`selector_max()` function returns the smallest value of a selected column and a timestamp.
Returns the largest value of a selected column and a timestamp.
##### Arguments:
- **value**: Column to operate on or a literal value.
- **expression**: Column to operate on or a literal value.
- **timestamp**: Time column or timestamp literal.
```sql
selector_max(<value>, <timestamp>)
selector_max(expression, timestamp)
```
{{< expand-wrapper >}}
@ -119,15 +128,15 @@ FROM h2o_feet
### selector_first
`selector_first()` returns the first value ordered by time ascending.
Returns the first value ordered by time ascending.
##### Arguments:
- **value**: Column to operate on or a literal value.
- **expression**: Column to operate on or a literal value.
- **timestamp**: Time column or timestamp literal.
```sql
selector_first(<value>, <timestamp>)
selector_first(expression, timestamp)
```
{{< expand-wrapper >}}
@ -149,15 +158,15 @@ FROM h2o_feet
### selector_last
`selector_last()` returns the last value ordered by time ascending.
Returns the last value ordered by time ascending.
##### Arguments:
- **value**: Column to operate on or a value literal.
- **expression**: Column to operate on or a value literal.
- **timestamp**: Time column or timestamp literal.
```sql
selector_last(<value>, <timestamp>)
selector_last(expression, timestamp)
```
{{< expand-wrapper >}}

View File

@ -4,4 +4,4 @@
{{- $keepClass := cond ( $keep ) " keep" "" -}}
{{- $isCloud := in $currentVersion "cloud" -}}
{{- $versionText := cond ($isCloud) "Cloud" $currentVersion -}}
<span class="current-version{{ $keepClass }}">{{- $versionText -}}</span>
<span class="current-version{{ $keepClass }}">{{- $versionText -}}</span>

View File

@ -8,7 +8,7 @@
"autoprefixer": ">=10.2.5",
"hugo-extended": ">=0.101.0",
"postcss": ">=8.2.13",
"postcss-cli": "=>9.1.0"
"postcss-cli": ">=9.1.0"
},
"dependencies": {
"axios": "^0.27.2",