幾乎都是官方文檔上的內容。
[ OFFSET offset { ROW | ROWS} ]
[ FETCH { FIRST | NEXT }[ { rowcount | percent PERCENT } ]
{ ROW| ROWS } { ONLY | WITH TIES } ]
row_limiting_clause
The row_limiting_clause allows you to limit therows returned by the query. You can
specify an offset, and number of rows or percentageof rows to return. You can use this
clause to implement top-N reporting. For consistentresults, specify the order_by_
clauseto ensure a deterministic sort order.
row_limiting_clause允許限制返回行的個數。
可以指定offset和行數(或者百分比)來返回行。
可以使用這個子句去實現top-N報表。
為保證一致性,需要指定order_by 子句以確定排列順序。
OFFSET
Use this clause to specify the number of rows toskip before row limiting begins.
offset must be a number. If you specify a negativenumber, then offsetis treated as
0. If you specify NULL, or a number greater than orequal to the number of rows
returned by the query, then 0 rows are returned. Ifoffsetincludes a fraction, then the
fractional portion is truncated. If you do notspecify this clause, then offsetis 0 and
row limiting begins with the first row.
使用這個子句可以指定跳躍多少行開始計數。
offset必須為一個數字。
如果指定一個附屬,那么會被當作0來處理。
如果指定為null,或者數字大於結果集的行數,就會返回0行。
如果offset是一個小數,那么小數點會被截取。
如果沒有offset子句,那么默認為0,從第一行開始計數。
ROW | ROWS
These keywords can be usedinterchangeably and are provided for
semantic clarity.
這些關鍵字使語義更加准確
FETCH
Use this clause to specify the number of rows orpercentage of rows to return. If you
do not specify this clause, then all rows arereturned, beginning at row offset+ 1.
使用這個子句去指定返回行的個數或者返回行的百分比。如果沒有指定,那么所有的行都會被返回,開始行為offset+1。
FIRST | NEXT
These keywords can be used interchangeably and areprovided for
semantic clarity.
這些關鍵字使語義更加准確
rowcount| percent PERCENT
Use rowcount to specify the number of rows toreturn.
rowcount must be a number. If you specify anegative number, then rowcountis
treated as 0. If rowcountis greater than the numberof rows available beginning at row
offset+ 1, then all available rows are returned. Ifrowcount includes a fraction, then
the fractional portion is truncated. If rowcountisNULL, then 0 rows are returned.
Use percent PERCENT to specify the percentage ofthe total number of selected rows to
return. percent must be a number. If you specify anegative number, then percentis
treated as 0. If percentis NULL, then 0 rows arereturned.
If you do not specify rowcountor percent PERCENT,then 1 row is returned.
使用rowcount去指定返回多少行。
rowcount必須為一個數字,如果指定了一個負數,那么rowcount會被當作0。如果rowcount大於以offset+1開始計數的所有行個數,那么所有的行都會被返回。
如果rowcount是一個小數,那么小數部分會被截斷。如果rowcount為null,那么返回0行。
使用percent去指定返回總行數的百分比。必須為一個數字。如果指定為負數,那么會被當作0。
如果為null,那么返回0行。(其實都是一個套路嘛)
ROW | ROWS
These keywords can be usedinterchangeably and are provided for
semantic clarity.
這些關鍵字使語義更加准確
ONLY | WITH TIES
Specify ONLYto return exactly the specified numberof rows or
percentage of rows.
指定only會返回明確的行數或者是百分比的行數。
Specify WITH TIES to return additional rows withthe same sort key as the last row
fetched. If you specify WITH TIES, then you mustspecify the order_by_clause. If you
do not specify the order_by_clause, then noadditional rows will be returned.
如果指定with ties子句,那么擁有和最后一行相同的排序鍵值的行都會被fetch。如果指定了with ties子句,那么必須指定order by 。如果沒有指定order by,那么不會有附加的行被返回。
Restrictions on the row_limiting_clause
This clause is subject to thefollowing
restrictions:
■ You cannot specify this clause with the for_update_clause.
■ If you specify this clause, then the select list cannot contain thesequence
pseudocolumns CURRVALor NEXTVAL.
■ Materialized views are not eligible for an incremental refreshif the defining query
contains the row_limiting_clause.
row_limiting_clause子句的限制:
無法指定for update子句
無法包含序列的偽列currentval或者nextval
如果定義的查詢語句中包含row_limiting_clause,那么無法在這之上創建增量刷新的
物化視圖。
下面為可能發生的錯誤舉例:
1、
SELECT employee_id, last_name
FROM employees ORDER BY employee_id OFFSET 5 ROWS FETCH first 5 ROWS ONLY for update ;
SELECT employee_id, last_name
* ERROR at line 1: ORA-02014: cannot select FOR UPDATE from view withDISTINCT, GROUP BY, etc.
2、
SELECT seq.currval,employee_id, last_name FROM employees ORDER BY employee_id OFFSET 5 ROWS FETCH NEXT 5 ROWS ONLY / SELECT seq.currval,employee_id, last_name * ERROR at line 1: ORA-02287: sequence number not allowed here
3、
CREATE MATERIALIZED VIEW LOG ON employees withprimary key ; Materialized view log created. CREATE MATERIALIZED VIEW mym REFRESH FAST AS ( SELECT employee_id, last_name FROM employees ORDER BY employee_id FETCH FIRST 5 ROWS ONLY ) / FETCH FIRST 5 ROWS ONLY * ERROR at line 6: ORA-12015: cannot create a fast refresh materializedview from a complex query
CREATE MATERIALIZED VIEW mym REFRESH FAST AS SELECT employee_id, last_name FROM employees ORDER BY employee_id / Materialized view created. CREATE MATERIALIZED VIEW mym as SELECT employee_id, last_name FROM employees ORDER BY employee_id FETCH FIRST 5 ROWS ONLY / FETCH FIRST 5 ROWS ONLY * ERROR at line 6: ORA-00933: SQL command not properly ended
記住加括號
CREATE MATERIALIZED VIEW mym as (SELECTemployee_id, last_name FROM employees ORDER BY employee_id FETCH FIRST 5 ROWS ONLY) /
Materialized view created.
默認是按需更新的物化視圖所以沒有什么問題。
先來幾個例子
Row Limiting: Examples
The following statement returns the 5 employeeswith the lowest employee_id values:
下面返回的是empid
最小的5
行。
SELECT employee_id, last_name FROM employees ORDER BY employee_id FETCH FIRST 5 ROWS ONLY;
這里將FIRST
換成NEXT
或者將ROWS
換成ROW
都沒有什么區別,但是擁有這些關鍵字是必須的。也證明了官檔上說的它們的作用是讓語義更加准確。
EMPLOYEE_ID LAST_NAME
----------- -------------------------
100 King
101 Kochhar
102 De Haan
103 Hunold
104 Ernst
下面返回的是跳過empid最小的5行的下5行數據。
SELECT employee_id, last_name FROM employees ORDER BY employee_id OFFSET 5 ROWS FETCH NEXT 5 ROWS ONLY;
offset
不提供percent
功能…
EMPLOYEE_ID LAST_NAME
----------- -------------------------
105 Austin
106 Pataballa
107 Lorentz
108 Greenberg
109 Faviet
下面返回的是薪水最小的5%
的數據。
SELECT employee_id, last_name FROM employees ORDER BY employee_id OFFSET 5 ROWS FETCH NEXT 5 ROWS ONLY;
EMPLOYEE_ID LAST_NAME SALARY
----------- ------------------------- ----------
132 Olson 2100
128 Markle 2200
136 Philtanker 2200
127 Landry 2400
135 Gee 2400
119 Colmenares 2500
使用了with ties
子句,下面的語句返回最小薪水的5%
的數據,附加和最后一行相同薪水的數據。
SELECT employee_id, last_name FROM employees ORDER BY employee_id OFFSET 5 ROWS FETCH NEXT 5 ROWS ONLY;
EMPLOYEE_ID LAST_NAME SALARY
----------- ------------------------- ----------
132 Olson 2100
128 Markle 2200
136 Philtanker 2200
127 Landry 2400
135 Gee 2400
119 Colmenares 2500
131 Marlow 2500
140 Patel 2500
144 Vargas 2500
182 Sullivan 2500
191 Perkins 2500
如果通過上面的例子還沒有完全搞懂,那么就看下面的官方文檔的翻譯吧。
Perform top-N queries by specifying an offset, andthe number of rows or
percentage of rows to return.
可以通過指定偏移量、需要返回的行數來實現top-n
查詢