Most recently created row

We have an error_log table which will log errors after the execution of a stored proc.
We have a create_time field which stores the time of creation of the new record.
How can i view the most recently created row? Is the below mentioned query(courtesy of OTN) Ok? Or are there any other better options?
select err_msg from (select err_msg from err_log order by create_time desc)
where rownum=1 ;

That seems to be quite expensive
SQL> get t
  1  with t as (select 'test' err_msg, sysdate create_time from dual
  2             union all select 'test2' err_msg, sysdate -1 create_time from dual
  3             union all select 'test3' err_msg, sysdate -2 create_time from dual
  4             union all select 'test4' err_msg, sysdate -3 create_time from dual)
  5  select *
  6    from t, (select max(t.create_time) over(order by create_time desc) maxtime
  7               from t) tab
  8   where t.create_time = tab.maxtime
  9*  and rownum = ceil(abs(sin(42)))
SQL> /
ERR_M CREATE_TI MAXTIME
test  17-JAN-08 17-JAN-08
Execution Plan
   0      SELECT STATEMENT Optimizer=CHOOSE (Cost=14 Card=1 Bytes=22)
   1    0   TEMP TABLE TRANSFORMATION
   2    1     LOAD AS SELECT
   3    2       UNION-ALL
   4    3         FAST DUAL (Cost=2 Card=1)
   5    3         FAST DUAL (Cost=2 Card=1)
   6    3         FAST DUAL (Cost=2 Card=1)
   7    3         FAST DUAL (Cost=2 Card=1)
   8    1     COUNT (STOPKEY)
   9    8       HASH JOIN (Cost=6 Card=1 Bytes=22)
  10    9         VIEW (Cost=2 Card=4 Bytes=52)
  11   10           TABLE ACCESS (FULL) OF 'SYS_TEMP_0FD9D7BD6_7502F1D5' (TABLE (TEMP)) (Cost=2 Card=4 Bytes=52)
  12    9         VIEW (Cost=3 Card=4 Bytes=36)
  13   12           WINDOW (SORT) (Cost=3 Card=4 Bytes=36)
  14   13             VIEW (Cost=2 Card=4 Bytes=36)
  15   14               TABLE ACCESS (FULL) OF 'SYS_TEMP_0FD9D7BD6_7502F1D5' (TABLE (TEMP)) (Cost=2 Card=4 Bytes=52)
Statistics
          2  recursive calls
          8  db block gets
          8  consistent gets
          1  physical reads
        648  redo size
        683  bytes sent via SQL*Net to client
        660  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          1  sorts (memory)
          0  sorts (disk)
          1  rows processed
SQL>But if you code it a little bit trickier, it suddenly costs "nothing"
SQL> get t
  1  with t as (select 'test' err_msg, sysdate create_time from dual
  2             union all select 'test2' err_msg, sysdate -1 create_time from dual
  3             union all select 'test3' err_msg, sysdate -2 create_time from dual
  4             union all select 'test4' err_msg, sysdate -3 create_time from dual)
  5  select *
  6    from  (select err_msg, create_time, row_number() over(order by create_time desc) rn
  7               from t) tab
  8*  where rn = ceil(abs(sin(42)))
SQL> /
ERR_M CREATE_TI         RN
test  17-JAN-08          1
Execution Plan
   0      SELECT STATEMENT Optimizer=CHOOSE (Cost=9 Card=4 Bytes=104)
   1    0   VIEW (Cost=9 Card=4 Bytes=104)
   2    1     WINDOW (SORT PUSHED RANK) (Cost=9 Card=4 Bytes=52)
   3    2       VIEW (Cost=8 Card=4 Bytes=52)
   4    3         UNION-ALL
   5    4           FAST DUAL (Cost=2 Card=1)
   6    4           FAST DUAL (Cost=2 Card=1)
   7    4           FAST DUAL (Cost=2 Card=1)
   8    4           FAST DUAL (Cost=2 Card=1)
Statistics
          1  recursive calls
          0  db block gets
          0  consistent gets
          0  physical reads
          0  redo size
        673  bytes sent via SQL*Net to client
        660  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          1  sorts (memory)
          0  sorts (disk)
          1  rows processed
SQL>

