[英]oracle sql wih rownum <=
why below query is not giving results if I remove the <
sign from query.Because even without <
it must match with results? 如果我从查询中删除
<
符号,为什么下面的查询没有给出结果。即使没有<
它也必须与结果匹配?
Query used to get second max id value: 用于获取第二个最大id值的查询:
select min(id)
from(
select distinct id
from student
order by id desc
)
where rownum <=2
student id
1
2
3
4
Rownum has a special meaning in Oracle. Rownum在Oracle中具有特殊含义。 It is increased with every row, but the optimizer knows that is increasing continuously and all consecutive rows must met the rownum condition.
它随着每一行的增加而增加,但是优化器知道它在不断增加,并且所有连续的行都必须满足rownum条件。 So if you specify
rownum = 2
it will never occur since the first row is already rejected. 因此,如果您指定
rownum = 2
,则由于第一行已被拒绝,因此它将永远不会发生。
You can see this very nice if you do an explain plan on your query. 如果您对查询做一个解释计划,您会发现这很好。 It will show something like:
它将显示如下内容:
Plan for rownum <=
: 计划
rownum <=
:
COUNT STOPKEY
Plan for rownum =
: 计划
rownum =
:
FILTER
A ROWNUM value is not assigned permanently to a row (this is a common misconception). ROWNUM值不会永久分配给行(这是常见的误解)。 A row in a table does not have a number;
表格中的行没有数字; you cannot ask for row 2 or 3 from a table click Here for more Info.
您无法从表格中询问第2行或第3行,请点击此处获取更多信息。
This is from the link provided: 这来自提供的链接:
Also confusing to many people is when a ROWNUM value is actually assigned. 当实际分配了ROWNUM值时,许多人也感到困惑。 A ROWNUM value is assigned to a row after it passes the predicate phase of the query but before the query does any sorting or aggregation.
ROWNUM值在经过查询的谓词阶段之后但在查询进行任何排序或聚合之前被分配给一行。 Also, a ROWNUM value is incremented only after it is assigned, which is why the following query will never return a row:
另外,仅在分配ROWNUM值后才递增它,这就是为什么以下查询永远不会返回行的原因:
select *
from t
where ROWNUM > 1;
Because ROWNUM > 1 is not true for the first row, ROWNUM does not advance to 2. Hence, no ROWNUM value ever gets to be greater than 1. Consider a query with this structure: 因为第一行的ROWNUM> 1不正确,所以ROWNUM不会前进到2。因此,任何ROWNUM值都不会大于1。考虑具有以下结构的查询:
select ..., ROWNUM
from t
where <where clause>
group by <columns>
having <having clause>
order by <columns>;
I think this is the query you are looking for: 我认为这是您要查找的查询:
select id
from (select distinct id
from student
order by id desc
) t
where rownum <= 2;
Oracle processes the rownum
before the order by
, so you need a subquery to get the first two rows. Oracle在
order by
之前处理rownum
,因此您需要一个子查询来获取前两行。 The min()
was forcing an aggregation that returned only one result, but before the rownum
was applied. min()
强制执行仅返回一个结果但在应用rownum
之前的聚合。
If you actually want only the second value, you need an additional layer of subqueries: 如果实际上只需要第二个值,则需要一个附加的子查询层:
select min(id)
from (select id
from (select distinct id
from student
order by id desc
) t
where rownum <= 2
) t;
However, I would do: 但是,我会这样做:
select id
from (select id, dense_rank() over (order by id) as seqnum
from student
) t
where seqnum = 2;
Why not just use 为什么不只是使用
select id
from ( select distinct id
, row_number() over (order by id desc) x
from student
)
where x = 2
Or even really bad. 甚至真的很糟糕。 Getting the count and index :)
获取计数和索引:)
select id
from ( select id
, row_number() over (order by id desc) idx
, sum(1) over (order by null) cnt
from student
group
by id
)
where idx = cnt - 1 -- get the pre-last
Or 要么
where idx = cnt - 2 -- get the 2nd-last
Or 要么
where idx = 3 -- get the 3rd
订购asc而不是desc
select id from student where rownum <=2 order by id asc;
Try this 尝试这个
SELECT *
FROM (
SELECT id, row_number() over (order by id asc) row_num
FROM student
) AS T
WHERE row_num = 2 -- or 3 ... n
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.