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.