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是不可少的。

你可能感兴趣的:(DB,Oracle,SQL,工作)