Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

How to distribute IDs over many rows using predefined distribution

$
0
0

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

ContainersMarbles
152
44
110

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

MarbleIDContainerID
11
21
32
42
53
63
etc...etc...
2915
3015

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!






Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>