--按salary排序,计算当前行到下面两行的salary累加值,红色部分为例,可以看到2340等于3个salary的值,物理行范围,一眼看就看出来了 sql> select id,last_name,salary,dept_id, 2sum(salary) over(order by salary 3 rows between current row and 2 following) 4from s_emp; ID LAST_NAME SALARYDEPT_ID SUM(SALARY)OVER(ORDERBYSALARYR -------- ------------------------- ------------- -------- ------------------------------ 20 Newman 750.00 43 2340 19 Patel 795.00 42 2390 23 Patel 795.00 34 2445 22 Chang 800.00 44 2510 21 Markarian 850.00 43 2650 24 Dancs 860.00 45 2900 17 Smith 940.00 41 3140 8 Biri 1100.00 43 3400 25 Schwartz 1100.00 45 3500 18 Nozaki 1200.00 42 3650 6 Urguhart 1200.00 41 3750 7 Menchu 1250.00 42 3857 9 Catchpole 1300.00 44 4007 10 Havel 1307.00 45 4107 3 Nagayama 1400.00 31 4200 16 Maduro 1400.00 41 4250 11 Magee 1400.00 31 4300 15 Dumas 1450.00 35 4350 4 Quick-To-See 1450.00 10 4390 2 Ngao 1450.00 41 4455 12 Giljum 1490.00 32 4530 13 Sedeghi 1515.00 33 4590 14 Nguyen 1525.00 34 5575 5 Ropeburn 1550.00 50 4050 1 Velasquez 2500.00 50 2500 25 rows selected sql> --range是逻辑行的范围,要经过计算的,一般range后面是数值或时间间隔等,这样根据当前行和range的表达式就能计算当前行对应的窗口范围,这个是逻辑范围,一眼 --看不出来,要经过range后的表达式和当前行的值计算才知道,这就是rows和range的区别。 --如下:按salary排序,计算当前行到比当前行的salary最大大350结束的窗口的累加salary值,如红色7990=第1行到第9行最大为1100的行结束,第1行对应的窗口计算范围就有9行,因为第10行的salary是1200,那么1200-750=450>350。 sql> select id, 2sum(salary) over(order by salary 3 range between current row and 350 following) 4from s_emp; ID LAST_NAME SALARYDEPT_ID SUM(SALARY)OVER(ORDERBYSALARYR -------- ------------------------- ------------- -------- ------------------------------ 20 Newman 750.00 43 7990 19 Patel 795.00 42 7240 23 Patel 795.00 34 7240 22 Chang 800.00 44 5650 21 Markarian 850.00 43 7250 24 Dancs 860.00 45 6400 17 Smith 940.00 41 6790 8 Biri 1100.00 43 17007 25 Schwartz 1100.00 45 17007 18 Nozaki 1200.00 42 20887 6 Urguhart 1200.00 41 20887 7 Menchu 1250.00 42 18487 9 Catchpole 1300.00 44 17237 10 Havel 1307.00 45 15937 3 Nagayama 1400.00 31 14630 16 Maduro 1400.00 41 14630 11 Magee 1400.00 31 14630 15 Dumas 1450.00 35 10430 4 Quick-To-See 1450.00 10 10430 2 Ngao 1450.00 41 10430 12 Giljum 1490.00 32 6080 13 Sedeghi 1515.00 33 4590 14 Nguyen 1525.00 34 3075 5 Ropeburn 1550.00 50 1550 1 Velasquez 2500.00 50 2500 25 rows selected |