Skip to content

Commit 01236c6

Browse files
committed
fix(sql): Apply suggestions from code review.
1 parent 6b0265b commit 01236c6

File tree

1 file changed

+90
-61
lines changed
  • content/shared/sql-reference/functions

1 file changed

+90
-61
lines changed

content/shared/sql-reference/functions/window.md

Lines changed: 90 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11

2-
A _window function_ performs an operation across a set of rows related to the
3-
current row. This is similar to the type of operations
4-
[aggregate functions](/influxdb3/cloud-dedicated/reference/sql/functions/aggregate/)
5-
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.
86

97
For example, the following query uses the {{< influxdb3/home-sample-link >}}
108
and returns each temperature reading with the average temperature per room over
@@ -65,9 +63,10 @@ ORDER BY
6563

6664
As window functions operate on a row, there is a set of rows in the row's
6765
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
6968
`RANGE`, `ROW`, or `GROUPS` frame units, each relative to the current row--for
70-
exmaple:
69+
example:
7170

7271
{{< code-tabs-wrapper >}}
7372
{{% code-tabs %}}
@@ -117,11 +116,9 @@ FROM home
117116

118117
_For more information about how window frames work, see the [frame clause](#frame-clause)._
119118

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
121120
partition to perform their operation.
122121

123-
## Window function syntax
124-
125122
```sql
126123
function([expr])
127124
OVER(
@@ -133,10 +130,10 @@ function([expr])
133130

134131
### OVER clause
135132

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.
140137

141138
### PARTITION BY clause
142139

@@ -154,13 +151,13 @@ may be explicit or implicit, limiting a window frame size in both directions
154151
relative to the current row.
155152

156153
> [!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.
160157
161158
### Frame clause
162159

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:
164161

165162
```sql
166163
{ RANGE | ROWS | GROUPS } frame_start
@@ -196,7 +193,7 @@ the current row value.
196193
> When using `RANGE` frame units, you must include an `ORDER BY` clause with
197194
> _exactly one column_.
198195
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
200197
row values. `RANGE` supports the following offset types:
201198

202199
- Numeric _(non-negative)_
@@ -207,7 +204,7 @@ row values. `RANGE` supports the following offset types:
207204
{{% expand "See how `RANGE` frame units work with numeric offsets" %}}
208205

209206
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.
211208

212209
```sql
213210
... OVER (
@@ -226,7 +223,7 @@ The window frame includes rows with sort column values between 45 below and
226223
{{% expand "See how `RANGE` frame units work with interval offsets" %}}
227224

228225
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.
230227

231228
```sql
232229
... OVER (
@@ -251,7 +248,7 @@ one hour after the current row's timestamp:
251248

252249
##### ROWS
253250

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.
255252
The offset is the difference in row position from the current row.
256253
`ROWS` supports the following offset types:
257254

@@ -279,14 +276,14 @@ The window frame includes the two rows before and the one row after the current
279276

280277
##### GROUPS
281278

282-
Defines frame boundaries using row groups.
279+
Defines window frame boundaries using row groups.
283280
Rows with the same values for the columns in the [`ORDER BY` clause](#order-by-clause)
284281
comprise a row group.
285282

286283
> [!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.
288285
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.
290287
`GROUPS` supports the following offset types:
291288

292289
- Numeric _(non-negative)_
@@ -319,8 +316,7 @@ You can then use group offsets to determine frame boundaries:
319316
)
320317
```
321318

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:
324320

325321
{{< sql/window-frame-units "groups with frame" >}}
326322

@@ -330,45 +326,75 @@ row group and the current row group to perform the operation:
330326
#### Frame boundaries
331327

332328
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.
335330

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)
343336

344337
##### UNBOUNDED PRECEDING
345338

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+
```
347344

348345
##### offset PRECEDING
349346

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+
```
352353

353354
##### CURRENT ROW
354355

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
356372

373+
Starts at the current row and ends at the last row of the partition.
357374
##### offset FOLLOWING
358375

359376
Use a specified offset of [frame units](#frame-units) _after_ the current row
360377
as a frame boundary.
361378

379+
```sql
380+
offset FOLLOWING
381+
```
382+
362383
##### UNBOUNDED FOLLOWING
363384

364385
Use the current row to the end of the current partition the frame boundary.
365386

387+
```sql
388+
UNBOUNDED FOLLOWING
389+
```
390+
366391
### WINDOW clause
367392

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:
372398

373399
```sql
374400
SELECT
@@ -400,16 +426,15 @@ can be used as window functions.
400426
Returns the cumulative distribution of a value within a group of values.
401427
The returned value is greater than 0 and less than or equal to 1 and represents
402428
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.
405431

406432
```sql
407433
cume_dist()
408434
```
409435

410436
> [!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.
413438
414439
{{< expand-wrapper >}}
415440
{{% expand "View `cume_dist` query example" %}}
@@ -451,8 +476,9 @@ WHERE
451476

452477
### dense_rank
453478

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.
456482
The [`ORDER BY` clause](#order-by-clause) in the `OVER` clause determines
457483
ranking order.
458484

@@ -500,9 +526,9 @@ WHERE
500526

501527
### ntile
502528

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.
506532
Group numbers range from 1 to the `expression` value, dividing the partition as
507533
equally as possible.
508534
The [`ORDER BY` clause](#order-by-clause) in the `OVER` clause determines
@@ -514,8 +540,7 @@ ntile(expression)
514540

515541
##### Arguments
516542

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.
519544

520545
{{< expand-wrapper >}}
521546
{{% expand "View `ntile` query example" %}}
@@ -556,10 +581,14 @@ WHERE
556581
### percent_rank
557582

558583
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+
561590
The [`ORDER BY` clause](#order-by-clause) in the `OVER` clause determines
562-
ranking order.
591+
the ranking order.
563592

564593
```sql
565594
percent_rank()
@@ -760,7 +789,7 @@ ORDER BY room, time
760789
### lag
761790

762791
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,
764793
the function returns the specified default.
765794

766795
```sql
@@ -876,7 +905,7 @@ ORDER BY room, time
876905
### lead
877906

878907
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,
880909
the function returns the specified default.
881910

882911
```sql

0 commit comments

Comments
 (0)