Different ways to get the last 5 records ordered by date

I have a query no that returns the tickets ordered by moddt, modification date,
I was thinking of adding a select on top, to get the last 5 records of that list the select already generates. An other way was to use that select to create a view,
and get the 5 last records that way. I also though of a possibly impossible cursor with a sequence, but hmm well I consider that not a good solution.
So I'm trying to find a way, using a select, the topmost select which is not completed yet, to get the last 5 records. Maybe I should just try the rownumber thing again or something likewise.
Select * from(
select * from(
Select ticketid, appliecatiecd, categorieid, substr(titel,&,200)||' ...' "titel", klantproriteitid, interneprioriteitid,
(select g.voornaam||' '||g.naam
from gebruiker g
Where g.gebruikerid = t.gebruikerid
And t.applicatiecd = NVL(:P0_applicatiecd, :F101_applicatiecd))"aangemaakt door",
(select s.statusdefoms "status"
From status s
Where s.statusid = t.statusid
And t.applicatiecd = NVL(:P0_applicatiecd, :F101_applicatiecd)) "status",
Versieid,
Moddt,
Row_number() over (order by ticketid desc) rn
From ticket t
Where applicatiecd = NVL(:P0_applicatiecd, :F101_applicatiecd) )
Order by moddt)
Where --

Hi Floris,
You can also use RANK: http://www.dba-oracle.com/oracle_news/oracle_faq/faq_beg_sql_top_n_rows.htm
Regards Pete

