0

如何根据数量将以下列表转换为多行但按项目分组?

当前结果。

ITEM     QTY
-----    ----
12345     1
12346     2
12347     3

这是想要的结果

ITEM     QTY
-----    ----
12345     1
12346     1
12346     1
12347     1
12347     1
12347     1

这是查询

SELECT a.item,sum(b.qty) as qty

FROM table1 a, table2 b 
WHERE a.item_id = b.item_id 
group by a.item
order by a.item_id
4

1 回答 1

1

您可以通过将 应用于rownum记录来做到这一点:

select t1.item, qty / qty as qty
from yourtable t1
left join
(
  select rownum rn, item
  from yourtable
) t2
  on t1.qty >= t2.rn
order by t1.item

SQL Fiddle with Demo

于 2012-09-17T21:57:04.907 回答