If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Write SQL to solve a combination problem

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-13-11, 08:17
oppee oppee is offline
Registered User
 
Join Date: Apr 2011
Posts: 6
Write SQL to solve a combination problem

Hi, I want to write a SQL StoredProcedure without using the cursor to solve a combination problem.

Here is the scenario:

5 Pool balls
Pool ball no: 1, 2, 3, 4, 5

Assign those balls to three people:
A – Choose ball 1, 2
B – Choose ball 3, 4
C – Choose ball 5

Then, let’s assume we have the following combination of the people who processed the balls:

1st combination: A + B
2nd combination: B + C
3rd combination: A + C
4th combination: A + B + C

/*====== Sample Data =========*/
combination_no Person Pool ball
1 A 1
1 A 2
1 B 3
1 B 4
2 B 3
2 B 4
2 C 5
3 A 1
3 A 2
3 C 5
4 A 1
4 A 2
4 B 3
4 B 4
4 C 5

Now, I want to know the combination of pool balls as follow:
For example:

In 1st combination: A + B will look like this:
A’s Pool ball B’s Pool Ball
Pool ball 1 Pool ball 3
Pool ball 1 Pool ball 4
Pool ball 2 Pool ball 3
Pool ball 2 Pool ball 4
…. …..
Remark: (combination 2 to3 will follow the same combination rule)

In 4th combination: A + B + C
A’s Pool ball B’s Pool Ball C’s Pool Ball
Pool ball 1 Pool ball 3 Pool ball 5
Pool ball 1 Pool ball 4 Pool ball 5
Pool ball 2 Pool ball 3 Pool ball 5
Pool ball 2 Pool ball 4 Pool ball 5


How can I get the result like below from the sample data??Many Thanks
/*====== Wanted Result =========*/
combination_no Person Pool ball
1 A 1
1 B 3
2 A 1
2 B 4
3 A 2
3 B 3
4 A 2
4 B 4
5 B 3
5 C 5
6 B 4
6 C 5
7 A 1
7 C 5
8 A 2
8 C 5
9 A 1
9 B 3
9 C 5
10 A 1
10 B 4
10 C 5
11 A 2
11 B 3
11 C 5
12 A 2
12 B 4
12 C 5
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On