
本人朋友圈经常会分享一些技术文章的点评,对优化感兴趣的朋友可以加微信 ora_service :

下面步入本文正题:
这是2024年墨天轮社区搞的一个活动:
解锁SQL的无限可能 | 墨天轮SQL挑战赛第二期,等你来战!
(链接:https://www.modb.pro/db/1826549073320697856

--建表并生成测试数据:create table test_gen as(select 1 as seqno,1 as amount from dualunion allselect 2 as seqno,null as amount from dualunion allselect 3 as seqno,null as amount from dualunion allselect 4 as seqno,null as amount from dualunion allselect 5 as seqno,2 as amount from dualunion allselect 6 as seqno,null as amount from dualunion allselect 7 as seqno,null as amount from dualunion allselect 8 as seqno,3 as amount from dualunion allselect 9 as seqno,null as amount from dualunion allselect 10 as seqno,null as amount from dualunion allselect 11 as seqno,5 as amount from dual);
select seqno,amount,last_value(amount) ignore nullsover(order by seqno) as amount_allfrom test_gen aorder by seqno;
select seqno,amount,nvl(amount,lag(amount) ignore nullsover(order by seqno)) as amount_allfrom test_gen aorder by seqno;
select seqno,amount,(select b.amount from test_gen bwhere b.seqno<=a.seqnoand b.amount is not nullorder by b.seqno descfetch first 1 rows only) as amount_allfrom test_gen aorder by seqno;
select a.seqno,a.amount,x.amount as amount_allfrom test_gen aouter apply(select * from test_gen bwhere a.seqno>=b.seqnoand b.amount is not nullorder by b.seqno descfetch first 1 rows only)xorder by 1;
select seqno,amount,amount_allfromtest_gen amatch_recognize(order by seqnomeasuresfirst(amount) as amount_allall rows per matchpattern(a b*)define b as amount is null);
select seqno,amount as amount_allfrom test_gen_2model return updated rowsdimension by (row_number() OVER (ORDER BY seqno) rn)measures( seqno,amount )rules (amount[any] = decode(amount[cv(rn)], null, NVL(amount[cv(rn) - 1], 0), amount[cv(rn)]))order by seqno;
with cte0 as(select row_number() over (order by seqno) as rn, a.* from test_gen a),cte1(rn,seqno,amount,amount_all) as(select rn,seqno, amount, amount as amount_allfrom cte0where amount is not nullunion allselect t.rn,t.seqno, t.amount, nvl(t.amount, x.amount_all) as amount_allfrom cte0 t,cte1 xwhere t.rn = x.rn + 1 and t.amount is null)select seqno, amount, amount_allfrom cte1order by seqno;
with mid1 as(select a.*,count(amount) over (order by seqno) as group#from test_gen_2 a)select a.seqno,a.amount,first_value(amount) over (partition by group#) amount_all_1,max(amount) over (partition by group#) amount_all_2,min(amount) over (partition by group#) amount_all_3from mid1 aorder by seqno;

文章转载自老虎刘谈oracle性能优化,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




