Created
January 9, 2014 12:11
-
-
Save palpha/8333162 to your computer and use it in GitHub Desktop.
PostgreSQL: generate a random series with configurable start, max and step.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
with recursive t (n) as ( | |
with base as ( | |
with settings as ( | |
select | |
10000 as max | |
, array[1000,2000] as start_range | |
, array[10,20] as step_range | |
) | |
select | |
(random() * (start_range[2] - start_range[1]) + start_range[1])::int as start | |
, step_range | |
, max | |
from settings | |
) | |
select start from base | |
union all | |
select n + (random() * (step_range[2] - step_range[1]) + step_range[1])::int | |
from t cross join base | |
where n < max | |
) | |
select n | |
from t; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment