oracle中over()開窗函數的理解

開窗函數指定了分析函數工作的數據窗口大小,這個數據窗口大小可能會隨着行的變化而變化,舉例如下:
over(order by salary) 按照salary排序進行累計,order by是個默認的開窗函數
over(partition by deptno)按照部門分區
over(order by salary range between 50 preceding and 150 following)
每行對應的數據窗口是之前行幅度值不超過50,之後行幅度值不超過150
over(order by salary rows between 50 preceding and 150 following)
每行對應的數據窗口是之前50行,之後150行
over(order by salary rows between unbounded preceding and unbounded following)
每行對應的數據窗口是從第一行到最後一行,等效:
over(order by salary range between unbounded preceding and unbounded following)

eg.

select * from
(
select p.deal_no,p.step_id,p.charge_id,p.dr_acc_no,
sum(p.dr_origin_amt)
over (partition by p.deal_no,p.step_id,p.dr_acc_no order by p.charge_id) as a,
dense_rank()
over (partition by p.deal_no,p.step_id,p.dr_acc_no order by p.charge_id) as b
from ixqdbaci p
where p.entity='002' and p.charge_id not like '99%'
and p.deal_no like '00779010010612%'
) where b=1
sum中的over()加上order by後會變成遞增累加,而不是分組的所有值sum,所以在這條sql中應該把a列的oder by p.charge_id刪除,b列的dense_rank是爲了區分同一組的不同行,並且order by是不可少的。
發佈了45 篇原創文章 · 獲贊 0 · 訪問量 2405
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章