Applies to: ✅ Data 360 SQL ✅ Tableau Hyper API
Creates an array of sequential values within a range.
Syntax
array_generate_series(<start>, <stop>[, <step>])
Arguments
Required
<start>: The starting value of the sequence.<stop>: The ending value of the sequence (inclusive).
Optional
<step>: The increment between values. The default is 1.
Returns
Returns an array that contains sequential values from start to stop, incrementing by step.
Considerations
- If you use a negative step, make sure
<start>is greater than<stop>. If<start>is less than or equal to<stop>, the function returns an empty array. - A negative step generates a descending sequence. A positive step generates an ascending sequence.
- This function is equivalent to the generate_series (Numerical) function but returns an array instead of a set of rows.
- Both the
startandstopvalues are included in the generated series. - The step size must be non-zero.
- The step size can also be negative. For negative step sizes,
startmust be greater thanstop. - For positive step sizes, if
startis greater thanstop, the function returns no rows. - For negative step sizes, if
startis less thanstop, the function returns no rows. - If any input is
null, the function returns no rows.
Examples
Basic Usage
Create an array of integers from 1 to 5.
SELECT array_generate_series(1, 5) AS result;
Returns [1, 2, 3, 4, 5].
Generate Sequence with Step
Create an array of integers from 1 to 5 with step 2.
SELECT array_generate_series(1, 5, 2) AS result;
Returns [1, 3, 5].
Reverse Sequence with Negative Step
Create an array counting down from 5 to 1 in increments of 2.
SELECT array_generate_series(5, 1, -2) AS result;
Returns [5, 3, 1].
Decimal Reverse Sequence
Create a descending array with decimal values.
SELECT array_generate_series(3.7, 3.0, -0.3) AS result;
Returns [3.7, 3.4, 3.1].
Negative Step with Increasing Range (Empty Result)
When the step direction doesn't match the range direction, an empty array is returned.
SELECT array_generate_series(1, 5, -1) AS result;
Returns [].
Additional Examples
The examples in generate_series examples apply to array_generate_series.
Related Documentation
- Array Data Type
ARRAY_GENERATE_SERIES(Time-based) - the time-based version of this functiongenerate_series(Numerical) - the set-returning version of this function