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
Copy file name to clipboardExpand all lines: docs/language-spec/functions/aggregate/max.md
+16Lines changed: 16 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -91,4 +91,20 @@ WHERE project = 'stackql-demo'
91
91
AND zone ='australia-southeast1-a';
92
92
```
93
93
94
+
### Use `MAX` as a window function to find maximum values within partitions
95
+
96
+
```sql
97
+
-- Find the most recent issue in each state
98
+
SELECT
99
+
number,
100
+
title,
101
+
state,
102
+
created_at,
103
+
MAX(created_at) OVER (PARTITION BY state) as latest_in_state
104
+
FROMgithub.issues.issues
105
+
WHERE owner ='stackql'
106
+
AND repo ='stackql'
107
+
ORDER BY state, created_at DESC;
108
+
```
109
+
94
110
For more information, see [https://www.sqlite.org/lang_aggfunc.html#max_agg](https://www.sqlite.org/lang_aggfunc.html#max_agg) or [https://www.sqlite.org/lang_corefunc.html#max_scalar](https://www.sqlite.org/lang_corefunc.html#max_scalar)
Copy file name to clipboardExpand all lines: docs/language-spec/functions/aggregate/min.md
+16Lines changed: 16 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -91,4 +91,20 @@ WHERE project = 'stackql-demo'
91
91
AND zone ='australia-southeast1-a';
92
92
```
93
93
94
+
### Use `MIN` as a window function to find minimum values within partitions
95
+
96
+
```sql
97
+
-- Find the earliest issue in each state
98
+
SELECT
99
+
number,
100
+
title,
101
+
state,
102
+
created_at,
103
+
MIN(created_at) OVER (PARTITION BY state) as earliest_in_state
104
+
FROMgithub.issues.issues
105
+
WHERE owner ='stackql'
106
+
AND repo ='stackql'
107
+
ORDER BY state, created_at;
108
+
```
109
+
94
110
For more information, see [https://www.sqlite.org/lang_aggfunc.html#min_agg](https://www.sqlite.org/lang_aggfunc.html#min_agg) or [https://www.sqlite.org/lang_corefunc.html#min_scalar](https://www.sqlite.org/lang_corefunc.html#min_scalar)
Copy file name to clipboardExpand all lines: docs/language-spec/windowing_functions.md
+46-7Lines changed: 46 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,6 +10,7 @@ keywords:
10
10
description: Query and Deploy Cloud Infrastructure and Resources using SQL
11
11
image: "/img/stackql-featured-image.png"
12
12
---
13
+
import RailroadDiagram from '/js/RailroadDiagram/RailroadDiagram.js';
13
14
14
15
Window functions perform calculations across a set of rows that are related to the current row. Unlike aggregate functions that return a single result for a group of rows, window functions return a value for each row while considering a "window" of related rows.
15
16
@@ -26,21 +27,59 @@ Window functions use the `OVER` clause to define the window specification:
26
27
SELECT<windowFunction> OVER ( <windowSpec> ) FROM<multipartIdentifier>;
27
28
```
28
29
29
-
*windowSpec::=*
30
+
*windowFunctionCall::=*
31
+
32
+
<RailroadDiagram
33
+
type="windowFunctionCall"
34
+
/>
35
+
36
+
### Window Function
37
+
38
+
Window functions are specialized functions that perform calculations across a set of rows defined by the window specification. They can be either dedicated window functions (like `ROW_NUMBER` or `LAG`) or aggregate functions (like `SUM` or `COUNT`) used with an `OVER` clause.
30
39
31
40
```sql
32
-
[ PARTITION BY <fieldList> ] [ ORDER BY<fieldList> [ ASC | DESC ] ]
The window specification defines how rows are partitioned, ordered, and framed for the window function calculation. `PARTITION BY` divides rows into groups, `ORDER BY` determines the sequence within each partition, and the optional frame clause specifies which rows relative to the current row are included in the calculation.
56
+
57
+
```sql
58
+
[ <windowName> ]
59
+
[ PARTITION BY <fieldList> ]
60
+
[ ORDER BY<fieldList> [ ASC | DESC ] ]
61
+
[ { ROWS | RANGE | GROUPS }
62
+
{ UNBOUNDED PRECEDING | <int> PRECEDING | CURRENT ROW }
63
+
| { ROWS | RANGE | GROUPS } BETWEEN
64
+
{ UNBOUNDED PRECEDING | <int> PRECEDING | CURRENT ROW | <int> FOLLOWING }
65
+
AND
66
+
{ <int> PRECEDING | CURRENT ROW | <int> FOLLOWING | UNBOUNDED FOLLOWING } ]
|**Aggregate**|[__`SUM()`__](/docs/language-spec/functions/aggregate/sum), [__`COUNT()`__](/docs/language-spec/functions/aggregate/count), [__`AVG()`__](/docs/language-spec/functions/aggregate/avg), [__`MIN()`__](/docs/language-spec/functions/aggregate/min), [__`MAX()`__](/docs/language-spec/functions/aggregate/max) with the __`OVER`__ clause |
0 commit comments