我正在努力在查询中获得以下结果
这是我的 table
我想要的结果是这个
基本上只显示 (6,1) 中具有 line_typ 的行,但将其间所有其他行的列成本相加。
感谢您的想法和帮助!!
伊凡
最佳答案
Tim Biegeleisen 的答案非常好,但需要一些中间产品和 oracle 功能的原因,可以在使用最少的 SQL 时看到:
基本复杂度 O(N³),索引略少。
所以:
select h.line_num, h.line_typ,
(select sum(cost)
from tbl g
where g.line_num >= h.line_num
and not exists (select *
from tbl
where line_num > h.line_num
and line_typ in (1, 6)
and line_num <= g.line_num)
) as cost
from tbl h
where h.line_typ in (1, 6)
order by h.line_num
(为非 Oracle 搜索者提供。)
这是一个工作 demo 的链接.
https://stackoverflow.com/questions/73076078/