我如何打印一行 x 次,其中 x 是该行中的一个值?
How do i print a row x amout of times, where x is a value in this row?
我正在用 sql 查询的内容填充 iReport,有问题的查询需要 return 订单行 x 次,其中 x 是 [= 的值28=].
这是有问题的查询:
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
PS。我无法使用视图或循环。
您可以使用以下查询来实现相同的目的:
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 按级别显示连接
我正在用 sql 查询的内容填充 iReport,有问题的查询需要 return 订单行 x 次,其中 x 是 [= 的值28=].
这是有问题的查询:
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
PS。我无法使用视图或循环。
您可以使用以下查询来实现相同的目的:
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 按级别显示连接