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.
Generate a Series in Postgres. generate_series([start], [stop], [{optional}step/interval]); Generate a series of numbers in postgres by using the generate_series function.
Enter the simple but handy set returning function of Postgres: generate_series . generate_series as the name implies allows you to generate a set of data starting at some point, ending at another point, and optionally set the incrementing value. generate_series works on two datatypes: integers. timestamps.
In PostgreSQL, the make_interval() function creates an interval from years, months, weeks, days, hours, minutes and seconds fields. You provide the years, months, weeks, days, hours, minutes and/or seconds fields, and it will return an interval in the interval data type.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With