How do I retrieve the latest record in Oracle?

To retrieve the latest records in an Oracle database, you can use one of the following methods:

  1. Order the records in descending order using the ORDER BY clause and the DESC keyword, then limit the results to only the first row using the FETCH FIRST ROW ONLY clause or the ROWNUM pseudocolumn. For example:
SELECT * FROM 表名 ORDER BY 日期列名 DESC FETCH FIRST ROW ONLY;

Alternatively

SELECT * FROM (SELECT * FROM 表名 ORDER BY 日期列名 DESC) WHERE ROWNUM = 1;
  1. Utilize the MAX function and a subquery to obtain the maximum value from the date column, and then join it with the original table to obtain the complete record. For example:
SELECT t1.* 
FROM 表名 t1
JOIN (SELECT MAX(日期列名) AS 最新日期 FROM 表名) t2
ON t1.日期列名 = t2.最新日期;

These methods can all be adjusted and optimized according to specific needs. Please choose the most suitable method based on the actual situation.

广告
Closing in 10 seconds
bannerAds