I have a table that shows how many rows in one table should be assigned IDs in another table. The best anology I can think of using is containers and marbles. So here is what the distribution table looks like
Containers | Marbles |
15 | 2 |
4 | 4 |
1 | 10 |
What this is telling is that for every container there are x number of marbles in it. For example, take the first row, 15 of the containers must have 2 marbles each. So for the above there are a total of 20 containers and a total of 56 marbles.
Each container has an ID assigned to it, container 1 to 20, as does the marbles, marble 1 to 56. The idea is to assign the container ID to the number of marbles it should contain. Taking the first row from the example above, the end result should look like this
MarbleID | ContainerID |
1 | 1 |
2 | 1 |
3 | 2 |
4 | 2 |
5 | 3 |
6 | 3 |
etc... | etc... |
29 | 15 |
30 | 15 |
The use of a cursor is not desired as performance will be terrible when this is used against tables with millions of rows. The preference is this should be done in a CTE using recursion or a tally/numbers table. I've been trying but have not hit on what will work to create the desired results. Has anyone done this or can provide a method to do so?
Thanks!