generate_series()
Creates a list of values between start and stop - the stop parameter is inclusive.
Reference: Haybarn WASM 2.0.0-rc12
Arguments & returns
10 overloadsgenerate_series(stop)
Scalar
| Overload link | stop |
Returns |
|---|---|---|
| #01 | BIGINT |
BIGINT[] |
generate_series(start, stop)
Scalar
| Overload link | start | stop |
Returns |
|---|---|---|---|
| #02 | BIGINT | BIGINT |
BIGINT[] |
generate_series(start, stop, step)
Scalar
generate_series(stop)
Table
| Overload link | stop |
Returns |
|---|---|---|
| #06 | BIGINT |
TABLE |
generate_series(start, stop)
Table
| Overload link | start | stop |
Returns |
|---|---|---|---|
| #07 | BIGINT | BIGINT |
TABLE |
generate_series(start, stop, step)
Table
Generate a sequence with an inclusive stop
Generate a sequence that includes its stop value when a step lands on it. Use the table form to produce one row per value.
A positive step of two from 1 to 5 produces 1, 3, and 5. The boundary is not added if the step never reaches it.
Haybarn WASM 2.0.0-rc12 ยท In your browser
Guide example ยท Run to view results.
| value |
|---|
1 |
3 |
5 |
Watch for: Unlike range(), generate_series() includes the stop. Check this difference when building date spines or counting intervals.
Example
Haybarn WASM 2.0.0-rc12 ยท In your browser
Engine example ยท Run to view results.
In other engines
Dremioarray_generate_range()
ARRAY_GENERATE_RANGE(1, 4)
GENERATE_SERIES(1, 4)
Used in larger rewrites 2
These examples use generate_series as one part of a larger SQL translation.
PostgreSQL
WITH dates AS (SELECT GENERATE_SERIES('2020-01-01'::DATE, '2024-01-01'::DATE, '1 day'::INTERVAL) AS date), date_table AS (SELECT DISTINCT DATE_TRUNC('MONTH', date) AS date FROM dates) SELECT * FROM date_table
WITH dates AS (SELECT UNNEST(GENERATE_SERIES(CAST('2020-01-01' AS DATE), CAST('2024-01-01' AS DATE), CAST('1 day' AS INTERVAL))) AS date), date_table AS (SELECT DISTINCT DATE_TRUNC('MONTH', date) AS date FROM dates) SELECT * FROM date_table
Snowflakearray_except()
SELECT ARRAY_EXCEPT([1, 2, 3], [2])
SELECT CASE WHEN [1, 2, 3] IS NULL OR [2] IS NULL THEN NULL ELSE LIST_TRANSFORM(LIST_FILTER(LIST_ZIP([1, 2, 3], GENERATE_SERIES(1, LENGTH([1, 2, 3]))), pair -> (LENGTH(LIST_FILTER([1, 2, 3][1:pair[2]], e -> e IS NOT DISTINCT FROM pair[1])) > LENGTH(LIST_FILTER([2], e -> e IS NOT DISTINCT FROM pair[1])))), pair -> pair[1]) END