Skip to content

Commit 994edb9

Browse files
authored
Merge pull request #211 from stackql/feature/web-content-updates
updated window diagrams
2 parents 848d2b7 + 4b55d71 commit 994edb9

24 files changed

+1022
-27
lines changed

docs/language-spec/functions/aggregate/avg.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,25 @@ AND zone = 'australia-southeast1-a'
6363
GROUP BY json_extract(tags, '$.instanceType');
6464
```
6565

66+
### Use `AVG` as a window function to calculate moving averages
67+
68+
```sql
69+
-- Calculate moving average of commit activity
70+
WITH weekly_totals AS (
71+
SELECT
72+
week,
73+
SUM(json_each.value) as commits_this_week
74+
FROM github.repos.stats_commit_activity, JSON_EACH(days)
75+
WHERE owner = 'stackql'
76+
AND repo = 'stackql'
77+
GROUP BY week
78+
)
79+
SELECT
80+
week,
81+
commits_this_week,
82+
ROUND(AVG(commits_this_week) OVER (ORDER BY week ROWS BETWEEN 3 PRECEDING AND CURRENT ROW), 1) as four_week_moving_avg
83+
FROM weekly_totals
84+
ORDER BY week;
85+
```
86+
6687
For more information, see [https://www.sqlite.org/lang_aggfunc.html#avg](https://www.sqlite.org/lang_aggfunc.html#avg).

docs/language-spec/functions/aggregate/count.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,21 @@ AND zone = 'australia-southeast1-a'
6161
GROUP BY json_extract(tags, '$.instanceType');
6262
```
6363

64+
### Use `COUNT` as a window function to calculate cumulative counts
65+
66+
```sql
67+
-- Track cumulative issue counts over time
68+
SELECT
69+
number,
70+
title,
71+
state,
72+
created_at,
73+
COUNT(*) OVER (ORDER BY created_at) as cumulative_issues,
74+
COUNT(*) OVER (PARTITION BY state ORDER BY created_at) as cumulative_by_state
75+
FROM github.issues.issues
76+
WHERE owner = 'stackql'
77+
AND repo = 'stackql'
78+
ORDER BY created_at;
79+
```
80+
6481
For more information, see [https://www.sqlite.org/lang_aggfunc.html#count](https://www.sqlite.org/lang_aggfunc.html#count).

docs/language-spec/functions/aggregate/max.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,20 @@ WHERE project = 'stackql-demo'
9191
AND zone = 'australia-southeast1-a';
9292
```
9393

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+
FROM github.issues.issues
105+
WHERE owner = 'stackql'
106+
AND repo = 'stackql'
107+
ORDER BY state, created_at DESC;
108+
```
109+
94110
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)

docs/language-spec/functions/aggregate/min.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,20 @@ WHERE project = 'stackql-demo'
9191
AND zone = 'australia-southeast1-a';
9292
```
9393

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+
FROM github.issues.issues
105+
WHERE owner = 'stackql'
106+
AND repo = 'stackql'
107+
ORDER BY state, created_at;
108+
```
109+
94110
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)

docs/language-spec/functions/aggregate/sum.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,19 @@ AND zone = 'australia-southeast1-a'
6969
GROUP BY json_extract(tags, '$.instanceType');
7070
```
7171

72+
### Use `SUM` as a window function to calculate running totals
73+
74+
```sql
75+
-- Calculate running total and percentage of contributions
76+
SELECT
77+
login,
78+
contributions,
79+
SUM(contributions) OVER (ORDER BY contributions DESC) as running_total,
80+
SUM(contributions) OVER () as total_contributions,
81+
ROUND(100.0 * contributions / SUM(contributions) OVER (), 2) as pct_of_total
82+
FROM github.repos.contributors
83+
WHERE owner = 'stackql'
84+
AND repo = 'stackql';
85+
```
86+
7287
For more information, see [https://www.sqlite.org/lang_aggfunc.html#sumunc](https://www.sqlite.org/lang_aggfunc.html#sumunc).

docs/language-spec/select.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,12 @@ type="select"
3232

3333
```sql
3434
[ WITH [ RECURSIVE ] <cteName> [ ( <columnList> ) ] AS ( <selectStatement> ) [, ...] ]
35-
SELECT { * | <fieldList> }
35+
SELECT { * | <fieldList> | <windowFunctionCall> [ AS <alias> ] }
3636
FROM { <multipartIdentifier> | <joinStatement(s)> }
3737
[ WHERE <expression> ]
3838
[ GROUP BY <fieldList> ]
3939
[ HAVING <expression> ]
40+
[ WINDOW <windowName> AS ( <windowSpec> ) [, ...] ]
4041
[ ORDER BY <fieldList> [ ASC | DESC ] ]
4142
[ LIMIT <integer> ]
4243
[ UNION <selectStatement> ];

