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

后端 未结 7 835
长发绾君心
长发绾君心 2021-01-17 07:36

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:



        
7条回答
  •  攒了一身酷
    2021-01-17 08:27

    I don't know if you can do use generate_series like that in PostgreSQL, but i would try a cross join:

    SELECT x FROM
      (SELECT generate_series(1, 10) AS x) t1, 
      (SELECT generate_series(1, 3) as y) t2
    

    Edit:

    As generate_series already returns a table there's no need for SELECT in a Derived Table:

    SELECT x FROM
      generate_series(1, 10) AS x, 
      generate_series(1, 3) as y
    

提交回复
热议问题