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 |
|
Hotice
Starting Member
9 Posts |
Posted - 2006-06-02 : 16:53:49
|
| ProblemI want to use loop to get all the possible combinations of number from 1 to 5 ( or even to n). For example for 1 to 5, I want to get 123451, 21, 31, 41, 5 2, 3…1, 2, 31, 3, 4…1,2, 3, 4, 5I wrote the following code declare @counter intdeclare @counter1 intdeclare @counter2 intset @counter =0set @counter1 =0set @counter2 =0while @counter<5begin set @counter= @counter + 1 while @counter1 < 4 begin set @counter1 = @counter1+1 while @counter2< 3 set @counter2 =@counter2 + 1 endprint cast(@counter as varchar(20))+ ',' + cast ( @counter1 as varchar(20))+',' + cast ( @counter2 as varchar(20))endThe result is as following:1,4,32,4,33,4,34,4,35,4,3But result is not what I want. Is there a way to use loop in SQL server to display all the possible combination? Also if I don’t have a definite end number (for example, it might be from 1 to 5, or it may be from 1 to 10), then how should I code the loop?Thanks. |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2006-06-02 : 17:29:52
|
Use a cross join, instead of a loop.Function F_TABLE_NUMBER_RANGE is available here:http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=47685select n1 = a.number, n2 = b.number, n3 = c.numberfrom dbo.F_TABLE_NUMBER_RANGE(1,5) a cross join dbo.F_TABLE_NUMBER_RANGE(1,5) b cross join dbo.F_TABLE_NUMBER_RANGE(1,5) corder by 1,2,3 Results:n1 n2 n3 ----------- ----------- ----------- 1 1 11 1 21 1 31 1 41 1 51 2 11 2 21 2 31 2 41 2 51 3 11 3 21 3 31 3 41 3 51 4 11 4 21 4 31 4 41 4 51 5 11 5 21 5 31 5 41 5 52 1 12 1 22 1 32 1 42 1 52 2 12 2 22 2 32 2 42 2 52 3 12 3 22 3 32 3 42 3 52 4 12 4 22 4 32 4 42 4 52 5 12 5 22 5 32 5 42 5 53 1 13 1 23 1 33 1 43 1 53 2 13 2 23 2 33 2 43 2 53 3 13 3 23 3 33 3 43 3 53 4 13 4 23 4 33 4 43 4 53 5 13 5 23 5 33 5 43 5 54 1 14 1 24 1 34 1 44 1 54 2 14 2 24 2 34 2 44 2 54 3 14 3 24 3 34 3 44 3 54 4 14 4 24 4 34 4 44 4 54 5 14 5 24 5 34 5 44 5 55 1 15 1 25 1 35 1 45 1 55 2 15 2 25 2 35 2 45 2 55 3 15 3 25 3 35 3 45 3 55 4 15 4 25 4 35 4 45 4 55 5 15 5 25 5 35 5 45 5 5(125 row(s) affected) CODO ERGO SUM |
 |
|
|
Hotice
Starting Member
9 Posts |
Posted - 2006-06-02 : 18:23:20
|
| Thank you for the replay. The function is cool and fast. But I still have the problem of output format. I will need the output to show like what I showed above(with comma to separate values, ect.). If the number is not too big(such as from 1 to 5), I can manage to output this kind of format. but if the number grows big, then I am not sure how to get the output in the format that I want. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-06-02 : 22:03:28
|
simpleselect convert(varchar(10), a.NUMBER) + ',' + convert(varchar(10), b.NUMBER) + ',' + convert(varchar(10), c.NUMBER)from dbo.F_TABLE_NUMBER_RANGE(1,5) a cross join dbo.F_TABLE_NUMBER_RANGE(1,5) b cross join dbo.F_TABLE_NUMBER_RANGE(1,5) corder by a.NUMBER, b.NUMBER, c.NUMBER KH |
 |
|
|
Arnold Fribble
Yak-finder General
1961 Posts |
Posted - 2006-06-03 : 04:30:19
|
Combinations, not permutations!SELECT N.NUMBER, SUBSTRING( CASE WHEN N.NUMBER & 1 > 0 THEN ', 1' ELSE '' END + CASE WHEN N.NUMBER & 2 > 0 THEN ', 2' ELSE '' END + CASE WHEN N.NUMBER & 4 > 0 THEN ', 3' ELSE '' END + CASE WHEN N.NUMBER & 8 > 0 THEN ', 4' ELSE '' END + CASE WHEN N.NUMBER & 16 > 0 THEN ', 5' ELSE '' END, 3, 8000) AS combinationFROM dbo.F_TABLE_NUMBER_RANGE(1, 31) AS NORDER BY SIGN(N.NUMBER & 1) + SIGN(N.NUMBER & 2) + SIGN(N.NUMBER & 4) + SIGN(N.NUMBER & 8) + SIGN(N.NUMBER & 16), N.NUMBER & 1 DESC, N.NUMBER & 2 DESC, N.NUMBER & 4 DESC, N.NUMBER & 8 DESC, N.NUMBER & 16 DESC |
 |
|
|
PSamsig
Constraint Violating Yak Guru
384 Posts |
Posted - 2006-06-03 : 05:50:58
|
| You are one smart dude Arnold [emote]Bows to superiority[/emote]-- This one's tricky. You have to use calculus and imaginary numbers for this. You know, eleventeen, thirty-twelve and all those. |
 |
|
|
Arnold Fribble
Yak-finder General
1961 Posts |
Posted - 2006-06-03 : 07:54:34
|
SQL Server 2005, using recursive CTEs. Change maxnum as required:WITH MaxNum(maxnum) AS ( SELECT 5 ), Pair(n) AS ( SELECT 0 UNION ALL SELECT 1 ), Comb(lvl, onbits, n, s) AS ( SELECT 1, P.n, P.n, CAST(CASE WHEN P.n = 1 THEN ', 1' ELSE '' END AS varchar(100)) FROM Pair AS P UNION ALL SELECT C.lvl + 1, C.onbits + P.n, C.n * 2 + P.n, CAST(C.s + CASE WHEN P.n = 1 THEN ', ' + CAST(C.lvl+1 AS varchar(100)) ELSE '' END AS varchar(100)) AS s FROM Comb AS C INNER JOIN MaxNum AS M ON C.lvl < M.maxnum CROSS JOIN Pair AS P )SELECT SUBSTRING(C.s, 3, 8000)FROM Comb AS CINNER JOIN MaxNum AS M ON C.lvl = M.maxnumWHERE C.n > 0ORDER BY C.onbits, C.n DESC |
 |
|
|
PSamsig
Constraint Violating Yak Guru
384 Posts |
Posted - 2006-06-03 : 13:06:46
|
| Oh man .... I hardly have begun to grasp the first one yet ... and now this !!-- This one's tricky. You need Arnold Fribble to solve this! |
 |
|
|
|
|
|
|
|