You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
perform. However, window functions do not return a single output row per group
6
-
like non-window aggregate functions do. Instead, rows retain their separate
7
-
identities.
2
+
Window functions let you calculate running totals, moving averages, or other aggregate-like results without collapsing rows into groups.
3
+
They perform their calculations over a “window” of rows, which you can partition and order in various ways, and return a calculated value for each row in the set.
4
+
5
+
Unlike non-window [aggregate functions](/influxdb3/version/reference/sql/functions/aggregate/) that combine each group into a single row, window functions preserve each row’s identity and calculate an additional value for every row in the partition.
8
6
9
7
For example, the following query uses the {{< influxdb3/home-sample-link >}}
10
8
and returns each temperature reading with the average temperature per room over
@@ -65,9 +63,10 @@ ORDER BY
65
63
66
64
As window functions operate on a row, there is a set of rows in the row's
67
65
partition that the window function uses to perform the operation. This set of
68
-
rows is called the _window frame_. Window frame boundaries can be defined using
66
+
rows is called the _window frame_.
67
+
Window frame boundaries can be defined using
69
68
`RANGE`, `ROW`, or `GROUPS` frame units, each relative to the current row--for
70
-
exmaple:
69
+
example:
71
70
72
71
{{< code-tabs-wrapper >}}
73
72
{{% code-tabs %}}
@@ -117,11 +116,9 @@ FROM home
117
116
118
117
_For more information about how window frames work, see the [frame clause](#frame-clause)._
119
118
120
-
If window frames are not defined, window functions use all rows in the current
119
+
If you don't specify window frames, window functions use all rows in the current
121
120
partition to perform their operation.
122
121
123
-
## Window function syntax
124
-
125
122
```sql
126
123
function([expr])
127
124
OVER(
@@ -133,10 +130,10 @@ function([expr])
133
130
134
131
### OVER clause
135
132
136
-
Window functions use an `OVER` clause directly following the window function's
137
-
name and arguments. The `OVER` clause syntactically distinguishes a window
138
-
function from a normal function or non-window aggregate function and determines
139
-
how rows are split up for the window operation.
133
+
Window functions use an `OVER` clause that directly follows the window function's
134
+
name and arguments.
135
+
The `OVER` clause syntactically distinguishes a window
136
+
function from a non-window or aggregate function and defines how to group and order rows for the window operation.
140
137
141
138
### PARTITION BY clause
142
139
@@ -154,13 +151,13 @@ may be explicit or implicit, limiting a window frame size in both directions
154
151
relative to the current row.
155
152
156
153
> [!Note]
157
-
> The `ORDER BY` clause in an `OVER` clause is separate from the `ORDER BY`
158
-
> clause of the query and only determines the order that rows in each partition
159
-
> are processed in.
154
+
> The `ORDER BY` clause in an `OVER` clause determines the processing order for
155
+
> rows in each partition and is separate from the `ORDER BY`
156
+
> clause of the query.
160
157
161
158
### Frame clause
162
159
163
-
The frame clause defines window frame boundaries and can be one of the following:
160
+
The _frame clause_ defines window frame boundaries and can be one of the following:
164
161
165
162
```sql
166
163
{ RANGE | ROWS | GROUPS } frame_start
@@ -196,7 +193,7 @@ the current row value.
196
193
> When using `RANGE` frame units, you must include an `ORDER BY` clause with
197
194
> _exactly one column_.
198
195
199
-
The offset is the difference the between the current row value and surrounding
196
+
The offset is the difference between the current row value and surrounding
200
197
row values. `RANGE` supports the following offset types:
201
198
202
199
- Numeric _(non-negative)_
@@ -207,7 +204,7 @@ row values. `RANGE` supports the following offset types:
207
204
{{% expand "See how `RANGE` frame units work with numeric offsets" %}}
208
205
209
206
To use a numeric offset with the `RANGE` frame unit, you must sort partitions
210
-
by a numeric-typed column.
207
+
by a numeric-type column.
211
208
212
209
```sql
213
210
... OVER (
@@ -226,7 +223,7 @@ The window frame includes rows with sort column values between 45 below and
226
223
{{% expand "See how `RANGE` frame units work with interval offsets" %}}
227
224
228
225
To use an interval offset with the `RANGE` frame unit, you must sort partitions
229
-
by `time` or a timestamp-typed column.
226
+
by `time` or a timestamp-type column.
230
227
231
228
```sql
232
229
... OVER (
@@ -251,7 +248,7 @@ one hour after the current row's timestamp:
251
248
252
249
##### ROWS
253
250
254
-
Defines frame boundaries using row positions relative to the current row.
251
+
Defines window frame boundaries using row positions relative to the current row.
255
252
The offset is the difference in row position from the current row.
256
253
`ROWS` supports the following offset types:
257
254
@@ -279,14 +276,14 @@ The window frame includes the two rows before and the one row after the current
279
276
280
277
##### GROUPS
281
278
282
-
Defines frame boundaries using row groups.
279
+
Defines window frame boundaries using row groups.
283
280
Rows with the same values for the columns in the [`ORDER BY` clause](#order-by-clause)
284
281
comprise a row group.
285
282
286
283
> [!Important]
287
-
> When using `GROUPS` frame units, you must include an `ORDER BY` clause.
284
+
> When using `GROUPS` frame units, include an `ORDER BY` clause.
288
285
289
-
The offset is the difference in row group position relative to the the current row group.
286
+
The offset is the difference in row group position relative to the current row group.
290
287
`GROUPS` supports the following offset types:
291
288
292
289
- Numeric _(non-negative)_
@@ -319,8 +316,7 @@ You can then use group offsets to determine frame boundaries:
319
316
)
320
317
```
321
318
322
-
The window function uses all rows in the two row groups before the current
323
-
row group and the current row group to perform the operation:
319
+
The window function uses all rows in the current row group and the two preceding row groups to perform the operation:
324
320
325
321
{{< sql/window-frame-units "groups with frame" >}}
326
322
@@ -330,45 +326,75 @@ row group and the current row group to perform the operation:
330
326
#### Frame boundaries
331
327
332
328
Frame boundaries (**frame_start** and **frame_end**) define the boundaries of
333
-
each frame the window function operates on. Use the following to define
334
-
frame boundaries:
329
+
each frame that the window function operates on.
335
330
336
-
```sql
337
-
UNBOUNDED PRECEDING
338
-
offset PRECEDING
339
-
CURRENT ROW
340
-
offset FOLLOWING
341
-
UNBOUNDED FOLLOWING
342
-
```
331
+
-[UNBOUNDED PRECEDING](#unbounded-preceding)
332
+
-[offset PRECEDING](#offset-preceding)
333
+
- CURRENT_ROW](#current-row)
334
+
-[offset> FOLLOWING](#offset-following)
335
+
-[UNBOUNDED FOLLOWING](#unbounded-following)
343
336
344
337
##### UNBOUNDED PRECEDING
345
338
346
-
Use the beginning of the partition to the current row as the frame boundary.
339
+
Starts at the first row of the partition and ends at the current row.
340
+
341
+
```sql
342
+
UNBOUNDED PRECEDING
343
+
```
347
344
348
345
##### offset PRECEDING
349
346
350
-
Use a specified offset of [frame units](#frame-units)_before_ the current row
351
-
as a frame boundary.
347
+
Starts at `offset`[frame units](#frame-units) before the current row and ends at the current row.
348
+
For example, `3 PRECEDING` includes 3 rows before the current row.
349
+
350
+
```sql
351
+
<offset> PRECEDING
352
+
```
352
353
353
354
##### CURRENT ROW
354
355
355
-
Use the current row as a frame boundary.
356
+
Both starts and ends at the current row when used as a boundary.
357
+
358
+
```sql
359
+
CURRENT ROW
360
+
```
361
+
362
+
##### offset FOLLOWING
363
+
364
+
Starts at the current row and ends at `offset`[frame units](#frame-units) after the current row.
365
+
For example, `3 FOLLOWING` includes 3 rows after the current row.
366
+
367
+
```sql
368
+
<offset> FOLLOWING
369
+
```
370
+
371
+
##### UNBOUNDED FOLLOWING
356
372
373
+
Starts at the current row and ends at the last row of the partition.
357
374
##### offset FOLLOWING
358
375
359
376
Use a specified offset of [frame units](#frame-units)_after_ the current row
360
377
as a frame boundary.
361
378
379
+
```sql
380
+
offset FOLLOWING
381
+
```
382
+
362
383
##### UNBOUNDED FOLLOWING
363
384
364
385
Use the current row to the end of the current partition the frame boundary.
365
386
387
+
```sql
388
+
UNBOUNDED FOLLOWING
389
+
```
390
+
366
391
### WINDOW clause
367
392
368
-
When a query has multiple window functions that use the same window, rather than
369
-
writing each with a separate `OVER` clause (which is duplicative and error-prone),
370
-
use the `WINDOW` clause to define the window and then reference the window alias
371
-
in each `OVER` clause--for example:
393
+
Use the `WINDOW` clause to define a reusable alias for a window specification.
394
+
This is useful when multiple window functions in your query share the same window definition.
395
+
396
+
Instead of repeating the same OVER clause for each function,
397
+
define the window once and reference it by alias--for example:
372
398
373
399
```sql
374
400
SELECT
@@ -400,16 +426,15 @@ can be used as window functions.
400
426
Returns the cumulative distribution of a value within a group of values.
401
427
The returned value is greater than 0 and less than or equal to 1 and represents
402
428
the relative rank of the value in the set of values.
403
-
The [`ORDER BY` clause](#order-by-clause) in the `OVER` clause determines
404
-
ranking order.
429
+
The [`ORDER BY` clause](#order-by-clause) in the `OVER` clause is used
430
+
to correctly calculate the cumulative distribution of the current row value.
405
431
406
432
```sql
407
433
cume_dist()
408
434
```
409
435
410
436
> [!Important]
411
-
> `cume_dist` needs an [`ORDER BY` clause](#order-by-clause) in the `OVER` clause
412
-
> to correctly calculate the cumulative distribution of the current row value.
437
+
> When using `cume_dist`, include an [`ORDER BY` clause](#order-by-clause) in the `OVER` clause.
413
438
414
439
{{< expand-wrapper >}}
415
440
{{% expand "View `cume_dist` query example" %}}
@@ -451,8 +476,9 @@ WHERE
451
476
452
477
### dense_rank
453
478
454
-
Returns the rank of the current row without gaps. This function ranks rows in a
455
-
dense manner, meaning consecutive ranks are assigned even for identical values.
479
+
Returns a rank for each row without gaps in the numbering.
480
+
Unlike [rank()](#rank), this function assigns consecutive ranks even when values
481
+
are identical.
456
482
The [`ORDER BY` clause](#order-by-clause) in the `OVER` clause determines
457
483
ranking order.
458
484
@@ -500,9 +526,9 @@ WHERE
500
526
501
527
### ntile
502
528
503
-
Distributes the rows in an ordered partition into a specified number of groups.
504
-
Each group is numbered, starting at one. For each row, `ntile` returns the
505
-
group number to which the row belongs.
529
+
Distributes the rows in an ordered partition into the specified number of groups.
530
+
Each group is numbered, starting at one.
531
+
For each row, `ntile` returns the group number to which the row belongs.
506
532
Group numbers range from 1 to the `expression` value, dividing the partition as
507
533
equally as possible.
508
534
The [`ORDER BY` clause](#order-by-clause) in the `OVER` clause determines
@@ -514,8 +540,7 @@ ntile(expression)
514
540
515
541
##### Arguments
516
542
517
-
-**expression**: An integer describing the number groups to split the partition
518
-
into.
543
+
-**expression**: An integer. The number of groups to split the partition into.
519
544
520
545
{{< expand-wrapper >}}
521
546
{{% expand "View `ntile` query example" %}}
@@ -556,10 +581,14 @@ WHERE
556
581
### percent_rank
557
582
558
583
Returns the percentage rank of the current row within its partition.
559
-
The returned value is between `0` and `1` and is computed as
560
-
`(rank - 1) / (total_rows - 1)`.
584
+
The returned value is between `0` and `1`, computed as:
585
+
586
+
```
587
+
(rank - 1) / (total_rows - 1)
588
+
```
589
+
561
590
The [`ORDER BY` clause](#order-by-clause) in the `OVER` clause determines
562
-
ranking order.
591
+
the ranking order.
563
592
564
593
```sql
565
594
percent_rank()
@@ -760,7 +789,7 @@ ORDER BY room, time
760
789
### lag
761
790
762
791
Returns the value from the row that is at the specified offset before the
763
-
current row in the partition. If the offset row is outside of the partition,
792
+
current row in the partition. If the offset row is outside the partition,
764
793
the function returns the specified default.
765
794
766
795
```sql
@@ -876,7 +905,7 @@ ORDER BY room, time
876
905
### lead
877
906
878
907
Returns the value from the row that is at the specified offset after the
879
-
current row in the partition. If the offset row is outside of the partition,
908
+
current row in the partition. If the offset row is outside the partition,
0 commit comments