简体   繁体   English

Oracle SQL最后n条记录

[英]Oracle SQL last n records

i have read tons of articles regarding last n records in Oracle SQL by using rownum functionality, but on my case it does not give me the correct rows. 我已经通过使用rownum功能阅读了有关Oracle SQL中最后n条记录的大量文章,但就我而言,它没有给我正确的行。

I have 3 columns in my table: 1) message (varchar), mes_date (date) and mes_time (varchar2). 我的表中有3列:1)消息(varchar),mes_date(日期)和mes_time(varchar2)。

Inside lets say there is 3 records: 内部可以说有3条记录:

Hello world | 20-OCT-14 | 23:50
World Hello | 21-OCT-14 | 02:32
Hello Hello | 20-OCT-14 | 23:52

I want to get the last 2 records ordered by its date and time (first row the oldest, and second the newest date/time) 我想要按日期和时间排序的最后2条记录(第一行是最早的,第二行是最新的日期/时间)

i am using this query: 我正在使用此查询:

SELECT * 
  FROM (SELECT message 
          FROM messages 
         ORDER 
            BY MES_DATE, MES_TIME DESC
       ) 
  WHERE ROWNUM <= 2 ORDER BY ROWNUM DESC;

Instead of getting row #3 as first and as second row #2 i get row #1 and then row #3 而不是将第3行作为第一,第二行作为第二行,而是获取第1行,然后是第3行

What should i do to get the older dates/times on top follow by the newest? 我该怎么做才能使最旧的日期/时间排在最前面,并紧随其后?

Maybe that helps: 也许有帮助:

SELECT * 
  FROM (SELECT message,
               mes_date,
               mes_time,
               ROW_NUMBER() OVER (ORDER BY TO_DATE(TO_CHAR(mes_date, 'YYYY-MM-DD') || mes_time, 'YYYY-MM-DD HH24:MI') DESC) rank
          FROM messages 
       ) 
  WHERE rank <= 2
  ORDER 
     BY rank

I am really sorry to disappoint - but in Oracle there's no such thing as "the last two records". 令人失望,我真的很抱歉-但是在Oracle中,没有“最后两个记录”之类的东西。

The table structure does not allocate data at the end, and does not keep a visible property of time (the only time being held is for the sole purpose of "flashback queries" - supplying results as of point in time, such as the time the query started...). 表结构不会在最后分配数据,也不会保留可见的时间属性(唯一保留的时间仅是“闪回查询”的目的-提供时间点的结果,例如时间)。查询已开始...)。

The last inserted record is not something you can query using the database. 您不能使用数据库查询最后插入的记录。

What can you do? 你能做什么? You can create a trigger that orders the inserted records using a sequence, and select based on it (so SELECT * from (SELECT * FROM table ORDER BY seq DESC) where rownum < 3 ) - that will assure order only if the sequence CACHE value is 1. 您可以创建一个触发器来使用序列对插入的记录进行排序,然后基于该触发器进行选择(因此, SELECT * from (SELECT * FROM table ORDER BY seq DESC) where rownum < 3 )-仅当序列CACHE值确定时,才可以保证顺序是1。

Notice that if the column that contains the message date does not have many events in a second, you can use that column, as the other solution suggested - eg if you have more than 2 events that arrive in a second, the query above will give you random two records, and not the actual last two. 请注意,如果包含消息日期的列在一秒钟内没有太多事件,则可以使用该列,如其他解决方案所建议的-例如,如果在一秒钟内有两个以上事件,则上面的查询将给出您会随机记录两条记录,而不是实际的最后两条记录。

AGAIN - Oracle will not be queryable for the last two rows inserted since its data structure do not managed orders of inserts, and the ordering you see when running "SELECT *" is independent of the actual inserts in some specific cases. 再次-Oracle将无法查询插入的最后两行,因为Oracle的数据结构不管理插入的顺序,在某些特定情况下,运行“ SELECT *”时看到的顺序与实际的插入无关。

If you have any questions regarding any part of this answer - post it down here, and I'll focus on explaining it in more depth. 如果您对此答案的任何部分有任何疑问,请在此处将其发布下来,我将专注于更深入地解释它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM