Skip to content

Commit 8ce5de3

Browse files
docs: Update aggregate and window function documentation with FILTER support
1 parent ff0b1c8 commit 8ce5de3

3 files changed

Lines changed: 60 additions & 0 deletions

File tree

dev/update_function_docs.sh

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,25 @@ dev/update_function_docs.sh file for updating surrounding text.
5959
# Aggregate Functions
6060
6161
Aggregate functions operate on a set of values to compute a single result.
62+
63+
## Filter clause
64+
65+
Aggregate functions support the SQL `FILTER (WHERE ...)` clause to restrict which input rows contribute to the aggregate result.
66+
67+
```sql
68+
function([exprs]) FILTER (WHERE condition)
69+
```
70+
71+
Example:
72+
73+
```sql
74+
SELECT
75+
sum(salary) FILTER (WHERE salary > 0) AS sum_positive_salaries,
76+
count(*) FILTER (WHERE active) AS active_count
77+
FROM employees;
78+
```
79+
80+
Note: When no rows pass the filter, `COUNT` returns `0` while `SUM`/`AVG`/`MIN`/`MAX` return `NULL`.
6281
EOF
6382

6483
echo "Running CLI and inserting aggregate function docs table"
@@ -266,6 +285,17 @@ where **offset** is an non-negative integer.
266285
267286
RANGE and GROUPS modes require an ORDER BY clause (with RANGE the ORDER BY must specify exactly one column).
268287
288+
## Filter clause for aggregate window functions
289+
290+
Aggregate window functions support the SQL `FILTER (WHERE ...)` clause to include only rows that satisfy the predicate from the window frame in the aggregation.
291+
292+
```sql
293+
sum(salary) FILTER (WHERE salary > 0)
294+
OVER (PARTITION BY depname ORDER BY salary ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
295+
```
296+
297+
If no rows in the frame satisfy the filter for a given output row, `COUNT` yields `0` while `SUM`/`AVG`/`MIN`/`MAX` yield `NULL`.
298+
269299
## Aggregate functions
270300
271301
All [aggregate functions](aggregate_functions.md) can be used as window functions.

docs/source/user-guide/sql/aggregate_functions.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,25 @@ dev/update_function_docs.sh file for updating surrounding text.
2929

3030
Aggregate functions operate on a set of values to compute a single result.
3131

32+
## Filter clause
33+
34+
Aggregate functions support the SQL `FILTER (WHERE ...)` clause to restrict which input rows contribute to the aggregate result.
35+
36+
```sql
37+
function([exprs]) FILTER (WHERE condition)
38+
```
39+
40+
Example:
41+
42+
```sql
43+
SELECT
44+
sum(salary) FILTER (WHERE salary > 0) AS sum_positive_salaries,
45+
count(*) FILTER (WHERE active) AS active_count
46+
FROM employees;
47+
```
48+
49+
Note: When no rows pass the filter, `COUNT` returns `0` while `SUM`/`AVG`/`MIN`/`MAX` return `NULL`.
50+
3251
## General Functions
3352

3453
- [array_agg](#array_agg)

docs/source/user-guide/sql/window_functions.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,17 @@ where **offset** is an non-negative integer.
145145

146146
RANGE and GROUPS modes require an ORDER BY clause (with RANGE the ORDER BY must specify exactly one column).
147147

148+
## Filter clause for aggregate window functions
149+
150+
Aggregate window functions support the SQL `FILTER (WHERE ...)` clause to include only rows that satisfy the predicate from the window frame in the aggregation.
151+
152+
```sql
153+
sum(salary) FILTER (WHERE salary > 0)
154+
OVER (PARTITION BY depname ORDER BY salary ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
155+
```
156+
157+
If no rows in the frame satisfy the filter for a given output row, `COUNT` yields `0` while `SUM`/`AVG`/`MIN`/`MAX` yield `NULL`.
158+
148159
## Aggregate functions
149160

150161
All [aggregate functions](aggregate_functions.md) can be used as window functions.

0 commit comments

Comments
 (0)