Skip to content

Instantly share code, notes, and snippets.

@cdussud
Created January 29, 2021 19:48
Show Gist options
  • Save cdussud/02b32c786b3e3e967cf25d582e50511c to your computer and use it in GitHub Desktop.
Save cdussud/02b32c786b3e3e967cf25d582e50511c to your computer and use it in GitHub Desktop.
How to generate a numbers table in Redshift
-- Generates a list of sequential integers
-- How it works:
-- empty_rows is 36 rows of 1
-- cross join with itself any number of times as needed
-- a -> 36 rows
-- a, b -> 36*36 rows
-- a, b, c -> 36*36*36 rows
-- row_number gives us our integer for the table
WITH empty_rows as (
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
)
SELECT row_number() over () as row_num
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