SEQUENCE()

All functions > ARRAY > SEQUENCE()

Generates an inclusive array from start to stop. Accepts integers with an optional step (default 1), dates with an optional INTERVAL step (default one day), or timestamps with a required INTERVAL step.

Signatures

Integer sequence

Returns: Array of integers in the sequence

SEQUENCE(start: BIGINT, stop: BIGINT, [step: BIGINT]) → ARRAY<BIGINT>
sql
ParameterTypeRequiredDescription
startBIGINTYesStarting value (inclusive)
stopBIGINTYesEnding value (inclusive)
stepBIGINTNoStep between elements (default: 1)

Signature notes:

  • Step defaults to 1; use a negative step for descending sequences
  • Both start and stop are included in the result

Date sequence

Returns: Array of dates or timestamps in the sequence

SEQUENCE(start: DATE | TIMESTAMP, stop: DATE | TIMESTAMP, step: INTERVAL) → ARRAY<DATE> | ARRAY<TIMESTAMP>
sql
ParameterTypeRequiredDescription
start`DATETIMESTAMP`Yes
stop`DATETIMESTAMP`Yes
stepINTERVALYesInterval between elements (default for dates: one day; required for timestamps)

Signature notes:

  • The INTERVAL step defaults to one day for dates; it is required for timestamps
  • Month-based intervals respect calendar month boundaries
  • Both start and stop are included when reachable by the given step

Notes

  • Generates sequence from start to stop inclusive
  • For integers: step defaults to 1; use a negative step for descending sequences
  • For dates the INTERVAL step defaults to one day; for timestamps it is required
  • Month-based intervals (e.g. 1 month) respect calendar month boundaries
  • If start equals stop the result contains exactly one element
  • Bind a date SEQUENCE in FOR CROSS against a fact INPUT to reconstruct a value on every calendar day; see help('date_spine')

Aliases

  • RANGE

  • GENERATE_SERIES

See also

Examples

Integer sequences

FeatureQL
RETURN
    -- Default step 1
    f1 := SEQUENCE(1, 5),
    -- Descending with negative step
    f2 := SEQUENCE(5, 1, -1),
    -- Single-point range
    f3 := SEQUENCE(3, 3)
;
Result
f1 ARRAYf2 ARRAYf3 ARRAY
[1, 2, 3, 4, 5][5, 4, 3, 2, 1][3]

Date sequences

FeatureQL
RETURN
    -- Day-by-day sequence
    f1 := SEQUENCE(
        DATE '2024-01-01',
        DATE '2024-01-05',
        INTERVAL_PARSE('1 day')
    )
;
Result
f1 ARRAY
[2024-01-01, 2024-01-02, 2024-01-03, 2024-01-04, 2024-01-05]

Timestamp sequences

FeatureQL
RETURN
    -- Monthly steps respect calendar boundaries (leap year Feb)
    f1 := SEQUENCE(
        DATE '2024-01-31',
        DATE '2024-04-30',
        INTERVAL_PARSE('1 month')
    )
;
Result
f1 ARRAY
[2024-01-31, 2024-02-29, 2024-03-29, 2024-04-29]