Similar Messages

  • What is the best way to get the delta records using DSS(Data Syncronus Task)?

    This document may be helpful: Incremental Loading with Informatica Cloud. The screenshots are a little out-of-date, but the content should still be useful.

    DSS

  • How to get the last record from the database

    I am using MS Access database and Swings as GUI. I want to get the last record of a particular column from the table and store it as a varaible.

    Hi
    To get Last record of resultset, you have pass some parameter in constructor of CreateStatement.In such case Resultset should be scrollable and Readonly
    Example
    objStatement=objCon.createStatement ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
    mwwResultSet=cwwStatement.executeQuery(mwwSqlQuery);
    while(mwwResultSet.next())
    if(mwwResultSet.isLast())
    //Fetch the required column record.
    String abc=mwwResultSet.getString(1);
    I think this will work. Try it.
    bye

  • How to get the last record of an internall table ....

    Hi All..
    i want to get the last record of an internal table itab, and i want the the value of the last record.

    Hi,
         Use describe statment.
    data: lv_line type i.
        Describe table itab lines lv_line.
        read table itab into wa_itab index lv_line.
    regards,
    Santosh Thorat

  • How to get the last record??

    Hi, I now have to get only the last record from one table
    from MS Access.
    I was looking for the useful method from API, however,
    I cannot get it.
    How to get only the last record from one table ???
    Please help, thanks.

    In RDBMS, row order is really not relavent by iteself. Meaning there is no such thing as 'last record' unless it's in the context of a column (e.g. timeStamp).
    Of course, rows are inserted and stored in some natural order, but you cannot assume they'll come back the same way in a 'Select *' as they were entered.
    So, if you mean last row as in last inserted row, I would add a timestamp field that's set at insert, or use a autonumber column. Either way, have a column that will always contain the highest number or newest timestamp, and then you can build your where clause from that.
    For instance, in Access, I have an autonumbered field called 'fred', with other columns. If I want the last record, I simply use the following:
    select * from atable where fred in (SELECT max(fred) AS Max FROM atable);
    the 'where in' clause will filter to only those records in the subsequent select statment, which of course is only one, the max of the column fred.
    bRi

  • Getting the last record from the internal table

    When we use a READ statement it always picks up the first record which fulfill its condition but in my case I want to pick up the last record that fulfills it condition
    I have a internal table like
    id     type
    N1      A
    N1      T
    N1      A
    N1      6 ----> LAST RECORD
    my code is
    read table itab wIth key id = netobjid.
    for eg if netobjid = N1 , then I want to read the last record that corresponds to N1 ie ID N1 TYPE - 6...
    How to do that?

    HI
    actually i have done same requirement like this ...
    Take  one count variable into your internal table ..you pass the number of records into cont variable for every time  u enter the loop .
    Sort the internal table with count variable descending . ( AS we cant sort the internal table with sy-tabix)
    Then use read statement with sy-index = 1 .
    USe below logic ,............
    data  :  count  type i value '0'.
    LOOP at int .
    count = count + 1 .
    endloop.
    sort int count descending .
    read int  with  index = 1 .

  • Different ways to get the initial context within the container

    Hi there,
    Simple question concerning WLS61:
    Is there a difference between getting the context like this:
    Context ctx = T3Services.getT3Services().name().getInitialContext();
    and getting it like:
    Context ctx = new InitialContext();
    The only difference I see is that the second version is portable.
    Is there a performance difference?
    Thanks
    Juerg

    Hi Floris,
    You can also use RANK: http://www.dba-oracle.com/oracle_news/oracle_faq/faq_beg_sql_top_n_rows.htm
    Regards Pete

  • Get the todays record based on date filed

    DB Version:10g
    OS:windows 7
    Hi Guys good afternoon.
    The scenario is that i want to get the records of the employees who were hired today between 2pm to 10pm
    lets say based on hire-date field which is a date field of emp table.This is just an example.This is not the real table.
    Can anyone help me out please.
    Iam not posting the data as emp table is general table.
    Edited by: user123 on Jul 18, 2011 2:22 PM

    user123 wrote:
    Riaz Thank for the query but can you explain me what does 14/24 and 22/24 means?????In Oracle, unit of date airthmetics is day.
    Since 1 day = 24 hours, in order to add one hour to a given date, I'll need to add
    date + (1/24)In your case, since the hour portion has to be betweem 2 PM (i.e. 14 in 24 Hour format) and 10 PM (i.e. 22 in 24 Hour format), date has to incremeted by that many hours.
    So
    trunc(sysdate) +(14/24)is equal to SYSDATE 02:00:00 PM. Similarly
    trunc(sysdate) +(22/24)is SYSDATE 10:00:00 PM

  • Error on getting as list of Clients with the last sales order 90 days

    Hi Dear,
    SELECT T0.[CardCode], Max(T0.[DocDate]) as [Last Sales Date], Datediff(D, Max(T0.[DocDate]), GetDate()) as [Days Old], Sum(T0.[DocTotal]) FROM ORDR T0 WHERE Datediff(D, Max(T0.[DocDate]), GetDate())>90 GROUP BY T0.[CardCode]
    I am trying to create a list of the clients for whom the last sales order date is greater then 90 day. When I run the above query, I get the following error message:
    u201C1). [Microsoft][SQL Native Client][SQL Server]An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference.
    2). [Microsoft][SQL Native Cli u201C
    Can somebody tell me what I am doing wrong or is there any other way to get the last sales order date.
    Thanks.

    Hi,
    Check this :
    SELECT T0.CardCode, Max(T0.DocDate) as 'Last Sales Date', Datediff(D, Max(T0.DocDate),
    GetDate()) as [Days OLD], Sum(T0.DocTotal) FROM ORDR T0
    GROUP BY T0.CardCode
    HAVING Datediff(D, Max(T0.DocDate),GetDate())>90
    Kind Regards,
    Jitin
    SAP Business One Forum Team

  • Problem getting the last database record

    I need to get the last record of my database. The only way i found to do this is to use cursor::get with DB_LAST flag.
    But using it i don't always get the last record. For instance if the database contains 5000 records i am getting record number 4864 and so on.
    What am i doing wrong?

    The DB_LAST flag is the right one to use.
    Are you sure the keys sort in the order you expect? In particular, check for little endian integers. Here is some Python code that might be instructive:
    import struct
    sorted([(struct.pack("<i", i), i) for i in xrange(5000)])[-1][1]Regards,
    Michael Cahill, Oracle Berkeley DB.

  • How to get the last inserted record from a table ?

    :-) Hiee E'body
    I work on Oracle 8i and need to get the last
    record inserted in a table.
    I have tried using rownum and rowid pseudo-columns
    but that doesn't work.
    Can you please help me out ?
    :-) Have a nice time
    Vivek Kapoor.
    IT, Atul Ltd.,
    India.

    I'm not sure about 8i features.
    I assume here that you don't have 'Date-Time' stamp columns on the table which is the easiest way to determine the last inserted row in the table.
    If not try the following :-
    select address, piece, SQL_TEXT
    from V$SQLTEXT
    where upper(sql_text) like '%INSERT INTO TABLE_NAME%'
    Substiute the TABLE_NAME with the name of the actual table.
    Have fun.
    Regards,
    Shailender
    :-) Hiee E'body
    I work on Oracle 8i and need to get the last
    record inserted in a table.
    I have tried using rownum and rowid pseudo-columns
    but that doesn't work.
    Can you please help me out ?
    :-) Have a nice time
    Vivek Kapoor.
    IT, Atul Ltd.,
    India.

  • Get the last query from the current user

    Is there a way to get the last query of the current user, so every query could be log with a database trigger?
    Let's just say I execute:
    DELETE xxxx;
    I tried :
    SELECT T.SQL_TEXT FROM V$SQLAREA T where ADDRESS=(SELECT prev_sql_addr FROM v$session where audsid=userenv('sessionid'));
    But the result of this query is :
    'SELECT T.SQL_TEXT FROM V$SQLAREA T where ADDRESS=(SELECT prev_sql_addr FROM v$session where audsid=userenv('sessionid'))'
    Is there a way to execute a query that would return :
    'DELETE xxxx'
    Thanks

    You could join SQL_ADDR in v$session with ADDRESS in v$sqlarea to determine the SID that executed that SQL statement last. Note that PREV_SQL_ADDR in v$session will indicate the previous SQL he executed. Though you would have to look at these tables very often to get all SQL statements issued. One note here, I think if a different user ran the SAME SQL with just bind var differences the SQL_AREA will only show the last user’s information that executed it.
    BTW - it will show deletes also...

  • How to Get the last data dymanic

    in my bussiness scenarios,I have a cube store some data by month
    but some material have no records in some month
    I need to check if this month have records,get it
    or if there is no records, get the last records by month
    how can i do this?

    You check in R3...whether you have material data for that month...if yopu have data, in BW, run a repair full request by giving this criteria i.e. month criteria.
    Thanks...
    Shambhu

  • Selecting the last record from a database table

    In my ABAP Program, I have to use a select statement to retrieve the last record from the database table with the same key.  In other words, the Program will get more than one hit on the database table for the selected keys and I need to retrieve values from only the last record and not the first.  I know I can use an internal table to sort the records first and then retrieve the right value.   But to make things easier, is there a SELECT statement keyword than I can use to do this in one single step?  Thanks!

    hi,
    tables:mara.
        data: begin of it_mara occurs 0,
                matnr like mara-matnr,
                meins like mara-meins,
                mtart like mara-mtart,
                end of it_mara.
    select-options:s_matnr for mara-matnr.
    select matnr
              meins
              mtart
    from mara
    into table it_mara
    where matnr in s_matnr.
    if not it_mara[] is initial.
    sort it_mara by matnr descending.
    read table it_mara index 1.
    endif.
    then you get the last record of the select statement.
    reward points if useful,
    venkat.

  • Sequence get the last value (or store the .NEXTVAL anywhere)

    Hi there,
    I created some tables and one table references the other (foreign key).
    Now I want to add some test data and insert rows to both tables.
    My problem is:
    The "insert into command" for table a uses a sequence value to create the primary key. Now I need the assigned primary key in my second insert command to assign the right value to the foreign key.
    Is there a way to get the last assigned value from a sequence? Or can I store the key to some sort of variable and use this in both statements?
    Thx a lot
    Dominik

    Is there a way to get the last assigned value
    from a sequence?
    NEXTVAL return next available value in the sequence.
    If you want last returned value you need to use
    CURRVAL instead.
    >SELECT <your_sequence>.CURRVAL from dual;Peter D.Currval not work always, read the following example :
    Re: auto generating primary key
    Nicolas.

Maybe you are looking for