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!

Amplify rows in query

Scott WesleyMay 3 2012 — edited May 4 2012
G'day SQL forum,

I'm looking for an elegant solution to this problem, and so far nothing is coming to mind (except maybe something scary like the model clause)

Given this test case:
create table my_table
  (id   number
  ,name varchar2(100)
  ,qty  number
);  

insert into my_table values (1, 'Scott', 3);
insert into my_table values (2, 'Penny', 2);
insert into my_table values (3, 'Eddie', 1);
insert into my_table values (4, 'Ray'  , 1);
I would like my output to be
ID  NAME  
--- -----
1   Scott
1   Scott
1   Scott
2   Penny
2   Penny
3   Eddie
4   Ray
Hence amplifying each row by the number in the QTY field.

Just before I posted a solution I came up with involves analytics, but I feel it could be better.
select * from (
  select id, name, qty
   ,row_number() over (partition by name order by null) rn
  from my_table 
  connect by level <= 2)
where qty >= rn
Any ideas for something neater?

Scott.
This post has been answered by 908002 on May 3 2012
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 1 2012
Added on May 3 2012
9 comments
127 views