我在Oracle数据库11g中有这个表:
NUNOTA SEQUENCIA QTD_CONTROLE QTDNEG
---------- ---------- ------------ ----------
446 1 30 60
446 2 30 30
446 3 30 120
我需要获得这样的结果:
NUNOTA SEQUENCIA QTD_CONTROLE QTDNEG
---------- ---------- ------------ ----------
446 1 30 30
446 1 30 30
446 2 30 30
446 3 30 30
446 3 30 30
446 3 30 30
446 3 30 30
基本上是QTDNEG / QTD_CONTROLE,结果必须是行数。
我将使用结果在FreeReport中打印标签
我尝试使用Oracle Views来获得结果但对我来说很难。
另一个例子:
NUNOTA SEQUENCIA QTD_CONTROLE QTDNEG
---------- ---------- ------------ ----------
446 1 30 60
446 2 100 300
446 3 15 30
结果应为
NUNOTA SEQUENCIA QTD_CONTROLE QTDNEG
---------- ---------- ------------ ----------
446 1 30 30
446 1 30 30
446 2 100 100
446 2 100 100
446 2 100 100
446 3 15 15
446 3 15 15
答案 0 :(得分:1)
递归解决方案(可从Oracle 11gR2获得):
with t(nunota, sequencia, qtd_controle, qtdneg) as
(select nunota
, sequencia
, qtd_controle
, qtdneg
from mytable
union all
select nunota
, sequencia
, qtd_controle
, qtdneg - qtd_controle
from t
where qtdneg - qtd_controle > 0)
select nunota
, sequencia
, qtd_controle
, least(qtdneg, qtd_controle)
from t
order by nunota
, sequencia
, least(qtdneg, qtd_controle) desc
答案 1 :(得分:0)
解决这个问题的一种方法是生成一个足够大的数字列表"然后加入该列表。我认为结果查询(在您的情况下)是这样的:
with n(n) as (
select level
from dual
where level <= 100
)
select tt.NUNOTA, tt.SEQUENCIA, tt.QTD_CONTROLE, 30 as QTDNEG
from thistable tt join
n
on n.n <= tt.QTDNEG / tt.QTD_CONTROLE ;
答案 2 :(得分:0)
试试这个:
with t_res (NUNOTA,SEQUENCIA,QTD_CONTROLE,QTDNEG, lvl) as (
select NUNOTA,SEQUENCIA,QTD_CONTROLE,QTDNEG, 1
from t
union all
select t.NUNOTA,t.SEQUENCIA,t.QTD_CONTROLE,t.QTDNEG, t_res.lvl + 1
from t, t_res
where t_res.lvl < t.QTDNEG/t.QTD_CONTROLE
)
select unique NUNOTA,SEQUENCIA,QTD_CONTROLE,QTD_CONTROLE, lvl
from t_res
order by SEQUENCIA, lvl
NUNOTA SEQUENCIA QTD_CONTROLE QTD_CONTROLE LVL
-------------------------------------------------------
446 1 30 30 1
446 1 30 30 2
446 2 30 30 1
446 3 30 30 1
446 3 30 30 2
446 3 30 30 3
446 3 30 30 4