2

I'm hoping there exists something like this:

> SELECT nrange(10)
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

Similar to the range function in languages like Python and Racket. Of course, if no such function exists in PostgreSQL I'd be more than satisfied to know the idiomatic way to approach this. Thanks!

This is a bit different from the question How can I generate a series of repeating numbers in PostgreSQL? in that I am not trying to generate a series of repeating numbers, but rather an array of sequential numbers.

Alex V
  • 3,416
  • 2
  • 33
  • 52
  • Possible duplicate of [How can I generate a series of repeating numbers in PostgreSQL?](https://stackoverflow.com/questions/23358333/how-can-i-generate-a-series-of-repeating-numbers-in-postgresql) – sh1hab Aug 25 '19 at 05:05
  • @shihab Fair enough. The solution is similar but the question is different. I want to generate a series of increasing numbers, not repeating. – Alex V Aug 25 '19 at 07:16

2 Answers2

6

Luckily you’re using PostgreSQL, so you can use the generate_series() function.

select generate_series as num from generate_series(1,10)
samredai
  • 662
  • 3
  • 8
2

In the event that you want an array, you can aggregate the values in to an array:

select array_agg(gs.val order by gs.val)
from generate_series(1, 10) gs(val);
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786