我正在使用的软件要求获取有序数据集的第一条记录和最后一条记录。数据集按日期列排序。
我有的数据:
--table "notes":
-- ordered by this
-- |
-- V
note_id date_created attribute1 attribute2 ... -- I want to get
-----------------------------------------------------
596 2014/01/20 ... ... ... -- <- this
468 2014/02/28 ... ... ...
324 2014/03/01 ... ... ...
532 2014/04/08 ... ... ...
465 2014/05/31 ... ... ... -- <- and this
所需的输出:
596 2014/01/20 ... ... ...
465 2014/05/31 ... ... ...
您可以使用窗口功能:
select t.*
from (select t.*, row_number() over (order by date_created) as seqnum,
count(*) over () as cnt
from t
) t
where seqnum = 1 or seqnum = cnt;
在Oracle 12中,您还可以执行以下操作:
select t.*
from t
order by date_created
fetch first 1 rows only
union all
select t.*
from t
order by date_created desc
fetch first 1 rows only;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句