1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-02 22:58:01 +00:00
Files
til/postgres/generate-series-of-numbers.md
2015-06-19 08:26:41 -05:00

944 B

Generate Series Of Numbers

Postgres has a generate_series function that can be used to, well, generate a series of something. The simplest way to use it is by giving it start and stop arguments

> select generate_series(1,5);
 generate_series 
-----------------
               1
               2
               3
               4
               5

The default step is 1, so if you want to count backwards, you need to specify a negative step

> select generate_series(5,1,-1);
 generate_series 
-----------------
               5
               4
               3
               2
               1

You can use a larger step value to, for instance, get only multiples of 3

> select generate_series(3,17,3);
 generate_series 
-----------------
               3
               6
               9
              12
              15

Trying this out with timestamps is left as an exercise for the reader.