如何在Oracle中选择前1条记录,这个问题有一个明确的答案(参见链接):
select * from table_name where rownum = 1
如何按日期降序排列:
select * from table_name order by trans_date desc
但它们不能一起工作(rownum不是根据trans_date生成的):
... where rownum = 1 order by trans_date desc
问题是如何选择最新的一项并按日期排序?
如何在Oracle中选择前1条记录,这个问题有一个明确的答案(参见链接):
select * from table_name where rownum = 1
如何按日期降序排列:
select * from table_name order by trans_date desc
但它们不能一起工作(rownum不是根据trans_date生成的):
... where rownum = 1 order by trans_date desc
问题是如何选择最新的一项并按日期排序?
... where rownum = 1 order by trans_date desc
这个语句任选一条记录 (where rownum = 1),然后对这个记录排序 (order by trans_date desc)。
正如Ivan所示,您可以使用子查询,在其中对记录进行排序,并在外部查询中保留第一条记录 where rownum = 1。然而,这种方法极其针对Oracle,违反了SQL标准,即子查询结果被视为无序(即DBMS可以忽略 order by 子句)。
因此还是采用标准解决方案。从Oracle 12c开始:
select *
from table_name
order by trans_date desc
fetch first 1 row only;
在旧版本中:
select *
from
(
select t.*, row_number() over (order by trans_date desc) as rn
from table_name t
)
where rn = 1;
现代Oracle版本有FETCH FIRST:
select * from table_name order by trans_date desc
fetch first 1 row only
应该使用子查询来使组合 rownum & order 能够工作:
select * from (select * from table_name order by trans_date desc) AS tb where rownum = 1
select t.*
from (
select *,
min(trans_date) over () as min_date,
max(trans_date) over () as max_date
from the_table
) t
where trans_date = min_date
or trans_date = max_date;
另一个选择是在派生表上进行连接
select t1.*
from the_table
join (
select min(trans_date) over () as min_date,
max(trans_date) over () as max_date
from the_table
) t2 on t1.trans_date = t2.min_date
or t1.trans_date = t2.max_date;
不确定哪个更快,您需要检查执行计划