Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
Phibian
Starting Member
32 Posts |
Posted - 2002-07-02 : 11:20:18
|
| I need a select statement that will return a series of numbers, and have not been able to find examples for anything relevant.Eg. A statement that will return 100 201 302 403 etcA statement that returns all numbers between 1900 and 2002.SQL wasn't exactly designed to do this, but can it be done?Thanks, |
|
|
jessop
Starting Member
9 Posts |
Posted - 2002-07-02 : 11:22:34
|
| yes it can be done - but you will need rules for your series and also to define the start and finish points - then loop through your rules |
 |
|
|
VyasKN
SQL Server MVP & SQLTeam MVY
313 Posts |
Posted - 2002-07-02 : 11:52:35
|
| You can use the pivot table/auxilary table (basically a table of numbers) technique to play around with this type of queries.For example, the following will create a table of 10000 numbers starting from 0.--This is for temporary useCREATE TABLE Prep (i char) GOINSERT PrepSELECT '0'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'GO--The following will create a numbers table--with number 0 to 9999, using the table create aboveSELECT CAST(a.i + b.i + c.i + d.i AS int) AS NumberINTO NumbersFROM Prep a CROSS JOIN Prep b CROSS JOIN Prep c CROSS JOIN Prep dGO--The following index helps query performanceCREATE UNIQUE CLUSTERED INDEX NumbersIndex ON Numbers(Number)--The following query will generate a particular --series of numbersSELECT Number+DiffFROM Numbers CROSS JOIN (SELECT 101 AS Diff) AS DfWHERE (Number/101.)-(Number/101) = 0 --I'm poor in math :-)--The following query should generate the--exact series you postedSELECT CASE WHEN Number+Diff = 101 THEN 100 ELSE Number+Diff-1 END FROM Numbers CROSS JOIN (SELECT 101 AS Diff) AS DfWHERE (Number/101.)-(Number/101) = 0 --Poor in math again :-)--HTH,Vyashttp://vyaskn.tripod.com |
 |
|
|
VyasKN
SQL Server MVP & SQLTeam MVY
313 Posts |
Posted - 2002-07-02 : 11:55:19
|
| Silly me, try this WHERE clause instead:SELECT CASE WHEN Number+Diff = 101 THEN 100 ELSE Number+Diff-1 END FROM Numbers CROSS JOIN (SELECT 101 AS Diff) AS Df WHERE Number%101 = 0 --Updated the WHERE clause--HTH,Vyashttp://vyaskn.tripod.com |
 |
|
|
VyasKN
SQL Server MVP & SQLTeam MVY
313 Posts |
Posted - 2002-07-02 : 11:58:45
|
| For one last time, here's the complete code required to produce the sequence 100, 201, 302, 403....SET NOCOUNT ON GOCREATE TABLE Prep ( i char(1)) GO INSERT Prep SELECT '0' UNION ALLSELECT '1' UNION ALLSELECT '2' UNION ALLSELECT '3' UNION ALLSELECT '4' UNION ALLSELECT '5' UNION ALLSELECT '6' UNION ALLSELECT '7' UNION ALLSELECT '8' UNION ALLSELECT '9' GO --The following will create a numbers table --with number 0 to 9999, using the table create above SELECT CAST(a.i + b.i + c.i + d.i AS int) AS Number INTO Numbers FROM Prep a CROSS JOIN Prep b CROSS JOIN Prep c CROSS JOIN Prep d GO --This index will improve performance for some queriesCREATE UNIQUE CLUSTERED INDEX NumbersIndex ON Numbers(Number) --Here's the query that generates the sequenceSELECT CASE Number WHEN 0 THEN 100 ELSE (Number+100) END AS [Sequence]FROM Numbers WHERE Number%101 = 0 ORDER BY [Sequence] ASC--HTH,Vyashttp://vyaskn.tripod.comEdited by - vyaskn on 07/02/2002 12:54:05Edited by - vyaskn on 07/02/2002 16:39:46 |
 |
|
|
MichaelP
Jedi Yak
2489 Posts |
|
|
AndrewMurphy
Master Smack Fu Yak Hacker
2916 Posts |
|
|
|
|
|
|
|