Question

  Asked: Jul 25 2005   6:50 PM GMT
  Asked by: Ajibola


Generating Random numbers in SQL


SQL Anywhere Studio/M-Business Anywhere/Avantgo, SQL

I HAVE a problem
How can i use command or functios to combine numbers
For example
If k = 6 and n = 3
The command should return (as B1,B2,B3)
1,2,3
1,2,4
1,3,4
1,3,5
1,3,6
1,4,5
1,4,6
1,5,6
2,3,4
2,3,5
2,3,6
2,4,5
2,4,6
2,5,6
3,4,5
3,4,6
3,5,6
4,5,6

For all n of k numbers
Thanks

Subscribe to Alerts! Get questions and answers delivered to your Inbox.


E-mail me updates on this question



   SUBSCRIBE

hidden modal window

Answer Wiki (Improve, edit or add to this answer)


 RATE THIS ANSWER
0
Click to Vote:
  •   0
  •  0



Sorry Ajibola, I must be having a low caffeine day. I can't figure out what you are trying to do (i.e. what the relationship is between K and n and the B1, B2, B3 list.

Can you expand on this please?
  • AddThis Social Bookmark Button

Browse more Questions and Answers on SQL Server and Development.

Looking for relevant SQL Server Whitepapers? Visit the SearchSQLServer.com Research Library.


Discuss This Answer


You must be logged-in to discuss a question. Log-in/Register

stevewaltz  |   Jul 26 2005  8:01PM GMT

I believe you are trying to choose all the distinct ways n numbers can be chosen from the first k positive integers without regard to order. By “without regard to order” I mean, for instance, that (1,2,3); (1,3,2); (2,1,3); (2,3,1); (3,1,2); and (3,2,1) are considered the same set of the 3 digits 1, 2 and 3. One way to do it is to create a single column table of integers (Oracle has had a way to do this without actually creating a table for a while now). The SQL below will work for the specific example. With this technique, k can be varied easily or provided as a variable, but the SQL has to be modified to deal with different values of n. (Just curious; why is the subject “Generating Random numbers in SQL”?)

select a.integer_value b1,
b.integer_value b2,
c.integer_value b3
from integer_table a,
integer_table b,
integer_table c
where a.integer_value <b>