how to generate sequence of values in SQLite when lower and upper bounds are taken from another table?

68 views Asked by At

I can use SQLite's built-in generate_sequence function to create a series of values with a known (constant) lower and upper bound:

sqlite> select value from generate_series(1, 5);
1
2
3
4
5

How do I generate such a sequence if the lower and upper bounds must be based on values from some table? For example, suppose I have:

sqlite> create table temp(
......>     num integer not null
......> );
sqlite> insert into temp values (1), (5);
sqlite> select * from temp;
1
5

The following (quite reasonably) gives a parse error:

sqlite> select value from generate_series(
......>     select min(num) from temp,
......>     select max(num) from temp
......> );

Is it possible to do what I want without using a recursive CTE? (My actual use case requires a series of dates based on the minimum and maximum dates in a dataset, but if I can get a series of integers, generating dates will be straightforward.)

1

There are 1 answers

2
scruss On
select value from generate_series(
    (select min(num) from temp), 
    (select max(num) from temp)
);
1
2
3
4
5