Oracle学习笔记--range/rows

本文详细介绍了SQL中的Range和Rows窗口函数用法,通过实例展示了如何使用这些函数进行数据聚合及计算,特别关注了如何指定前后数据范围来实现不同的业务需求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Range表示数值范围,Rows表示行范围。
Range的语法:
over(order by xx range between n preceding and n following)
Range between必须为聚合函数/开窗函数服务,而且必须针对数值字段,因为表示的是一个数值范围,这个很容易和行范围rows混

SQL> select a.loan_typ, a.loan_no, a.deal_orig_prs_amt,
  2  sum(a.deal_orig_prs_amt) over (order by a.deal_orig_prs_amt range between 1 preceding and 1 following)
  3  from loan_dealer_info a;

LOAN_TYP LOAN_NO              DEAL_ORIG_PRS_AMT SUM(A.DEAL_ORIG_PRS_AMT)OVER(O
-------- -------------------- ----------------- ------------------------------
11383    2100001001                        0.00                              0
11221    2100003801                        0.00                              0
11350    2100001901                        0.00                              0
11388    2100005204                        0.00                              0
11121    2100005601                        0.00                              0
11388    2100005201                        0.00                              0
11388    2100004401                        0.00                              0
11388    2100005202                        0.00                              0
11121    2100016301                    10000.00                          80000
11122    2100014501                    10000.00                          80000
11151    2100013601                    10000.00                          80000
11121    2100012101                    10000.00                          80000
11121    2100013901                    10000.00                          80000
11222    2100007801                    10000.00                          80000
11202    2100002001                    10000.00                          80000
11101    210000032101                  10000.00                          80000
11101    2100000131                    15000.00                          30001
11388    2100042401                    15001.00                          30001
11121    2100020401                    20000.00                          60000
11141    2100014501                    20000.00                          60000

LOAN_TYP LOAN_NO              DEAL_ORIG_PRS_AMT SUM(A.DEAL_ORIG_PRS_AMT)OVER(O
-------- -------------------- ----------------- ------------------------------
11102    2100006401                    20000.00                          60000
11121    2100021201                    30000.00                          60000
11223    2100001801                    30000.00                          60000
11221    2100002001                    34567.00                          34567
11388    2100040801                    50000.00                         250000
11141    2100012901                    50000.00                         250000
11201    2100016301                    50000.00                         250000
11151    2100002901                    50000.00                         250000
11102    2100002201                    50000.00                         250000
11114    2100001901                   100000.00                         400001
11151    2100004501                   100000.00                         400001
11226    2100001201                   100000.00                         400001
11202    2100005401                   100001.00                         400001
11152    2100004301                   150000.00                         150000
11101    2100000214                   200000.00                         400000
11151    2100001101                   200000.00                         400000
11121    2100004801                   500000.00                         500000
11202    2100013701                            
11222    2100003601                            
11162    2100009201                            
11123    2100008601 
(后面省略)

142 rows selected
(原始表的记录数)

这个例子可以看到,这个查询会先按照DEAL_ORIG_PRS_AMT排序, 然后计算新字段,新字段是全表中所有行(包括本行)的 >= 本行的DEAL_ORIG_PRS_AMT - 1 AND <= 本行的DEAL_ORIG_PRS_AMT + 1 的DEAL_ORIG_PRS_AMT字段的合计,比如loan_no为2100016301的这行,DEAL_ORIG_PRS_AMT值为10000,全表中符合 >= 10000-1 AND <= 10000+1的为8条10000,所以新字段值为80000.
看loan_no 为2100000131和2100042401这2条记录,符合条件的刚好是这2行的值,所以新字段都是30001。
显然这个例子里设置范围为1显得很奇怪。

前后范围必须设置,如果不想要前面或后面的范围,可以把偏移量设为0,但不能为负数。 如果遇到null值,显然结果也是null。


如果前后的范围想要不设边界,即全部都要,可以用unbounded代替范围值。
但是一旦设置了边界,就会包含边界。

SQL> select a.loan_typ, a.loan_no, a.deal_orig_prs_amt,
  2  sum(a.deal_orig_prs_amt) over (order by a.deal_orig_prs_amt range between unbounded preceding and 10000 following)
  3  from loan_dealer_info a;

LOAN_TYP LOAN_NO              DEAL_ORIG_PRS_AMT SUM(A.DEAL_ORIG_PRS_AMT)OVER(O
-------- -------------------- ----------------- ------------------------------
11383    2100001001                        0.00                          80000
11221    2100003801                        0.00                          80000
11350    2100001901                        0.00                          80000
11388    2100005204                        0.00                          80000
11121    2100005601                        0.00                          80000
11388    2100005201                        0.00                          80000
11388    2100004401                        0.00                          80000
11388    2100005202                        0.00                          80000
11121    2100016301                    10000.00                         170001
11122    2100014501                    10000.00                         170001
11151    2100013601                    10000.00                         170001
11121    2100012101                    10000.00                         170001
11121    2100013901                    10000.00                         170001
11222    2100007801                    10000.00                         170001
11202    2100002001                    10000.00                         170001
11101    210000032101                  10000.00                         170001
11101    2100000131                    15000.00                         170001
11388    2100042401                    15001.00                         170001
11121    2100020401                    20000.00                         230001
11141    2100014501                    20000.00                         230001

Rows的语法:

over(order by xx rows between x preceding and y following)
每行对应的数据窗口是之前x行,之后y行,共包括x+1+y行

SQL> select a.loan_typ, a.loan_no, a.deal_orig_prs_amt,
  2  sum(a.deal_orig_prs_amt) over (order by a.deal_orig_prs_amt rows between 1 preceding and 2 following)
  3  from loan_dealer_info a;

LOAN_TYP LOAN_NO              DEAL_ORIG_PRS_AMT SUM(A.DEAL_ORIG_PRS_AMT)OVER(O
-------- -------------------- ----------------- ------------------------------
11383    2100001001                        0.00                              0
11221    2100003801                        0.00                              0
11350    2100001901                        0.00                              0
11388    2100005204                        0.00                              0
11121    2100005601                        0.00                              0
11388    2100005201                        0.00                              0
11388    2100004401                        0.00                          10000
11388    2100005202                        0.00                          20000
11121    2100016301                    10000.00                          30000
11122    2100014501                    10000.00                          40000
11151    2100013601                    10000.00                          40000
11121    2100012101                    10000.00                          40000
11121    2100013901                    10000.00                          40000
11222    2100007801                    10000.00                          40000
11202    2100002001                    10000.00                          45000
11101    210000032101                  10000.00                          50001
11101    2100000131                    15000.00                          60001
11388    2100042401                    15001.00                          70001
11121    2100020401                    20000.00                          75001
11141    2100014501                    20000.00                          90000
(后面省略)

142 rows selected
(原始表的记录数)

新字段为从前一条记录开始、到后2条记录为止(即包括4条记录)的DEAL_ORIG_PRS_AMT值的统计。

如果前后的范围想要不设边界,即全部都要,可以用unbounded代替范围值。

SQL> select a.loan_typ, a.loan_no, a.deal_orig_prs_amt,
  2  sum(a.deal_orig_prs_amt) over (order by a.deal_orig_prs_amt rows between unbounded preceding and 2 following)
  3  from loan_dealer_info a;

LOAN_TYP LOAN_NO              DEAL_ORIG_PRS_AMT SUM(A.DEAL_ORIG_PRS_AMT)OVER(O
-------- -------------------- ----------------- ------------------------------
11383    2100001001                        0.00                              0
11221    2100003801                        0.00                              0
11350    2100001901                        0.00                              0
11388    2100005204                        0.00                              0
11121    2100005601                        0.00                              0
11388    2100005201                        0.00                              0
11388    2100004401                        0.00                          10000
11388    2100005202                        0.00                          20000
11121    2100016301                    10000.00                          30000
11122    2100014501                    10000.00                          40000
11151    2100013601                    10000.00                          50000
11121    2100012101                    10000.00                          60000
11121    2100013901                    10000.00                          70000
11222    2100007801                    10000.00                          80000
11202    2100002001                    10000.00                          95000
11101    210000032101                  10000.00                         110001
11101    2100000131                    15000.00                         130001
11388    2100042401                    15001.00                         150001
11121    2100020401                    20000.00                         170001
11141    2100014501                    20000.00                         200001


0 preceding和0 following是和current row等价的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值