From c94df95d40c0934e8034a710678694b0e103e61d Mon Sep 17 00:00:00 2001 From: dahn Date: Thu, 7 Dec 2023 13:38:56 -0500 Subject: [PATCH] Add example for listagg with filter --- docs/src/main/sphinx/functions/aggregate.md | 30 ++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/docs/src/main/sphinx/functions/aggregate.md b/docs/src/main/sphinx/functions/aggregate.md index fc59ce710c20..20af7dd7462c 100644 --- a/docs/src/main/sphinx/functions/aggregate.md +++ b/docs/src/main/sphinx/functions/aggregate.md @@ -213,7 +213,7 @@ of omitted non-null values in case that the length of the output of the function exceeds `1048576` bytes: ``` -SELECT LISTAGG(value, ',' ON OVERFLOW TRUNCATE '.....' WITH COUNT) WITHIN GROUP (ORDER BY value) +SELECT listagg(value, ',' ON OVERFLOW TRUNCATE '.....' WITH COUNT) WITHIN GROUP (ORDER BY value) FROM (VALUES 'a', 'b', 'c') t(value); ``` @@ -222,7 +222,7 @@ If not specified, the truncation filler string is by default `'...'`. This aggregation function can be also used in a scenario involving grouping: ``` -SELECT id, LISTAGG(value, ',') WITHIN GROUP (ORDER BY o) csv_value +SELECT id, listagg(value, ',') WITHIN GROUP (ORDER BY o) csv_value FROM (VALUES (100, 1, 'a'), (200, 3, 'c'), @@ -241,7 +241,31 @@ results in: 200 | b,c ``` -The current implementation of `LISTAGG` function does not support window frames. +This aggregation function can be also used with the `FILTER` keyword to specify +which rows are processed during the `listagg` aggregation: + +```sql +SELECT listagg(value, ',') + WITHIN GROUP (ORDER BY id) + FILTER (WHERE id % 2 = 0) csv_value +FROM (VALUES + (1, 'a'), + (2, 'b'), + (3, 'c'), + (4, 'd') +) t(id, value) +``` + +The example aggregates rows that have even-numbered `id`, and concatenates +`value` to a comma-separated string: + +``` + csv_value +----------- + b,d +``` + +The current implementation of `listagg` function does not support window frames. ::: :::{function} max(x) -> [same as input]