title: Projection/Replication in SQL Query?
body: <p>My SQL is a bit rusty -- is there a SQL way to project an input table that looks something like this:</p>
  
  <pre><code>Name                SlotValue              Slots
  ----                ---------              -----
  ABC                 3                      1
  ABC                 4                      2
  ABC                 6                      5
  </code></pre>
  
  <p>Into a 'projected' result table that looks like this:</p>
  
  <pre><code>Name                SlotSum                 Slot
  ----                -------                 ----
  ABC                 13                      1
  ABC                 10                      2
  ABC                 6                       3
  ABC                 6                       4
  ABC                 6                       5
  </code></pre>
  
  <p>In other words, the result set should contain a number of rows equal to MAX(Slots), enumerated (Slot) from 1 to MAX(Slots), and Sum for each of these 'slots' should reflect the sum of the SlotValues projected out to the 'Slots' position. for the pathological case:</p>
  
  <pre><code>Name               SlotValue                Slots
  ----               ---------                -----
  ABC                4                        3
  </code></pre>
  
  <p>we should get:</p>
  
  <pre><code>Name               SlotSum                  Slot
  ----               -------                  ----
  ABC                4                        1
  ABC                4                        2
  ABC                4                        3
  </code></pre>
  
  <p>The summation logic is pretty straightforward -- project each SlotValue out to the number of Slots:</p>
  
  <pre><code>SlotValue         SlotValue        SlotValue       Slot             Sum
  ---------         ---------        ---------       ----             ---
  3                 4                6               1                13 (3+4+6)
  0                 4                6               2                10 (0+4+6)
  0                 0                6               3                6 (0+0+6)
  0                 0                6               4                6 (0+0+6)
  0                 0                6               5                6 (0+0+6)
  </code></pre>
  
  <p>UPDATE: In the end I used a variant of LOCALGHOST's approach in a stored proc. I was hoping there might be a way to do this without a loop.</p>