几乎都是官方文档上的内容。
 
 
[ 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查询
 

oracle 12c新特性 FETCH FIRST、WITH TIES 关键字详解的更多相关文章

  1. Oracle 12c新特性

    转载自:Oracle 12c新特性(For DBA) 一: Multitenant Architecture (12.1.0.1)      多租户架构是Oracle 12c(12.1)的新增重磅特性 ...

  2. Oracle 12C 新特性之扩展数据类型(extended data type)

    Oracle 12C 新特性-扩展数据类型,在12c中,与早期版本相比,诸如VARCHAR2, NAVARCHAR2以及 RAW这些数据类型的大小会从4K以及2K字节扩展至32K字节.只要可能,扩展字 ...

  3. oracle 12c 新特性之(相同字段上的多重索引、ddl 日志、限制PGA的大小、分页查询)

    1. 相同字段上的多重索引   在Oracle 12c R1之前,一个字段是无法以任何形式拥有多个索引的.或许有人会想知道为什么通常一个字段需要有多重索引,事实上需要多重索引的字段或字段集合是很多的. ...

  4. Oracle 12c 新特性之 数据库内归档(In-Database Archiving)

    Oracle Database 12c中引入了 In-Database Archiving的新特性, 该特性允许用户通过对表上的数据行标记为inactive不活跃的,以归档数据. 这些inactive ...

  5. ORACLE 12C新特性——CDB与PDB

    Oracle 12C引入了CDB与PDB的新特性,在ORACLE 12C数据库引入的多租用户环境(Multitenant Environment)中,允许一个数据库容器(CDB)承载多个可插拔数据库( ...

  6. Oracle 12C 新特性之 PDB热克隆(本地克隆、远端异机克隆)

    说明:版本12.2.0.1 12c r1版本中 clone 一份PDB源库需要打开在read only只读模式 , 在12c r2版本中引入了local undo mode, 源PDB在read/wr ...

  7. Oracle 12c新特性之——TABLE ACCESS BY INDEX ROWID BATCHED

    Oracle12c开始,我们在获取SQL语句的执行计划时,也会经常看到"TABLE ACCESS BY INDEX ROWID BATCHED"操作,那么,这个操作到底是什么意思呢 ...

  8. Oracle 12c新特性(For DBA)

    一: Multitenant Architecture (12.1.0.1)      多租户架构是Oracle 12c(12.1)的新增重磅特性,内建的多分租(Multi-tenancy),一个容器 ...

  9. Oracle 12C 新特性之 db默认字符集AL32UTF8、PDB支持不同字符集

    一. db默认字符集AL32UTF8Specify the database character set when you create the database. Starting from Ora ...

随机推荐

  1. Kubernetes之Controllers三

    StatefulSets StatefulSet is the workload API object used to manage stateful applications. Note: Stat ...

  2. communication

    Always consider the challenge as a chance. Basic principles: Know your audience. Know your purpose. ...

  3. Robot Framework+AutoItLibrary使用

    目的:用Robot Framework测试win7桌面程序   因为安装完了才补的记录,估计有错漏:(     步骤: 1. 尝试pip install AutoItLibrary 失败 2. 下载A ...

  4. windows 上让文件类型和程序关联的批处理程序。

    文件关联工具 地址: https://github.com/wll8/assoc-tool 本工具可以用来为你的便携程序添加文件关联,比如 nodepad2.exe . vscode 或其他图片处理程 ...

  5. Javascript 高级程序设计(第3版) - 第02章

    2017-05-10 更新原文: http://www.cnblogs.com/daysme 在 html 中使用 js 把js代码写在 <script type="text/java ...

  6. HTTPS 如何保证数据传输的安全性

    为什么需要 HTTPS? 我们知道 HTTP 是一个纯文本传输协议,对传输过程中的数据包不进行加密,是明文传输,那这样的话对于介于在发送端和接收端之间的任何 一个节点都能知道传输的内容,这些节点可能是 ...

  7. Qt5学习记录:QString与int值互相转换

    1)QString转int 直接调用toInt()函数 例: QString str("100"); int tmp = str.toInt(); 或者: bool ok; QSt ...

  8. 项目Alpha冲刺--5/10

    项目Alpha冲刺--5/10 1.团队信息 团队名称:基于云的胜利冲锋队 成员信息 队员学号 队员姓名 个人博客地址 备注 221500201 孙文慈 https://www.cnblogs.com ...

  9. table固定列的宽度,超出部分用…代替(针对普通table和antd)

    一. 实现思路 我们都知道让溢出内容变成...,只需要以下: overflow: hidden; text-overflow:ellipsis; white-space: nowrap; 表格里的内容 ...

  10. VMWare16安装windows7遇到的一些问题

    本人写这篇博客是为了记录了一些自己在使用VMware16安装Windows7时遇到的一些问题.本人使用的Windows7 ios镜像是小于4g的镜像. Windows7 ios的镜像地址为:https ...