Andrii Havrylyak
Andrii Havrylyak

Reputation: 675

Last entry in the table oracle

I want to create a query in an oracle that will show the last entry in the table that I made, I tried through max and through date, but after max, query is not accurate, and due date shows all dates, I will be grateful for your help)

after the date

select seal_number 
  from SEC_OBJECT_SEALING 
 where SEALING_OBJECT = :P26_OBJECT_UNPLUG 
   and (to_date(data ,'dd.mm.yyyy hh24:mi:ss') = 
       (select max(to_date(data ,'dd.mm.yyyy hh24:mi:ss')) from SEC_OBJECT_SEALING)

through max

select seal_number 
  from SEC_OBJECT_SEALING 
 where SEALING_OBJECT = :P26_OBJECT_UNPLUG 
   and id = (select max(id) from SEC_OBJECT_SEALING)

Upvotes: 0

Views: 53

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1271003

In Oracle 12+, use fetch:

select seal_number
from SEC_OBJECT_SEALING
where SEALING_OBJECT = :P26_OBJECT_UNPLUG 
order by data desc
fetch first 1 row only;

In earlier versions, use a subquery:

select sos.*
from (select seal_number
      from SEC_OBJECT_SEALING
      where SEALING_OBJECT = :P26_OBJECT_UNPLUG 
      order by data desc
     ) sos
where rownum = 1;

Upvotes: 2

Related Questions