Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

SQL list of numbers 1-100

6,082 views
Skip to first unread message

R. Soby@discussions.microsoft.com Jason R. Soby

unread,
Jul 14, 2009, 1:22:13 PM7/14/09
to
Hi, i just need to make a list of numbers 1 - 100.

Right now, i can only think of

Select '1'
UNION
Select '2'
...All the way to 100. Is there a good, efficient, way of doing this other
than 100 unions? Thanks!

Jason

Plamen Ratchev

unread,
Jul 14, 2009, 1:29:09 PM7/14/09
to
Here is one method (SQL Server 2005/2008):

WITH Num1 (n) AS (SELECT 1 UNION ALL SELECT 1),
Num2 (n) AS (SELECT 1 FROM Num1 AS X, Num1 AS Y),
Num3 (n) AS (SELECT 1 FROM Num2 AS X, Num2 AS Y),
Num4 (n) AS (SELECT 1 FROM Num3 AS X, Num3 AS Y),
Nums (n) AS (SELECT ROW_NUMBER() OVER(ORDER BY n) FROM Num4)
SELECT n FROM Nums
WHERE n <= 100;

--
Plamen Ratchev
http://www.SQLStudio.com

Bob

unread,
Jul 14, 2009, 2:35:01 PM7/14/09
to
SQL 2005 onwards ...

;WITH cte AS
(
SELECT 1 x
UNION ALL
SELECT x + 1
FROM cte
WHERE x < 100
)
SELECT x
FROM cte

Jason R. Soby

unread,
Jul 14, 2009, 3:13:01 PM7/14/09
to
Wow...thanks so much guys. could you explain what each of those methods do?
I'm a newbie at SQL

Plamen Ratchev

unread,
Jul 14, 2009, 4:37:44 PM7/14/09
to
The one that I posted does a cross join of CTEs to generate a large set of numbers. You can understand it better by
using only 2 CTEs first, then adding one more, etc. That is a very efficient technique.

Bob's solution uses a recursive CTE. In essence it is a loop that increments with each recursive call.

Uri Dimant

unread,
Jul 15, 2009, 4:15:43 AM7/15/09
to
Jason
Another one using CROOS JOIN command
WITH digits AS (

SELECT 0 as Number

UNION SELECT 1

UNION SELECT 2

UNION SELECT 3

UNION SELECT 4

UNION SELECT 5

UNION SELECT 6

UNION SELECT 7

UNION SELECT 8

UNION SELECT 9

)

SELECT (tens.Number * 10) + ones.Number +1 as Number

FROM digits as tens

CROSS JOIN digits as ones

ORDER BY Number

"Jason R. Soby" <Jason R. So...@discussions.microsoft.com> wrote in message
news:60BC4144-5A94-48A5...@microsoft.com...

Jason R. Soby

unread,
Jul 15, 2009, 8:18:15 AM7/15/09
to
Thanks so much to all of you. This helps me a ton!!! :)

Jason R. Soby

unread,
Jul 15, 2009, 10:35:01 AM7/15/09
to
Ok. Sorry to ask this, but what if i wanted to go in increments of .25 with
either of the queries. i dont understand the logic enough to manage on my own
:)

Thanks so much for ya'lls help

Plamen Ratchev

unread,
Jul 15, 2009, 10:43:19 AM7/15/09
to
Try this:

WITH Num1 (n) AS (SELECT 1 UNION ALL SELECT 1),
Num2 (n) AS (SELECT 1 FROM Num1 AS X, Num1 AS Y),
Num3 (n) AS (SELECT 1 FROM Num2 AS X, Num2 AS Y),
Num4 (n) AS (SELECT 1 FROM Num3 AS X, Num3 AS Y),

Num5 (n) AS (SELECT 1 FROM Num4 AS X, Num4 AS Y),
Nums (n) AS (SELECT ROW_NUMBER() OVER(ORDER BY n) FROM Num5)
SELECT (n - 1) * 0.25
FROM Nums
WHERE n <= 100 * 4 + 1;

Jason R. Soby

unread,
Jul 15, 2009, 11:28:01 AM7/15/09
to
That worked, but the query ended at 63.75. Any reason why?

--CELKO--

unread,
Jul 15, 2009, 11:45:33 AM7/15/09
to
>> i just need to make a list of numbers 1 - 100.<<

You can literally do that:

CREATE VIEW OneHundred(seq)
AS
SELECT i
FROM (VALUES (1), (2), ..(100)) AS X(i);

Plamen Ratchev

unread,
Jul 15, 2009, 12:04:32 PM7/15/09
to
You did not use the query that I posted. Most likely you used the prior version. Note here I added one more CTE to
generate sufficient volume of numbers.

Tom Cooper

unread,
Jul 15, 2009, 2:14:07 PM7/15/09
to
In addition to the other replies, I would recommend you create a Numbers
table. See
http://www.aspfaq.com/show.asp?id=2516
for what the are and why you want one.

Once you have created the Numbers table, then your first query is just

Select Number
From Numbers
Where Number <= 100
Order By Number;

and your second query is

Select Number * 0.25
From Numbers
Where Number <= 400
Order By Number;

Also, note that Joe Celko's solution only works if you are on SQL Server
2008. It uses a new feature that does not exist in previous versions of SQL
Server.

Tom

"Jason R. Soby" <Jason R. So...@discussions.microsoft.com> wrote in message
news:60BC4144-5A94-48A5...@microsoft.com...

Uri Dimant

unread,
Jul 16, 2009, 3:36:45 AM7/16/09
to
CELKO
How much does it take typing values till 10000000 for example? :-))) Are
you serious Joe?


"--CELKO--" <jcel...@earthlink.net> wrote in message
news:3c5d8abb-499d-4892...@i6g2000yqj.googlegroups.com...

--CELKO--

unread,
Jul 16, 2009, 7:32:15 AM7/16/09
to
> How much does it take  typing values till 10000000 for example? :-))) Are you serious Joe?<<

If he wants a different table of constants, we can try a different
approach. I also don't do as lot of repetitive typing -- there are
tools for that,

You missed my point; SQL is a declarative data language and not a
computational one. Even in your procedural mindset, would write a
loop to compute pi every place you use it? Or would you declarative
it once as a global constant?

Uri Dimant

unread,
Jul 16, 2009, 8:12:31 AM7/16/09
to
>If he wants a different table of constants, we can try a different
>approach. I also don't do as lot of repetitive typing -- there are
>tools for that,

heheheh like
INSERT INTO tbl DEFAULT VALUES
GO 10000

"--CELKO--" <jcel...@earthlink.net> wrote in message

news:deb44c78-868d-470e...@d32g2000yqh.googlegroups.com...

Jason R. Soby

unread,
Jul 19, 2009, 6:04:01 PM7/19/09
to
The goal if this is simply to create two columns. One with the decimal value
of a percent eg: 0.01 and it's friendly name equivalent 1% from 1 > 100 in
increments of .25.

Paddy

unread,
Jul 20, 2009, 5:58:04 AM7/20/09
to
create table #tbl(i int identity)
Go
INSERT INTO #tbl DEFAULT VALUES
GO 100
go
select * from #tbl
go

create table #tbl2(i decimal (10,2))
Insert into #tbl2 Values (.00)
Insert into #tbl2 Values (.25)
Insert into #tbl2 Values (.5)
Insert into #tbl2 Values (.75)

select
a.i + b.I as DecimalValue,
Cast((a.i + b.I) as Varchar(10)) + ' Percentage' as 'FriendlyName'
From
#tbl a,
#tbl2 b
Order By 1

drop table #tbl2
drop table #tbl

0 new messages