Search code examples
sqloracle-databaseireport

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


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.


Solution

  • 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!!