Skip to content

Commit 4b55d71

Browse files
committed
updated window examples
1 parent 264c45e commit 4b55d71

File tree

7 files changed

+89
-28
lines changed

7 files changed

+89
-28
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/windowing_functions.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,10 @@ type="windowSpec"
7676

7777
| Category | Functions |
7878
|----------|-----------|
79-
| **Ranking** | `ROW_NUMBER()`, `RANK()`, `DENSE_RANK()`, `NTILE()` |
80-
| **Offset** | `LAG()`, `LEAD()`, `FIRST_VALUE()`, `LAST_VALUE()`, `NTH_VALUE()` |
81-
| **Distribution** | `PERCENT_RANK()`, `CUME_DIST()` |
82-
| **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 |
8383

8484
* * *
8585

static/js/RailroadDiagram/RailroadDiagram.js

Lines changed: 0 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)