几乎都是官方文档上的内容。
 
 
[ 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. printf和std::cout ...endl

    printf效率要比std::cout...endl高些,可以减少打印所花时间

  2. getAttribute与getParameter的区别

    1.getParameter得到的是字符串,其取值源于jsp页面,从jsp页面中接受一个存在的参数,多用于servlet中,用于判断业务的类型和跳转页面.如: request.getParameter ...

  3. HDU 1298 T9(字典树+dfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1298 题意:模拟手机9键,给出每个单词的使用频率.现在给出按键的顺序,问每次按键后首字是什么(也就是要概率最大的 ...

  4. PHP 时间函数time、date和microtime的区别

    一.time.date 和 microtime函数 time----返回当前的 Unix 时间戳 date----格式化一个本地时间/日期 microtime----返回当前的 Unix 时间戳和微秒 ...

  5. ASP.NET开发总结

    ASP.NET的界面可以是.aspx,会对应有一个.aspx.cs的逻辑处理文件,.aspx的所有控件对应着变量,变量名就是控件的ID. 为了代码编写方便起见,一般将数据库表的新增字段,放在最后. 日 ...

  6. [原][osg][osgearth]倾斜摄影1.介绍

    总体介绍: 倾斜摄影就是将拍好的数据,三角网格化再附上贴图. 目前流行处理软件: Street Factory.PIX4DMapper.smart3D 后期开发平台:超图 Skyline smart3 ...

  7. ImageConverter引起的 invalid address or address of corrupt block 0xb7feab58 passed to dlfree

    虹软人脸识别,其方法要传NV21格式的byte[], github上有一个虹软的Demo,是不是虹软工作人员写的不清楚,这个Demo里bitmap转NV21格式byte[]用的是一个第三方库https ...

  8. 学习笔记29—Linux基础

    一.简单命令: 1.进入文件夹heyi目录:cd ./heyi 2.查看该目录下内容:ls 二.linux压缩和解压缩命令大全 tar命令 解包:tar zxvf FileName.tar 打包:ta ...

  9. python入门知识点(上)

    1.硬件系统: 主机部分: 1.中央处理器(CPU): 电脑的大脑 运算器: 数值计算和逻辑判断 控制器: 可以电脑中的各个部件协同工作 2.内部存储器: 随机存储器:内存条 使用电信号表示数据; 特 ...

  10. js判断手指的上滑,下滑,左滑,右滑,事件监听

    原理:1:当开始一个touchstart事件的时候,获取此刻手指的横坐标startX和staerY: 2:当触发touchmove事件的时候,再获取此时手指的横坐标moveEndX和纵坐标moveEn ...