Similar Messages

  • Most recently inserted row

    Hi,
    I have a table with a composite key on it's 4 columns(a,b,c,d). and i have another column called timestamp, which is used to identify when a record is inserted into the table.
    how to query which record is most recently inserted ?
    Any help is appriciated
    Thanks
    greddy

    or, this:
    SQL> select * from (select * from emp order by hiredate desc) where rownum = 1 ;
         EMPNO ENAME      JOB              MGR HIREDATE           SAL       COMM     DEPTNO
          7876 ADAMS      CLERK           7788 23-May-1987       1100                    20
    1 row selected.
    SQL>

  • Sync most recently created playlist in a playlist folder

    I have a playlist folder, and each playlist is a different week's Billboard chart. Each playlist is named by the date of the chart, ie "Country Chart 02-28-11." I'd like to have the most recent one sync automatically to my iPhone. Is there a trick to doing that?
    I thought I could create a smart playlist that would always be a copy of the most recent playlist, and sync that, but I haven't figured out how to do that. Any suggestions?

    Your iPhone was probably set to sync your old playlist but haven't for the newly created one.
    Plug your iPhone to your computer, select it in iTunes and then select the music tab.  In this tab, look at which playlist is selected to be sync with your phone.

  • Open Recent for V5.5 Not Showing Most Recent

    After upgrading to Pages 5.5, the Open Recent list of documents is buggy - sometimes after saving a document, the list of recent documents is updated and sometimes not. Before upgrading to Yosemite and version 5.5 of Pages, this feature worked.
    Clearing the menu has not resolved this issue. Changing the Recent Items number in System Preference has not resolved this issue.
    I tried to report this issue using the Apple feedback link, but the dropdown menu for version number only went up to V 5.2.
    Any suggestions? Anyone else having this issue?

    Any pattern? Are they the most recently created or modified? Anything in common about how they are named, or in the +dot plus three+ extensions? Are you still seeing the older version of files on the Mac compared to directly in Windows.
    I've not used Windows 7 so I'm not familiar with any sharing filters that are possible. If you are still seeing older versions of files, and the ones you don't see on the Mac happen to be the most recently added to the folder, it still feels to me like you are actually connecting to a different sharepoint than what you believe... maybe one that is in an backup archive or something.
    Sorry I can't be any more helpful. It's a bit mysterious.

  • Not Showing Most Recent Files

    I've got a strange one. I've been able to network my imac to a dell laptop and they can see each other's shared directories. Tried to access an Excel file from the laptop and it didn't show the most recent file, but one that was nearly three weeks old. Is the imac caching the files anywhere? I didn't see any windows settings. Still getting used to the Mac world and didn't see any ability to refresh. Any ideas would be appreciated. Thanks in advance.

    Any pattern? Are they the most recently created or modified? Anything in common about how they are named, or in the +dot plus three+ extensions? Are you still seeing the older version of files on the Mac compared to directly in Windows.
    I've not used Windows 7 so I'm not familiar with any sharing filters that are possible. If you are still seeing older versions of files, and the ones you don't see on the Mac happen to be the most recently added to the folder, it still feels to me like you are actually connecting to a different sharepoint than what you believe... maybe one that is in an backup archive or something.
    Sorry I can't be any more helpful. It's a bit mysterious.

  • "Recent Activity" - not always showing most recent replies

    I replied to this thread in the Essbase space: hammerora equivalent
    The 'Recent Activity' list for the space - Essbase - still shows the original question ('RobertAngel asked...') as the preview rather than my reply, although it does show '1 Reply'.
    Is there a reason that some threads show the most recent reply in the 'Recent Activity' listing and others do not?  Difficult to see quickly what's been going on in the Space.  Never noticed the behavior before, personally.
    Thanks!

    Any pattern? Are they the most recently created or modified? Anything in common about how they are named, or in the +dot plus three+ extensions? Are you still seeing the older version of files on the Mac compared to directly in Windows.
    I've not used Windows 7 so I'm not familiar with any sharing filters that are possible. If you are still seeing older versions of files, and the ones you don't see on the Mac happen to be the most recently added to the folder, it still feels to me like you are actually connecting to a different sharepoint than what you believe... maybe one that is in an backup archive or something.
    Sorry I can't be any more helpful. It's a bit mysterious.

  • How to return only the most recent row?

    Hi guys,
    I have a question which I am hoping is not too difficult but I just am not sure how to do it. I need to run a query on a table which may bring back multiple rows for my criteria, however I only want to bring back 1 of these multiple rows and the one I want to bring back has to be the most recent. I do have a date_created column on my table as well as a date_last_updated column. So basically using the select statement below as an example this query could return lets say 5 rows, I however only want it to return the most recent row and the most recent row is determined by the date_last_updated (which will only be populated if the record has been updated otherwise it is null) or the date_created fields.
    select * from my_table
    where model_id = 234
    Any help on this matter would be greatly appreciated.
    Thank you.

    Hi,
    you can do; -
    select *
    from
    (select *
    from my_table
    order by creation_date desc)
    where rownum = 1This does not guarantee that you will get the most recent in cases of records being created at exactly the same time, but if your table has a prime key then you could use this as the order by to get the same effect, though if say 5 records are inserted as a batch what does it really mean to be inserted last....
    regards,
    Robert.
    Edited by: Robert Angel on 09-Jul-2012 08:22 to add code tags.

  • Is there a way to create a smart folder in iTunes, that contains the most recent (say 10) used playlists?

    Is there a way to create a smart folder in iTunes, that contains the most recent (say 10) used playlists? By having this I would be three clicks (touches from what is recent).
    Thanks

    100s with about 30 folders, comprised on different moods, years, genre, favorites, audiobooks, podcasts. Being accessed from multiple Macs and Apple TVs.

  • Is there a way of displaying  the various item in my Downloads folder as icons arranged in order of Date Created, such that the most recent items appear at the bottom, rather than the top?

    Is there a way of displaying  the various item in my Downloads folder as icons arranged in order of Date Created, such that the most recent items appear at the bottom, rather than the top? I know this can be done when they are displayed as a list, but I want icons!

    Hi geezer,
    when you open your 'Downloads' folder choose 'View'  in the Menu bar and choose:
    'as List'
    'Show View Options'
    In 'View options' window you tick:
    'Always open in list view'
    'Date Modified', or
    'Date created'
    Now inside the 'Downloads' folder click on 'Date Created' (or 'Date Modified')
    column to get items alined from oldest to newest date (or reverse)…

  • SQL to compare 2 most recent rows in table

    Hi,
    I am trying to compare STATUS fields ( Most recent to Next Most recent )
    and select Most recent row if there STATUS values are different.
    SQL :
    select c.id,c.effdt,c.end_dt,c.status
    from table c ,table k
    where c.id = k.id
    and c.status <> k.status
    SAMPLE DATA
    ID EFFDT END_DT STATUS
    6 10/10/2006 12/31/9999 Y
    6 10/01/2006 10/09/2006 N
    6 09/02/2006 09/30/2006 Y
    EXPETING O/P
    6,10/10/2006 , 12/31/9999 , Y
    but with the above SQL i am getting 3 rows with same data .
    6,10/10/2006 , 12/31/9999 , Y
    6,10/10/2006 , 12/31/9999 , Y
    6,10/10/2006 , 12/31/9999 , Y
    is there a way to select only one row without using DISTINCT ?
    i can't use DISTINCT keyword because if sample data contains
    below data my SQL compares 1st row and 4th row since STATUS are not
    equal and returns results which we dont want . we just want
    to compare Most recent and next most recent ( i.e first and second rows
    only )
    SAMPLE DATA
    ID EFFDT END_DT STATUS
    6 10/10/2006 12/31/9999 Y
    6 10/01/2006 10/09/2006 Y
    6 09/02/2006 09/30/2006 Y
    6 08/01/2006 09/01/2006 N
    Best regards,
    Karru

    Try this
    SELECT *
    FROM
         (SELECT end_dt,
              status,
              lead(status) over(PARTITION BY id
            ORDER BY end_dt DESC) AS
           next_status
            FROM qry
            ORDER BY end_dt DESC)
    WHERE NVL(status,'Z') <> next_status
    AND rownum = 1

  • How do I get the numbers ipad app to plot a graph of dates against values, where the most recent date is on the right of the graph, but the most recent date is at the top rather than bottom of the rows in the table?

    How do I get the numbers ipad app to plot a graph of dates against values, where the most recent date is on the right of the graph, but the most recent date is at the top rather than bottom of the rows in the table?
    Also how can it be a line graph without plotting a circle at each value?
    Thanks this is very frustrating

    Make a copy of the table and produce your Line chart or Bar chart from the copy. Sort the copy into the order you want to see in your chart.
    Alternately you could use a Scatter Chart...
    Jerry

  • Selecting row by most recent date

    I'm attempting to select a row where the time ends in :00 and
    date is the most recent date or just the last record where time
    ends in :00, either will do.
    Once I have that record selected I need to query that query
    to pull in the previous 3 records which will be times ending in
    :15, :30 and :45 to round out that full hour of data for
    processing.
    It seems to me that this should be pretty easy but my CF
    books are not helping me here.
    Any Ideas?

    Two things I do to make this sort of thing easier are:
    1. Take baby steps. Start with one field and the date field.
    Once you get that to work, add one or two fields at at time.
    2. Comma control. This structure helps prevent extra commas
    from slipping into your query.
    select field1
    , field2
    , date
    from yourtable join (
    select field1 f1
    , field2 f2
    , max(datefield) maxdate)
    from yourtable
    where something
    group by field1
    , field2
    ) temp on field1 = f1 etc
    where clause

  • Remove the duplicate rows but keep the most recent and oldest records

    I have a Product table, some products have duplicate rows(same productID). I want to keep the most recent and the oldest records based on ETL load date (or min and max productKey), delete others. How to do this in a simple way?
    ProductKey | ProductID |ETL_Load_Date
    1001 | 501 | 2014-01-01
    1002 | 501 | 2014-01-02
    1003 | 501 | 2014-01-03
    1011 | 502 | 2014-01-01
    1012 | 502 | 2014-01-02
    1013 | 502 | 2014-01-03

    declare @table table (ProductKey int, ProductID int, ETL_Load_Date date)
    insert @table (ProductKey, ProductID, ETL_Load_Date)
    values
    ( 1001 , 501 , '2014-01-01' ), ( 1002 , 501 , '2014-01-02' ),
    ( 1003 , 501 , '2014-01-03' ), ( 1011 , 502 , '2014-01-01' ),
    ( 1012 , 502 , '2014-01-02' ), ( 1013 , 502 , '2014-01-03' )
    SELECT *
    FROM @table t
    INNER JOIN (
    SELECT MIN(etl_load_date) min_load_date, MAX(etl_load_date) max_load_date, ProductID
    FROM @table
    GROUP BY ProductID
    ) m
    ON t.ProductID = m.ProductID
    AND t.ETL_Load_Date IN (min_load_date,max_load_date)
    This will show both the min and max load dates, for each ProductID
    Thanks to Saeid for posting some test objects.

  • I just updated to to the most recent version of Pages and now cannot view or open any of my previous documents created before the update.  Where did they disappear to? Can I retrieve them?

    I just updated to the most recent version of Pages.  now I can't view or access  any of the documents created in the previous version.  Where did they go? How can I retrieve them?

    Hi cmccloud,
    What is the error message you get? If it's telling you to upgrade Numbers but you already have updated, then that means you probably are trying to open the document with the old version, which is still on your Mac, in a sub folder within Applications named iWork '09. Open the document with the Numbers that is in your Applications folder (not the one in a subfolder). It's easy to confuse the two, because the icons are similar.
    If you're getting some other error message, could you post specifics?
    SG

  • Highlighting the most recent date in a row

    hi all
    I have a series of 5 dates in a row. I would like to be able to highlight only the cell containing the most recent date (in respect to the current date) in each row.
    There will be instances when there is no date in some of the cells.
    Thanks for the help in the past, this and the other forums are awesome.

    Jason is correct. It requires two tables.
    I had started experimenting with this last night, but an outage at my ISP prevented posting at that time.
    Conditional formatting of a cell depends on comparing the value in that cell with a fixed value contained in the rule; the rule itself cannot reference a different cell to pick up the comparison value. So you need to calculate a value in a cell on a second table, apply conditional formatting to that cell, and place it directly behind the cell containing the date to be highlighted.
    Example:
    Dates are in cells B7 to F7 of Table 1
    Cell H1 is used to select the maximum date.
    Table 2 is a single row table with no row or column header cells, and only 5 columns.
    Select Table 1, open the Inspector, and use the Table inspector to set Cell Background to “None”
    In Cell H1 enter: =MAX(B7:F7)
    Go Insert > Table > Plain
    Resize the new table (Table 2) to 1 row x 5 columns.
    In A1 of Table 2, enter: =DATEDIF(Table 1::B7,Table 1::$H7,"D")
    Drag the handle at the lower right of the cell to copy the formula into all five cells.
    Click on A1.
    Open the Inspector and choose the Cell Format inspector.
    Check the Conditional Format checkbox, then click Show rules...
    Set the rule to “Equal to... 0”
    Choose a Fill colour to serve as your highlight colour.
    Click on the next cell and repeat.
    When you have applied the rule to all five cells, close the Conditional Format dialogue.
    Choose all five cells. Open the Text inspector.
    Click on the colour well.
    Leave the text colour set at Black, but move the Opacity slider to 0%.
    Close the Inspector and the Colors dialogue.
    Select Table 2 and drag it into position covering the five cells with dates on Table 1. Use the Arrow keys to nudge it into exact alignment.
    Go Arrange > Send Backward to move Table 2 behind Table 1. Repeat until you can see the dates on Table 1.
    Regards,
    Barry

Maybe you are looking for