Oracle如何"row_limiting_clause" for rowcount|百分比 PERCENT 行在 Oracle 12c 中有效吗?
How Oracle "row_limiting_clause" for rowcount| percent PERCENT rows works in Oracle 12c?
平台:Windows 7 x-64
数据库:Oracle Database 12c 企业版 12.1.0.2.0 - 64 位。
Oracle如何row_limiting_clause
获取rowcount|百分比 PERCENT 行在 Oracle 12c 中有效吗?
Oracle row_limiting_clause
行为异常。
在下面的代码中,table "fetch_test" 有 10 行
案例一
当使用 row_limiting_clause
获取前 0.5 行时,我得到,
no rows selected
案例二
当仅对前 5% 的行使用 row_limiting_clause 子句时,我得到
1 row as output.
案例三
当仅对前 1% 的行使用 row_limiting_clause 子句时,我得到
1 row as output.
这意味着案例 1!=案例 2 [0.5!=0.5(10 行的 5%)]
SQL> select * from fetch_test;
COL1 COL2
---- ----
1 A
2 B
3 C
4 D
5 E
6 F
7 G
8 H
9 I
10 J
10 rows selected.
案例 1:
SQL> select col1 from fetch_test
2 fetch first 0.5 rows only;
no rows selected
案例 2:
SQL> select col1 from fetch_test
2 fetch first 5 percent rows only;
COL1
----
1
1 row selected
案例 3:
SQL> select col1 from fetch_test
2 fetch first 1 percent rows only;
COL1
----
1
1 row selected
Oracle 文档怎么说,
如果行计数包含小数,则小数部分将被截断。如果
行计数为 NULL,则返回 0 行
"source"
这解释了案例 1,但我在上面的示例中找不到案例 2 和案例 3 的可能解释。
任何帮助将不胜感激。
您的观察是正确的。严格阅读 Oracle 文档表明此行为是正确的:
rowcount 大小写指定截断分数。
没有提到百分比被截断了。因此,假设百分比没有被截断是合理的。
这似乎是合理的,因为小数行没有逻辑意义,但小数百分比确实有逻辑意义。
Use rowcount to specify the number of rows to return. rowcount must be a number or an expression that evaluates to a numeric value. If you specify a negative number, then rowcount is treated as 0. If rowcount is greater than the number of rows available beginning at row offset + 1, then all available rows are returned. If rowcount includes a fraction, then the fractional portion is truncated. If rowcount is NULL, then 0 rows are returned.
Use percent PERCENT to specify the percentage of the total number of selected rows to return. percent must be a number or an expression that evaluates to a numeric value. If you specify a negative number, then percent is treated as 0. If percent is NULL, then 0 rows are returned.
http://docs.oracle.com/database/121/SQLRF/statements_10002.htm
对于这组特定的数字,我们可以观察到 Oracle 将 10 的 5% 舍入为 1。
> select 10*.05, round(10*.05) from dual;
10*.05 round(10*.05)
------ -------------
0.5 1
更新,rishabi 笔记:
a remainder (when using PERCENT in the row limiter) will ALWAYS return a whole number rounded UP, as per http://community.oracle.com/message/13176927#13176927
平台:Windows 7 x-64 数据库:Oracle Database 12c 企业版 12.1.0.2.0 - 64 位。
Oracle如何row_limiting_clause
获取rowcount|百分比 PERCENT 行在 Oracle 12c 中有效吗?
Oracle row_limiting_clause
行为异常。
在下面的代码中,table "fetch_test" 有 10 行
案例一
当使用 row_limiting_clause
获取前 0.5 行时,我得到,
no rows selected
案例二 当仅对前 5% 的行使用 row_limiting_clause 子句时,我得到
1 row as output.
案例三 当仅对前 1% 的行使用 row_limiting_clause 子句时,我得到
1 row as output.
这意味着案例 1!=案例 2 [0.5!=0.5(10 行的 5%)]
SQL> select * from fetch_test;
COL1 COL2
---- ----
1 A
2 B
3 C
4 D
5 E
6 F
7 G
8 H
9 I
10 J
10 rows selected.
案例 1:
SQL> select col1 from fetch_test
2 fetch first 0.5 rows only;
no rows selected
案例 2:
SQL> select col1 from fetch_test
2 fetch first 5 percent rows only;
COL1
----
1
1 row selected
案例 3:
SQL> select col1 from fetch_test
2 fetch first 1 percent rows only;
COL1
----
1
1 row selected
Oracle 文档怎么说,
如果行计数包含小数,则小数部分将被截断。如果 行计数为 NULL,则返回 0 行 "source"
这解释了案例 1,但我在上面的示例中找不到案例 2 和案例 3 的可能解释。 任何帮助将不胜感激。
您的观察是正确的。严格阅读 Oracle 文档表明此行为是正确的:
rowcount 大小写指定截断分数。
没有提到百分比被截断了。因此,假设百分比没有被截断是合理的。
这似乎是合理的,因为小数行没有逻辑意义,但小数百分比确实有逻辑意义。
Use rowcount to specify the number of rows to return. rowcount must be a number or an expression that evaluates to a numeric value. If you specify a negative number, then rowcount is treated as 0. If rowcount is greater than the number of rows available beginning at row offset + 1, then all available rows are returned. If rowcount includes a fraction, then the fractional portion is truncated. If rowcount is NULL, then 0 rows are returned.
Use percent PERCENT to specify the percentage of the total number of selected rows to return. percent must be a number or an expression that evaluates to a numeric value. If you specify a negative number, then percent is treated as 0. If percent is NULL, then 0 rows are returned.
http://docs.oracle.com/database/121/SQLRF/statements_10002.htm
对于这组特定的数字,我们可以观察到 Oracle 将 10 的 5% 舍入为 1。
> select 10*.05, round(10*.05) from dual;
10*.05 round(10*.05)
------ -------------
0.5 1
更新,rishabi 笔记:
a remainder (when using PERCENT in the row limiter) will ALWAYS return a whole number rounded UP, as per http://community.oracle.com/message/13176927#13176927