[英]How do i print a row x amout of times, where x is a value in this row?
我正在用 sql 查詢的內容填充 iReport,有問題的查詢需要返回訂單行 x 次,其中 x 是 order.qty 的值。
這是有問題的查詢:
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 是 3
我目前的結果是:
order_id | title | desc | ean | curr | price | qty
__________________________________________________
order_1 | title1| desc1| ean1|curr1 | price1| qty1
我需要的是:
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
附注。 我不能使用視圖或循環。
您可以使用以下查詢來實現相同的目的:
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;
干杯!!
是這樣解決的:
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;
感謝@Tejash 按級別顯示連接
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.