How can I generate a series of repeating numbers in PostgreSQL?

oshongo picture oshongo · Apr 29, 2014 · Viewed 27.7k times · Source

In PostgreSQL, is it possible to generate a series of repeating numbers? For example, I want to generate the numbers 1 to 10, with each number repeated 3 times:

1
1
1
2
2
2
3
3
3
.. and so on.

Answer

Bohemian picture Bohemian · Apr 29, 2014

You could cross join it to a series of 3:

SELECT a.n
from generate_series(1, 100) as a(n), generate_series(1, 3)