Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

SQL row generation with connect by level

dvsoukupSep 12 2014 — edited Sep 12 2014

Hello,

I'm attempting to generate rows based off of a part numbers quantity.  I've looked all around at several techniques for row generation, but have been unsuccessful in accomplishing this.  The current method it seems is pulling a permutation of all possible combinations, and the result quickly amounts to millions/billions of records... which is not the answer.  Anyone out there that could help with writing a query to do this?

What I'm trying to achieve is if a part qty has 5, then I want to display 5 records of qty 1.  If It's qty 12, then 12 records of qty 1.  So on and so forth.

Operating off Oracle 10.2.0.4

Sample data:

drop table test;

create table test (part varchar2(20), qty number);

insert into test values ('WIDGET', 12);

insert into test values ('FOO', 5);

insert into test values ('THING', 3);

insert into test values ('WAD', 8);

commit;

--this query doesn't work... I think it's getting all possible permutations

--select part, 1

--from test

--connect by level <= qty

--what I want to see....

part        qty

------      ----

WIDGET      1

WIDGET      1

WIDGET      1

WIDGET      1

WIDGET      1

WIDGET      1

WIDGET      1

WIDGET      1

WIDGET      1

WIDGET      1

WIDGET      1

WIDGET      1

FOO         1

FOO         1

FOO         1

FOO         1

FOO         1

THING       1

THING       1

THING       1

WAD         1

WAD         1

WAD         1

WAD         1

WAD         1

WAD         1

WAD         1

WAD         1

This post has been answered by Frank Kulash on Sep 12 2014
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 10 2014
Added on Sep 12 2014
5 comments
1,088 views