ORACLE over中range和rows的区别

16 篇文章 0 订阅
13 篇文章 0 订阅

ROWS

按salary排序,计算当前行到下面两行的salary累加值,红色部分为例,可以看到2340等于3个salary的值,物理行范围,一眼看就看出来了

select id,
       last_name,
       salary,
       dept_id,
       sum(salary) over(order by salaryrows between current row and 2 following) AS rslt
from s_emp;
IDLAST_NAMESALARYDEPT_IDRSLT
20Newman750.00432340
19Patel795.00422390
23Patel795.00342445
22Chang800.00442510
21Markarian850.00432650
24Dancs860.00452900
17Smith940.00413140
8Biri1100.00433400
25Schwartz1100.00453500
18Nozaki1200.00423650
6Urguhart1200.00413750
7Menchu1250.00423857
9Catchpole1300.00444007
10Havel1307.00454107
3Nagayama1400.00314200
16Maduro1400.00414250
11Magee1400.00314300
15Dumas1450.00354350
4Quick-To-See1450.00104390
2Ngao1450.00414455
12Giljum1490.00324530
13Sedeghi1515.00334590
14Nguyen1525.00345575
5Ropeburn1550.00504050
1Velasquez2500.00502500

RANGE

range是逻辑行的范围,要经过计算的,一般range后面是数值或时间间隔等,这样根据当前行和range的表达式就能计算当前行对应的窗口范围,这个是逻辑范围,一眼看不出来,要经过range后的表达式和当前行的值计算才知道,这就是rows和range的区别。
如下:按salary排序,计算当前行到比当前行的salary最大大350结束的窗口的累加salary值,如红色7990=第1行到第9行最大为1100的行结束,第1行对应的窗口计算范围就有9行,因为第10行的salary是1200,那么1200-750=450>350。

select id,
       last_name,
       salary,
       dept_id,
       sum(salary) over(order by salary range between current row and 350 following) AS rslt
from s_emp;
IDLAST_NAMESALARYDEPT_IDRSLT
20Newman750.00437990
19Patel795.00427240
23Patel795.00347240
22Chang800.00445650
21Markarian850.00437250
24Dancs860.00456400
17Smith940.00416790
8Biri1100.004317007
25Schwartz1100.004517007
18Nozaki1200.004220887
6Urguhart1200.004120887
7Menchu1250.004218487
9Catchpole1300.004417237
10Havel1307.004515937
3Nagayama1400.003114630
16Maduro1400.004114630
11Magee1400.003114630
15Dumas1450.003510430
4Quick-To-See1450.001010430
2Ngao1450.004110430
12Giljum1490.00326080
13Sedeghi1515.00334590
14Nguyen1525.00343075
5Ropeburn1550.00501550
1Velasquez2500.00502500
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值