How do i print a row x amout of times, where x is a value in this row?

57 Views Asked by At

I'm filling an iReport with content of an sql query, the query in question needs to return the order row for x amount of times, where x is the value of order.qty.

this is the query in question:

select ol.order_id, i.title, i.desc, s.ean, ol.curr, ol.price, i.qty
FROM inventory i
LEFT JOIN dcsdba.sku s
ON (s.title    = i.title
AND s.client_id = i.client_id)
INNER JOIN order_container oc
ON (i.client_id    = oc.client_id
AND i.container_id = oc.container_id)
INNER JOIN order_header oh
ON (oh.order_id  = oc.order_id
AND oh.client_id = oc.client_id)
inner join order_line ol
on (ol.order_id = oc.order_id
and ol.client_id = oh.client_id)
WHERE i.container_id = 'CONTAINER1'
AND i.client_id      = 'TEST'
AND rownum <= i.qty

i.qty is 3 in this example

My current result is :

order_id | title | desc | ean | curr | price | qty
__________________________________________________
order_1  | title1| desc1| ean1|curr1 | price1| qty1

what i need is :

order_id | title | desc | ean | curr | price | qty
__________________________________________________
order_1  | title1| desc1| ean1|curr1 | price1| qty1
order_1  | title1| desc1| ean1|curr1 | price1| qty1 
order_1  | title1| desc1| ean1|curr1 | price1| qty1 


PS. I can't use a view or loops.

2

There are 2 best solutions below

1
Popeye On BEST ANSWER

You can use the following query to achieve the same:

create table tabl(id, X) as
(select 1, 2 from dual
union all
select 2,5 from dual);

select id, x from
(select distinct id, x, level
from tabl
connect by level <= x)
order by id;

db<>fiddle demo

Cheers!!

0
S0me0neRand0m On

Solved it like this:

with DETAILS AS (select
  i.title,
  i.Desc,
  s.ean,
  ol.curr,
  ol.price,
  i.qty
FROM inventory i
LEFT JOIN title s
ON (s.title = i.title
AND s.client_id = i.client_id)
INNER JOIN order_container oc
ON (i.client_id    = oc.client_id
AND i.container_id = oc.container_id)
INNER JOIN order_header oh
ON (oh.order_id  = oc.order_id
AND oh.client_id = oc.client_id)
INNER JOIN order_line ol
ON (ol.order_id      = oc.order_id
AND ol.client_id     = oh.client_id)
where i.container_id = 'DT1'
and i.client_id = 'TEST')
select * from DETAILS d,
(select rownum repeat from dual CONNECT BY LEVEL<=(select max(qty) from DETAILS))r
where D.qty>=r.repeat
order by d.title, r.repeat;

Credit to @Tejash for showing the connect by level