Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...This is easily the most helpful website I've ever used, and this is the best forum with the quickest response time bar none...."

Geography

Where in the world do Tek-Tips members come from?

How to generate a number of rows based on a count in a table?Helpful Member!(2) 

MdotButler (Programmer)
24 Apr 12 23:49
I have a customer and inventory table.  I have a need to create a result set that includes 1 row for each item in inventory.  If there are 3 items in inventory for a item, it should generate 3 rows.

CODE

CustomersTable Structure
  custid
  name

CustomersTable Rows
  cust01 cust01name
  cust02 cust02name

ItemsTable Structure
  custid
  itemid
  count

ItemsTable Rows
  cust01 item01 2
  cust01 item02 3
  cust02 item01 3
  cust02 item03 2

Desired Result
  cust01 cust01name item01
  cust01 cust01name item01
  cust01 cust01name item02
  cust01 cust01name item02
  cust01 cust01name item02
  cust02 cust02name item01
  cust02 cust02name item01
  cust02 cust02name item01
  cust02 cust02name item03
  cust02 cust02name item03
I am sure there is a way but I am drawing a blank.

TIA
Mark
Helpful Member!  drlex (TechnicalUser)
25 Apr 12 3:37
One way would be the use of a numbers table, joined on the ItemTable Count

Using a table "Numbers" with a single integer field entitled "Counter", fill it with the numbers 1 to 10 and try this:

CODE

SELECT CustomersTable.custid, CustomersTable.custname, ItemsTable.itemid from ItemsTable inner join CustomersTable on ItemsTable.custid = CustomersTable.custid inner join Numbers on Numbers.Counter <= ItemsTable.[count]

There are plenty of bits of code on the internet to quickly fill a numbers table with 1M lines, and many articles that show the use of a numbers table in T-SQL data wrangling; I commend such a table to you!

soi là, soi carré

Helpful Member!  imex (Programmer)
25 Apr 12 16:30
Try:

CODE

with CTE as
(
    select c.custid, c.name, i.itemid, i.count, 1 as seq
    from CustomersTable as c
    join ItemsTable as i on i.custid = c.custid

    union all
    
    select r.custid, r.name, r.itemid, r.count, (r.seq + 1) as seq
    from CTE as r
    where r.seq < r.count
)

select r.custid, r.name, r.itemid
from CTE as r
order by r.custid, r.itemid

Hope this helps.
 

Imoveis em Guarulhos

MdotButler (Programmer)
14 May 12 23:19
Thanx drlex. Solution worked like a champ.

Thanx imex. I did not use your solution as the first fit my requirements better based on the real problem, not the simple one I used in the question. I had not thought of using the union. I will have to file that one away for the next time.

Again thanx.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close