docs/language-spec/windowing_functions.md

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ keywords:
1010
description: Query and Deploy Cloud Infrastructure and Resources using SQL
1111
image: "/img/stackql-featured-image.png"
1212
---
13+
import RailroadDiagram from '/js/RailroadDiagram/RailroadDiagram.js';
1314

1415
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.
1516

@@ -26,21 +27,59 @@ Window functions use the `OVER` clause to define the window specification:
2627
SELECT <windowFunction> OVER ( <windowSpec> ) FROM <multipartIdentifier>;
2728
```
2829

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

3140
```sql
32-
[ PARTITION BY <fieldList> ] [ ORDER BY <fieldList> [ ASC | DESC ] ]
33-
[ ROWS BETWEEN <frameStart> AND <frameEnd> ]
41+
{ <aggregateFunction> | ROW_NUMBER() | RANK() | DENSE_RANK() | PERCENT_RANK()
42+
| CUME_DIST() | NTILE(<int>) | LAG(<expr> [,<offset> [,<default>]])
43+
| LEAD(<expr> [,<offset> [,<default>]]) | FIRST_VALUE(<expr>)
44+
| LAST_VALUE(<expr>) | NTH_VALUE(<expr>, <int>) }
45+
```
46+
47+
*windowFunction::=*
48+
49+
<RailroadDiagram
50+
type="windowFunction"
51+
/>
52+
53+
### Window Specification
54+
55+
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 } ]
3467
```
3568

69+
*windowSpec::=*
70+
71+
<RailroadDiagram
72+
type="windowSpec"
73+
/>
74+
3675
## Available Window Functions
3776

3877
| Category | Functions |
3978
|----------|-----------|
40-
| **Ranking** | `ROW_NUMBER()`, `RANK()`, `DENSE_RANK()`, `NTILE()` |
41-
| **Offset** | `LAG()`, `LEAD()`, `FIRST_VALUE()`, `LAST_VALUE()`, `NTH_VALUE()` |
42-
| **Distribution** | `PERCENT_RANK()`, `CUME_DIST()` |
43-
| **Aggregate** | `SUM()`, `COUNT()`, `AVG()`, `MIN()`, `MAX()` with `OVER` clause |
79+
| **Ranking** | [__`ROW_NUMBER()`__](/docs/language-spec/functions/window/row_number), [__`RANK()`__](/docs/language-spec/functions/window/rank), [__`DENSE_RANK()`__](/docs/language-spec/functions/window/dense_rank), [__`NTILE()`__](/docs/language-spec/functions/window/ntile) |
80+
| **Offset** | [__`LAG()`__](/docs/language-spec/functions/window/lag), [__`LEAD()`__](/docs/language-spec/functions/window/lead), [__`FIRST_VALUE()`__](/docs/language-spec/functions/window/first_value), [__`LAST_VALUE()`__](/docs/language-spec/functions/window/last_value), [__`NTH_VALUE()`__](/docs/language-spec/functions/window/nth_value) |
81+
| **Distribution** | [__`PERCENT_RANK()`__](/docs/language-spec/functions/window/percent_rank), [__`CUME_DIST()`__](/docs/language-spec/functions/window/cume_dist) |
82+
| **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 |
4483

4584
* * *
4685

docs/language-spec/with.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,13 @@ See also:
2424
*withClause::=*
2525

2626
<RailroadDiagram
27-
type="with"
27+
type="withClause"
28+
/>
29+
30+
*cteDefinition::=*
31+
32+
<RailroadDiagram
33+
type="cteDefinition"
2834
/>
2935

3036
&nbsp;
Lines changed: 6 additions & 0 deletions
Loading

ref/antlr/rendered/columnList.svg

Lines changed: 6 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)