Skip to content

Commit ba7f109

Browse files
committed
feat(sql): Add Window aggregate and Ranking functions to SQL reference index
1 parent 733bd67 commit ba7f109

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

content/shared/sql-reference/_index.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,58 @@ FROM "h2o_feet"
582582
GROUP BY "location"
583583
```
584584

585+
### Window aggregate functions
586+
587+
Window functions let you calculate running totals, moving averages, or other
588+
aggregate-like results without collapsing rows into groups
589+
(unlike non-window aggregate functions).
590+
591+
Window aggregate functions include **all [aggregate functions](#aggregate-functions/)**
592+
and the [ranking functions](#ranking-functions).
593+
The SQL `OVER` clause syntactically distinguishes a window
594+
function from a non-window or aggregate function and defines how to group and
595+
order rows for the window operation.
596+
597+
#### Examples:
598+
599+
{{% influxdb/custom-timestamps %}}
600+
601+
```sql
602+
SELECT
603+
time,
604+
room,
605+
temp,
606+
avg(temp) OVER (PARTITION BY room) AS avg_room_temp
607+
FROM
608+
home
609+
WHERE
610+
time >= '2022-01-01T08:00:00Z'
611+
AND time <= '2022-01-01T09:00:00Z'
612+
ORDER BY
613+
room,
614+
time
615+
```
616+
617+
| time | room | temp | avg_room_temp |
618+
| :------------------ | :---------- | ---: | ------------: |
619+
| 2022-01-01T08:00:00 | Kitchen | 21.0 | 22.0 |
620+
| 2022-01-01T09:00:00 | Kitchen | 23.0 | 22.0 |
621+
| 2022-01-01T08:00:00 | Living Room | 21.1 | 21.25 |
622+
| 2022-01-01T09:00:00 | Living Room | 21.4 | 21.25 |
623+
624+
{{% /influxdb/custom-timestamps %}}
625+
626+
#### Ranking Functions
627+
628+
| Function | Description |
629+
| :------- | :--------------------------------------------------------- |
630+
| CUME_DIST() | Returns the cumulative distribution of a value within a group of values |
631+
| DENSE_RANK() | Returns a rank for each row without gaps in the numbering |
632+
| NTILE() | Distributes the rows in an ordered partition into the specified number of groups |
633+
| PERCENT_RANK() | Returns the percentage rank of the current row within its partition |
634+
| RANK() | Returns the rank of the current row in its partition, allowing gaps between ranks |
635+
| ROW_NUMBER() | Returns the position of the current row in its partition |
636+
585637
### Selector functions
586638

587639
Selector functions are unique to InfluxDB. They behave like aggregate functions in that they take a row of data and compute it down to a single value. However, selectors are unique in that they return a **time value** in addition to the computed value. In short, selectors return an aggregated value along with a timestamp.

0 commit comments

Comments
 (0)