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

转载于:https://www.cnblogs.com/CandiceW/p/10030936.html

oracle 12c新特性 FETCH FIRST、WITH TIES 关键字详解相关推荐

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

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

  2. Oracle 12c新特性-多租户的维护管理

    云和恩墨技术专家 现就职于云和恩墨,为某省电信公司数据库运维服务:在IT行业拥有10年以上的工作经历.擅长 ORACLE 数据库运维管理.shell 脚本开发:长期服务于电信.金融,政府行业:具有丰富 ...

  3. 【云和恩墨大讲堂】高凯 | Oracle 12c 新特性-多租户的维护管理

    "云和恩墨大讲堂" 线上课程周四晚继续开讲.本期我们邀请的嘉宾是云和恩墨西北区技术专家 - 高凯,在这里跟大家分享一下 Oracle 12c 新特性方面的主题.课程以图文形式在微信 ...

  4. Oracle 12c新特性--LREG进程专门负责注册实例服务

    Oracle 12c引入了一些大量的新特性,数据库隐藏参数12.2比11.2就多了2000多个, 同时对之前版本的结构也发生了一些变化,虽然不如有些特性像temporary undo\immemory ...

  5. oracle 12c undo,Oracle 12c 新特性之临时Undo--temp_undo_enabled

    Oracle 12c 新特性之临时Undo--temp_undo_enabled 每个 Oracle 数据库包含一组与系统相关的表空间,例如 SYSTEM , SYSAUX , UNDO&TE ...

  6. Android M新特性Doze and App Standby模式详解

    转载请标明出处:http://blog.csdn.net/xx326664162/article/details/52312122 文章出自:薛瑄的博客 你也可以查看我的其他同类文章,也会让你有一定的 ...

  7. oracle12c 新增维护时间窗口,ORACLE 12C新特性-自动维护全局索引 | 信春哥,系统稳,闭眼上线不回滚!...

    今天在网上看了一篇关于12C新特性-自动维护全局索引的帖子,经测试,貌似根本不是那么回事呀.如果对分区表进行分区DDL操作,如果不加update index字句,全局索引就会失效,下面先看一下是11. ...

  8. oracle12c asmfd,Oracle 12c新特性--ASMFD(ASM Filter Driver)特性

    1 说明 ASMFD 是 12.1 中就引入的新特性,它可以不用手动配置 ASM 磁盘,更重要的是它可以保护磁盘被其他非 Oracle 操作复写,例如 dd , echo 等命令. 更为详尽的介绍,请 ...

  9. oracle中pdb,Oracle 12C新特性-CDB和PDB 详解

    最近看到好多人都在尝试Oracle中的12C新特性-容器数据库,今年3月Orcle推出了Release2版本,可以算是一个稳定版本了.下午着手尝试了一下,还是蛮不错得 1.前言 CDB与PDB是Ora ...

  10. oracle desc能看约束,ORACLE 12C新特性-DESC显示不可见字段 | 信春哥,系统稳,闭眼上线不回滚!...

    之前写过一篇关于12C新特性-不可见字段的文章,详见http://www.dbdream.com.cn/2014/01/19/oracle-12c%E6%96%B0%E7%89%B9%E6%80%A7 ...

最新文章

  1. jupyter notebook中执行命令报错No module named ‘wordcloud‘
  2. 遇到困难挫折也不要悲观:每个人生来就是冠军(转)
  3. kernel vim阅读 设置tags的标签
  4. kmeans聚类算法_聚类算法入门:k-means
  5. Optimize Slow VBA Code
  6. 何时才有Email发布功能
  7. PTA-习题11-2 查找星期 (15 分)-enum
  8. 2020HW必备-蜜罐如何在攻防演练中战术部防
  9. Nginx反向代理配置配置实例
  10. Mybatis(7)参数传递和结果封装
  11. java+selenium模拟登陆新浪微博demo
  12. HDOJ 2084 数塔 简单解题报告
  13. 光纤传输网的发展及其新的分层结构
  14. tplink软件升级有用吗_tplink路由器固件更新的方法
  15. python最大分词_北大开源了Python中文分词工具包,准确度远超Jieba
  16. BT源代码学习心得(四):种子文件的生成 -- 转贴自wolfenstein (NeverSayNever)
  17. html div全屏遮罩层,div遮罩层_Jquery全屏遮罩层DIV的实现代码
  18. 安装zsh 、omyzsh
  19. 机器学习预测结果评估展示_评估通用社区测试计划的性能并预测结果
  20. Android 开发技术干货

热门文章

  1. va_list 、va_start、 va_arg、 va_end 使用说明
  2. 5.无线射频基础知识介绍_无线射频基础介绍
  3. matlab等距偏置曲线,144 偏置曲线命令详解
  4. Mac软件推荐:Soulver原来这么好用
  5. ValueError: Cannot assign “<...>“: the current database router prevents this relation.
  6. 【clickhouse踩坑记录】clusters表中分片副本的浅析
  7. 在浏览器访问action中的方法(动态方法调用)
  8. 在线免费压缩pdf文件
  9. vs2015遇到找不到kernel32.lib,无法解析的外部符号 __imp__printf的问题
  10. 考研四六级英语引用名句