选择最后 12 行 oracle
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11848905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Select last 12 rows oracle
提问by Klaus Seidel
I want to do a select in oracle that returns more or less 300 rows, but I just want the 12 last registries. Here is the query:
我想在 oracle 中做一个返回或多或少 300 行的选择,但我只想要最后 12 个注册表。这是查询:
(SELECT *
FROM series
ORDER BY odata DESC) estatisticas
WHERE ponteiro = 50 AND lotus = 30
回答by Chris
Something along the lines of:
类似的东西:
select * from
( select estatisticas, rownum rn
(SELECT *
FROM series
ORDER BY odata DESC) estatisticas
WHERE ponteiro = 50 AND lotus = 30
order by odata asc) where rownum <=12
Edit: updated it for your query, you want to sort it opposite of the inner query, ascending in your case, so you can get the last 12
编辑:为您的查询更新了它,您希望将其与内部查询相反排序,在您的情况下升序,因此您可以获得最后 12 个
回答by Pablo
The Oracle RDBMS uses a pseudo-column called rownumwhen constructing the result set of a query. Each row in the result is numbered in ascending order, starting from 0. You can evaluate conditions as follows:
Oracle RDBMS在构造查询的结果集时使用名为rownum的伪列。结果中的每一行都按升序编号,从 0 开始。您可以按如下方式评估条件:
select job_name from dba_scheduler_jobs where rownum < 10;
This will return the first 10 rows it finds.
这将返回它找到的前 10 行。
It is important to remember that the rownum is evaluated after the records have been fetched from the database but before the order by clauses in the query.
重要的是要记住,rownum 是在从数据库中获取记录之后但在查询中的 order by 子句之前计算的。
so, your query should look as follows:
因此,您的查询应如下所示:
SELECT * FROM
((SELECT * FROM series
ORDER BY odata DESC) estatisticas
WHERE ponteiro = 50 AND lotus = 30 [ASC/DESC])
WHERE rownum < 12;
You should set the order by clause to ascending or descending, depending on which rows you want to get.
您应该将 order by 子句设置为升序或降序,具体取决于要获取的行。