Skip to content

Instantly share code, notes, and snippets.

@dwcullop
Last active June 9, 2016 04:40
Show Gist options
  • Save dwcullop/7195686abc482d1f66da61e78c79333d to your computer and use it in GitHub Desktop.
Save dwcullop/7195686abc482d1f66da61e78c79333d to your computer and use it in GitHub Desktop.
Generates a table listing values from 0 to 99,999 which can be a really handy table if you know what you're doing.
WITH digits AS
(
SELECT *
FROM
(
VALUES (0), (1), (2), (3), (4),
(5), (6), (7), (8), (9)
) AS nums(n)
)
SELECT lOOOO.OO + lOOO.OO + lOO.OO + lO.OO + l.OO AS N
FROM (SELECT n*10000 AS OO FROM digits) lOOOO
, (SELECT n*1000 AS OO FROM digits) lOOO
, (SELECT n*100 AS OO FROM digits) lOO
, (SELECT n*10 AS OO FROM digits) lO
, (SELECT n FROM AS OO digits) l
ORDER BY N;
@dwcullop
Copy link
Author

dwcullop commented Jun 9, 2016

Generates a Numbers table

The basic strategy is to use the VALUES() to emit a table that contain 0-9 and then cross join that table with itself 4 or 5 (or more) times to get up to the values that you need in your numbers table. This one goes up to 99,999.

NOTE: The part that looks like lOOOO is a lower-case "L" and capital letters "O" after it, so that the identifier follows SQL naming rules. If you copy and paste, you will be fine. If you are transcribing by hand for some reason, that might not be immediately obvious. I did it that way so that when you combine it with using the field name of OO (which is already delimited by a decimal point) it looks like adding up lOOOO.OO + lOOO.OO + lOO.OO + lO.OO + l.OO which is just my lame attempt at appearing clever or something.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment