Skip to content

Instantly share code, notes, and snippets.

@cdussud
Created September 30, 2021 13:31
Show Gist options
  • Save cdussud/47b57145968f615188113fe28e78dc66 to your computer and use it in GitHub Desktop.
Save cdussud/47b57145968f615188113fe28e78dc66 to your computer and use it in GitHub Desktop.
Generate a sequence of numbers
-- Generates a numbers table
-- Works on Azure SQL, BigQuery, Postgres, Redshift, and Snowflake
--
-- empty_rows generates 36 rows with a single column
-- the FROM statements are cross joins -- each multiplies the number of rows by 36
-- row_number gives us our desired output
-- going all the way to e is 36^5 = 60M rows
WITH empty_rows AS (
SELECT 1 AS n UNION ALL SELECT 1 AS n UNION ALL SELECT 1 AS n UNION ALL SELECT 1 AS n UNION ALL
SELECT 1 AS n UNION ALL SELECT 1 AS n UNION ALL SELECT 1 AS n UNION ALL SELECT 1 AS n UNION ALL
SELECT 1 AS n UNION ALL SELECT 1 AS n UNION ALL SELECT 1 AS n UNION ALL SELECT 1 AS n UNION ALL
SELECT 1 AS n UNION ALL SELECT 1 AS n UNION ALL SELECT 1 AS n UNION ALL SELECT 1 AS n UNION ALL
SELECT 1 AS n UNION ALL SELECT 1 AS n UNION ALL SELECT 1 AS n UNION ALL SELECT 1 AS n UNION ALL
SELECT 1 AS n UNION ALL SELECT 1 AS n UNION ALL SELECT 1 AS n UNION ALL SELECT 1 AS n UNION ALL
SELECT 1 AS n UNION ALL SELECT 1 AS n UNION ALL SELECT 1 AS n UNION ALL SELECT 1 AS n UNION ALL
SELECT 1 AS n UNION ALL SELECT 1 AS n UNION ALL SELECT 1 AS n UNION ALL SELECT 1 AS n UNION ALL
SELECT 1 AS n UNION ALL SELECT 1 AS n UNION ALL SELECT 1 AS n UNION ALL SELECT 1 AS n
)
SELECT row_number() over (order by a.n) as number
FROM empty_rows as a, empty_rows as b, empty_rows as c, empty_rows as d, empty_rows as e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment