Skip to content

Instantly share code, notes, and snippets.

@dhaninugraha
Created January 9, 2019 02:03
Show Gist options
  • Save dhaninugraha/dae8c1582bd20611386deccb59828fae to your computer and use it in GitHub Desktop.
Save dhaninugraha/dae8c1582bd20611386deccb59828fae to your computer and use it in GitHub Desktop.
[SQL] generate a number sequence without external functions
with ones as (
select base.n from (values (0), (1), (2), (3), (4), (5), (6), (7), (8), (9)) base(n)
), tens as (
select 10 * ones.n as n from ones
), hundreds as (
select 100 * ones.n as n from ones
--), thousands as (
-- select 1000 * ones.n as n from ones
), num_series as (
select
ones.n + tens.n + hundreds.n /*+ thousands.n*/ as n
from ones, tens, hundreds/*, thousands*/
order by n asc
)
-- this will return a sequence of 0-999
select n from num_series;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment