[20180917]關於分析函式的range與rows的區別.txt
[20180917]關於分析函式的range與rows的區別.txt
--//這幾天看文件<Oracle SQL Revealed.pdf>,主要想了解學習分析函式這方面內容.
--//遇到一個問題,P99.
SCOTT@test01p> @ ver1
PORT_STRING VERSION BANNER CON_ID
------------------------------ -------------- -------------------------------------------------------------------------------- ----------
IBMPC/WIN_NT64-9.1.0 12.1.0.1.0 Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 - 64bit Production 0
with t as
(select rownum id, column_value value from table(sys.odcinumberlist (1,2,3,4.5,4.6,7,10)))
select t.*,
last_value(value) over (order by value range between unbounded preceding and 1 preceding) l1,
last_value(value) over (order by value rows between unbounded preceding and 1 preceding) l2
from t;
ID VALUE L1 L2
---------- ---------- ---------- ----------
1 1
2 2 1 1
3 3 2 2
4 4.5 3 3
5 4.6 3 4.5
6 7 4.6 4.6
7 10 7 7
7 rows selected.
--//不明白為什麼id=5,L1輸出是3,L2輸出是4.5.
--//我仔細看了問題的關鍵理解視窗函式里面range與rows的含義.
--//看了一些文件,才明白,實際上還真不好理解.做一個筆記:
--//range是邏輯視窗,是指定當前行對應值的範圍取值(注意包括當前行),列數不固定,只要行值在範圍內,對應列都包含在內,注意
--//理解指的是取值範圍.對應欄位是value.
--//按照例子:range between unbounded preceding and 1 preceding, 對應id=5那行,values的取值範圍是1到4.6-1,實際上就是1到3.6
--//這樣id=5那行L1的輸出=3.
--//rows是物理視窗,即根據order by 子句排序後,取的前N行及後N行的資料計算(與當前行的值無關,只與排序後的行號相關),
--//對於rows between unbounded preceding and 1 preceding,對應id=5那行,行號就是1到4(注意這裡值行號),這樣L2的輸出就是5.
--//有點不好理解如果寫成如下:
with t as
(select rownum id, column_value value from table(sys.odcinumberlist (1,2,3,4.5,4.6,7,10)))
select t.*,
last_value(value) over (order by value range between unbounded preceding and current row) l1,
last_value(value) over (order by value rows between unbounded preceding and 1 preceding) l2
from t;
ID VALUE L1 L2
---------- ---------- ---------- ----------
1 1 1
2 2 2 1
3 3 3 2
4 4.5 4.5 3
5 4.6 4.6 4.5
6 7 7 4.6
7 10 10 7
7 rows selected.
--//如果1 preceding換成current row,value的取值範圍是1到4.6(id=5).
--//另外書中還介紹:
"partition by part" means that we apply an analytic function for each part independently. If it's omitted, then the
whole recordset is treated as one partition. Without an "order by" clause, window for each row covers all the rows for
the current partition so the result is the same for all rows. With an "order by" clause, window for each row covers all
rows from the beginning of the partition to the current row. This can be adjusted by specifying a windowing clause after
"order by" while the default behavior is "range between unbounded preceding and current row" (or simply "range unbounded
preceding") when "order by" is specified; otherwise it's "range between unbounded preceding and unbounded following."
--//裡面partition by ,order by 作用範圍預設從開始到當前行.
--//預設就是,這裡並不指rows:
--//預設是 range between unbounded preceding and current row
--//也可以寫 range unbounded preceding
--//range between unbounded preceding and unbounded following
--//隨手看了該書後面的一些例子好難,還是放棄....
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/267265/viewspace-2214337/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Python range與enumerate函式區別解析Python函式
- range與enumerate的區別
- 建構函式與普通函式的區別函式
- 箭頭函式與普通函式的區別函式
- TypeScript 中函式的理解?與 JavaScript 函式的區別?TypeScript函式JavaScript
- fill函式與memset函式的區別(c++)函式C++
- python內建函式-eval()函式與exec()函式的區別Python函式
- [20190401]關於semtimedop函式呼叫.txt函式
- DataTable.ImportRow()與DataTable.Rows.Add()的區別Import
- JavaScript:鉤子函式與回撥函式的區別JavaScript函式
- MySQL中的FOUND_ROWS()與ROW_COUNT()函式MySql函式
- [20190918]關於函式索引問題.txt函式索引
- 關於建構函式與解構函式的分享函式
- 箭頭函式與普通函式區別函式
- python函式與方法的區別總結Python函式
- JQuery的ready函式與JS的onload的區別jQuery函式JS
- [20211123]sqlplus @與@@的區別.txtSQL
- nginx關於root與alias的區別Nginx
- 關於C與C++的區別C++
- Android關於buildToolVersion與CompileSdkVersion的區別AndroidUICompile
- strcpy函式和memcpy函式的區別函式memcpy
- [20190402]關於semtimedop函式呼叫2.txt函式
- [20180628]expdp與rows=n.txt
- TransparentBlt、StretchBlt與BitBlt三個函式的區別函式
- 類别範本與函式模板的區別函式
- 函式宣告與函式表示式有什麼區別?函式
- # 普通函式和箭頭函式的區別函式
- 箭頭函式、簡寫函式、普通函式的區別函式
- StretchBlt函式和BitBlt函式的區別和用法函式
- Python range() 函式用法Python函式
- 關於count函式的理解函式
- 簡述箭頭函式和普通函式的區別函式
- 箭頭函式和普通函式的10個區別函式
- 迭代器與可迭代物件的區別,以及iter()函式的使用。物件函式
- python入門:range函式Python函式
- Python 關於TCP簡介以及與UDP的區別PythonTCPUDP
- Python中函式和方法的區別Python函式
- Day 59/100 箭頭函式和普通函式的區別函式