How can I get just the first row in a result set AFTER ordering?

前端 未结 4 575
萌比男神i
萌比男神i 2021-01-04 00:38

This gives me just one row (the first one):

SELECT BLA
FROM BLA
WHERE BLA
AND ROWNUM < 2

However, I want the most recent date val; I can

相关标签:
4条回答
  • 2021-01-04 00:53

    An alternative way:

    SELECT ...
    FROM bla
    WHERE finalDate = (SELECT MAX(finalDate) FROM bla) AND
          rownum = 1
    
    0 讨论(0)
  • You can nest your queries:

    select * from (
        select bla
        from bla
        where bla
        order by finaldate desc
    )
    where rownum < 2
    
    0 讨论(0)
  • 2021-01-04 00:58

    In 12c, here's the new way:

    select bla
      from bla
     where bla
     order by finaldate desc
     fetch first 1 rows only; 
    

    How nice is that!

    0 讨论(0)
  • 2021-01-04 01:13

    This question is similar to How do I limit the number of rows returned by an Oracle query after ordering?.

    It talks about how to implement a MySQL limit on an oracle database which judging by your tags and post is what you are using.

    The relevant section is:

    select *
    from  
      ( select * 
      from emp 
      order by sal desc ) 
      where ROWNUM <= 5;
    
    0 讨论(0)
提交回复
热议问题