1.rownum:rownum是一個偽列,需要在數據取出來后,rownum才會有值,因此在分頁查找時,需要進行嵌套查詢。
select sal,ename from (select rownum as rn,sal,ename from (select sal,ename from emp where sal is not null order by sal) x where rownum<10) where rn>6
采用分析函數也是可以實現一次嵌套
select rn,ename,sal from (select rownum() over(order by sal) as rn,sal,ename where sal is not null ) x where rn between 6 and 10
但是由於分析函數的影響,有些索引可能失效,建議大家采用第一種寫法。
隔行返回數據,對偽列求余即可,MOD(rn,X)
2.Merge:高效的表更新處理
Merge inot test a using (select rowid as rid,nbr*100+rownum() over(partition by nbr order by rowid) as nnbr from test) b on(a.rowid=b.rid) when matched then update set a.nbr = b.nnbr;
大家猜猜 test表掃描了幾次?????
3.將表中某些列,排列組合去重
step1:列轉行
select * from test unpivot(b2 for b3 in(t1,t2,t3)
unpivot行列轉置非常牛逼的一個方法
step2:按照值排序並合並
with x1 as (select * from test unpivot(b2 for b3 in(t1,t2,t3)), select id,listagg(b2,',') within group (order by b2) as b from x1 group by id;
listagg實現分組后,值排序后,值按逗號拼接
step3:去除重復
with x1 as (select * from test unpivot(b2 for b3 in(t1,t2,t3)), x2 as ( select id,listagg(b2,',') within group (order by b2) as b from x1 group by id) select id,b,row_number() over(partition by b order by id) as sn from x2
