The GENERATE_SERIES function creates a series of numbers within a specified interval.
The interval and the step between series values are defined by the user.
<generate_series_function> ::=
GENERATE_SERIES(<start>, <finish> [, <step>]) [AS] <correlation name> [ ( <derived column name> ) ]
-
start- The first value in the interval.startis specified as a variable, a literal, or a scalar expression of typesmallint,integer,bigintornumeric(18, x). -
finish- The last value in the interval.finishis specified as a variable, a literal, or a scalar expression of typesmallint,integer,bigintornumeric(18, x). The series stops once the last generated step value exceeds thefinishvalue. -
step- Indicates the number of values to increment or decrement between steps in the series.stepis an expression of typesmallint,integer,bigintornumeric(18, x).stepcan be either negative or positive, but can't be zero (0). This argument is optional. The default value forstepis 1.
The function GENERATE_SERIES returns a set with BIGINT or NUMERIC(18, x) column, where the scale is
determined by the maximum of the scales of the function arguments.
-
If
start > finishand a negativestepvalue is specified, an empty set is returned. -
If
start < finishand a positivestepvalue is specified, an empty set is returned. -
If the
stepargument is zero, an error is thrown.
SELECT n
FROM GENERATE_SERIES(1, 3) AS S(n);
SELECT n
FROM GENERATE_SERIES(3, 1, -1) AS S(n);
SELECT n
FROM GENERATE_SERIES(0, 9.9, 0.1) AS S(n);
SELECT
DATEADD(n MINUTE TO timestamp '2025-01-01 12:00') AS START_TIME,
DATEADD(n MINUTE TO timestamp '2025-01-01 12:00:59.9999') AS FINISH_TIME
FROM GENERATE_SERIES(0, 59) AS S(n);