[英]Oracle 10g Recursive Query
I have the following recursive query which would run in 11g but is not supported in Oracle 10g database: 我有以下递归查询,该查询将在11g中运行,但在Oracle 10g数据库中不受支持:
with st as (
select
rownum id,
case
when rownum-1 < 1 then null
else rownum-1
end parent_id,
customer,
tickets
from tickets
),
st2(id, parent_id, customer, tickets, offset) as (
select
id, parent_id, shuffler_id, subno, contrno, tickets, 0 offset
from st
where id = 1
union all
select
st2.id, st2.parent_id, st2.tickets, (st.tickets + st.offset) offset
from st, st2
where st2.parent_id = st.id
)
select * from st2
What I'm trying to do is to calculate an offset for each row based on its previous row columns tickets
and offset
(same calculated column), and I'm starting the first row with an offset of 0; 我想要做的是根据前一行的
tickets
和offset
(相同的计算列)为每一行计算偏移量,并且我从偏移量0开始第一行; the fact that I need to depend on a column that I'm calculating during the execution of the query requires recursion. 我需要依赖于查询执行期间正在计算的列这一事实需要递归。
The issue is the above query is not supported in Oracle 10g, so I tried to use connect by
instead which works, but the ugly part is it's extremely inefficient: 问题是上面的查询在Oracle 10g中不受支持,因此我尝试使用
connect by
代替,但是有效的是,它效率极低:
with st as (
select
rownum id,
case
when rownum-1 < 1 then null
else rownum-1
end parent_id,
customer,
tickets
from tickets
)
select
id,
parent_id,
customer,
tickets,
(
select nvl(sum(tickets), 0) from st
where level < x.id
start with id = 1
connect by prior id = parent_id
) offset
from st x
Using the 2nd query I'm summing all the previous rows, which does the job but is redundant as well and I cannot depend on when this table grows to millions of records. 使用第二个查询,我对所有先前的行进行求和,这可以完成工作,但也很多余,我不能依赖于该表何时增长到数百万个记录。
Any idea on how to implement something similar to the first query in Oracle 10g database? 关于如何在Oracle 10g数据库中实现类似于第一个查询的任何想法?
select
id,
customer,
tickets,
nvl(
sum(tickets) over (
order by id rows between unbounded preceding and 1 preceding
), 0) as offset
from (
select
rownum id,
customer,
tickets
from tickets
)
or even more shorter (without introducing id
s) 甚至更短(不引入
id
)
select
customer,
tickets,
nvl(
sum(tickets) over (
order by rownum rows between unbounded preceding and 1 preceding
), 0) as offset
from tickets
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.