Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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. 
like image 268
oshongo Avatar asked Apr 29 '14 07:04

oshongo


People also ask

How do you create a series of numbers and insert it into a table in PostgreSQL?

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.

What is generate series in PostgreSQL?

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.

How do I create an interval in PostgreSQL?

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.


Video Answer


1 Answers

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) 
like image 51
Bohemian Avatar answered Sep 21 '22 03:09

Bohemian