Help in selecting last n records

Hi All
I have the following tables in my database
CREATE TABLE MACHINE_M (
ID     VARCHAR(20)     NOT NULL,
NAME     VARCHAR(20)
CREATE TABLE STG_READING (
ID     VARCHAR(20)     NOT NULL,
MACHINE_ID     VARCHAR(20),
DATE_ID     NUMBER,
TIME_ID NUMBER,
READING NUMBER
CREATE TABLE READING_DWR (
ID     VARCHAR(20)     NOT NULL,
MACHINE_ID     VARCHAR(20),
DATE_ID     NUMBER,
TIME_ID NUMBER,
READING NUMBER
CREATE TABLE DATE_DIMENSION (
DATE_ID     NUMBER     NOT NULL,
DATE_VALUE     DATE     NOT NULL,
DAY     VARCHAR(10),
DAY_OF_WEEK     INTEGER,
DAY_OF_MONTH     INTEGER,
DAY_OF_YEAR     INTEGER,
PREVIOUS_DAY     DATE,
NEXT_DAY     DATE,
WEEK_OF_YEAR     INTEGER,
MONTH     VARCHAR(10),
MONTH_OF_YEAR     INTEGER,
QUARTER_OF_YEAR     INTEGER,
YEAR     INTEGER
CREATE TABLE TIME_DIMENSION (
TIME_ID     NUMBER     NOT NULL,
HOUR     VARCHAR(3),
MINUTE     VARCHAR(3),
SECOND     VARCHAR(3),
INTERVAL     NUMBER
Referential Constrains:-
     STG_READING(DATE_ID)>>>>>DATE_DIMENSION(DATE_ID)
     STG_READING(TIME_ID)>>>>>TIME_DIMENSION(TIME_ID)
     MACHINE_M(ID)>>>>>STG_READING(MACHINE_ID)
     READING(DATE_ID)>>>>>DATE_DIMENSION(DATE_ID)
     READING(TIME_ID)>>>>>TIME_DIMENSION(TIME_ID)
     MACHINE_M(ID)>>>>>READING(MACHINE_ID)
i will be getting the reading from the machine every 1min which i will be inserting into STG_READING table. the READING column must always be progressive.
so i have to validate the reading column from the staging with the last n records in the READING_DWR table.
selecting the last n records for a particular machine from READING_DWR table every time is causing performance bottleneck.
Can some one suggest me a way to improve the performance

Please consider the following when you post a question. This would help us help you better
1. New features keep coming in every oracle version so please provide Your Oracle DB Version to get the best possible answer.
You can use the following query and do a copy past of the output.
select * from v$version 2. This forum has a very good Search Feature. Please use that before posting your question. Because for most of the questions
that are asked the answer is already there.
3. We dont know your DB structure or How your Data is. So you need to let us know. The best way would be to give some sample data like this.
I have the following table called sales
with sales
as
      select 1 sales_id, 1 prod_id, 1001 inv_num, 120 qty from dual
      union all
      select 2 sales_id, 1 prod_id, 1002 inv_num, 25 qty from dual
select *
  from sales 4. Rather than telling what you want in words its more easier when you give your expected output.
For example in the above sales table, I want to know the total quantity and number of invoice for each product.
The output should look like this
Prod_id   sum_qty   count_inv
1         145       2 5. When ever you get an error message post the entire error message. With the Error Number, The message and the Line number.
6. Next thing is a very important thing to remember. Please post only well formatted code. Unformatted code is very hard to read.
Your code format gets lost when you post it in the Oracle Forum. So in order to preserve it you need to
use the {noformat}{noformat} tags.
The usage of the tag is like this.
<place your code here>\
7. If you are posting a *Performance Related Question*. Please read
   {thread:id=501834} and {thread:id=863295}.
   Following those guide will be very helpful.
8. Please keep in mind that this is a public forum. Here No question is URGENT.
   So use of words like *URGENT* or *ASAP* (As Soon As Possible) are considered to be rude.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

Similar Messages

  • Select last 50 records

    I have 230 records in a table.
    How to select last 50 records?

    <BLOCKQUOTE><font size="1" face="Verdana, Arial">quote:</font><HR>Originally posted by Asad:
    I have 230 records in a table.
    How to select last 50 records?<HR></BLOCKQUOTE>
    Logic : The last 50 records are those records who have less than or equal to 50 records below them.
    Select T1.sortcolumn, ...
    From TABLE T1
    Where 50 >= ( Select Count('x')
    From TABLE T2
    Where T1.sortcolumn >
    T2.sortcolumn
    null

  • Select last 50 records closed

    Looking for a simple sql select statement that will select the last 50 records closed in the database.
    I found a few helps out there that have an example like:
    SELECT RecordID, closed_date
    FROM ( SELECT RecordID, closed_date, RANK() OVER (ORDER BY closed_Date DESC) date_rank
    FROM table1 )
    WHERE date_rank <= 50;
    Only problem is, this is selecting something like 4000 records and many of them are not even closed.

    I believe you should retrieve the records that are only closed first.
    Try this,
    SELECT RecordID, closed_date
      FROM (SELECT RecordID,
                   closed_date,
                   ROW_NUMBER () OVER (ORDER BY closed_Date DESC) rn
              FROM table1
             WHERE closed_date < SYSDATE)  --Notice this.
    WHERE rn <= 50;G.

  • Select last ten records

    How can I retrieve the last 10 records in a table

    to get last 10 records
    Sim
    Message was edited by:
    sgalaxy

  • Help with selecting 10 random records from all records meeting report selection criteria in Crystal 11

    <p>I am trying to select ten random records from all that match the report selection criteria then report on each of these random records for QA/QI.  I have tried the RND function however it is giving me a random number rather than a random record selection.  I cannot figure this out and am despirately seeking assistance.</p><p>Thank you,</p><p>Amy</p>

    <p>I don&#39;t know of any Random record selection functions but maybe you could write your own custom function inside of a record selection to randomly filter the records.  You would use the Rand function we currently have to decide if a record was included in the report data or not.</p><p>Another possible option is to filter the records before they get to the report.  You can only do this if you are pushing the data into the report instead of having the report pull the data.  An example of this would be passing an ado recordset to a report at runtime. </p><p>Rob Horne</p><p>http://diamond.businessobjects.com/blog/10 </p>

  • Need help on selection of records in dynamically created table

    hi,
    Ia m able to bind the onleadSelect event to the dynamially created table .
    But i was not able to select the table records.
    default first record was only selected.
    can u help me out to find the way.
    i am pasting the code for reference.
    can u suggest me the right direction.
    i want the selected record in the table to be dispalyed in another view thru this action event.
    but only the forst record is now getting selected by default.
    here the code.
    IWDNode node =wdContext.currentContextElement().node().getChildNode(nodename,0);
    for (int l = 0; l < node.size(); l++) {
    int ele = node.getLeadSelection();
    IWDNodeElement element= node.getElementAt(ele);
    table.setOnLeadSelect(wdThis.wdGetObjectSelectedAction());

    Hi,
      <a href="/people/sap.user72/blog/2005/05/09/have-you-played-blindfold-chess's</a> how to create a dynamic table, though I think you have already done this.
      Do look at the comment that Armin Reichert has put in. Now in the source code that he has provided, the last line should be
    table.setOnLeadSelect(wdThis.wdGetLeadSelectAction());
    Have you done something similar to this?
    Have you set the initializeLeadSelect property of the datasource node to true?
    Regards,
    Satyajit.
    Message was edited by: Satyajit Chakraborty

  • Select the last saved records in a table

    I all
    I have this struct in a table
    create table INT_OBJ
    IDOBJ NUMBER(15) not null,
    IDERP NUMBER(15),
    IDE NUMBER(15),
    CPO VARCHAR2(20),
    VLR VARCHAR2(2500),
    VLRC VARCHAR2(2500)
    How to Select the last saved records in a table. I think usea a temp table for put the last row read and begin to read in the next row.
    The table is reading each four hours.

    Theoretically it can be done with ORA_ROWSCN and enabled ROWDEPENDENCIES, but there are so many nuances, so only adding column for ordering is right.
    Regards,
    Sayan Malakshinov

  • Retrive a record before the last inserted record

    Hi,
    I use simple select statement to retrive records where cola = 'Yes':
    select * from tbl1
    where cola = 'Yes';
    The result gives me about 10 records with different date.
    From the above result, I want to get the record right before the last inserted record according to the date field; which means I can not use MAX function (all I want just one record).
    If I write a function to return a date and passes it back to the SELECT statement, I'll get a date for each record, which I don't want.
    I know there's a way to do that in PL/SQL.
    Plese help me.
    pem

    select * from (
    select * from
    (select * from (select * from (table> order by <date> desc) where rownum < 3)
    order by <date>) where rownum =1

  • Delete all by last 10 records...

    I have a table where I only want to keep the last 10 records of the of the product.
    say for instance, I have a products table and it has apples, oranges... and another table that lists each sales transaction for each product.  As you can see.. you would get 1000's of records in the sales table.
    I want to delete all records in the sales table, except for the last 10 records for apples?
    any help?

    I have a table where I only want to keep the last 10 records of the of the product.
    TRUNCATE TABLE is faster than DELETE and uses fewer system and transaction log resources.
    Try
    -- code #1 v2
    IF Object_ID('tempdb..#LastSales','U') is not null DROP TABLE #LastSales;
    ;with
    SalesSeq as (
    SELECT *, Seq= row_number() over (partition by IDProd order by SalesDate desc)
      from Sales
    SELECT IDSales, IDProd, SalesDate, Qty
      into #LastSales
      from SalesSeq
      where Seq <= 10;
    TRUNCATE TABLE Sales;
    INSERT into Sales (IDSales, IDProd, SalesDate, Qty)
    SELECT IDSales, IDProd, SalesDate, Qty
    from #LastSales;
    IF Object_ID('tempdb..#LastSales','U') is not null DROP TABLE #LastSales;
    You cannot use TRUNCATE TABLE on tables that:
        Are referenced by a FOREIGN KEY constraint
        Participate in an indexed view
        Are published by using transactional replication or merge replication.
    To test:
    CREATE TABLE Products (IDProd int primary key, DescProd varchar(30));
    CREATE TABLE Sales (IDSales int primary key, IDProd int references Products, SalesDate smalldatetime, Qty smallint);
    INSERT into Products values
    (1, 'apple'), (2, 'orange');
    INSERT into Sales values
    (1, 1, '20120103', 18), (2, 1, '20120104', 22), (3, 1, '20120105', 50),
    (4, 1, '20120105', 19), (5, 2, '20120103', 5), (6, 1, '20120113', 5),
    (7, 1, '20120123', 20), (8, 1, '20120203', 7), (9, 1, '20120203', 20),
    (10, 1, '20120303', 21), (11, 1, '20120313', 2), (12, 1, '20120401', 15);
    José Diz     Belo Horizonte, MG - Brasil

  • Oracle - Last inserted record

    hello
    I need select last added/inserted records where value = 'test1' and = 'test2' and = 'test3' in column 'NAME'
    should be one record for each 'test1','test2' ...
    aslo..table have column 'DATE_USR' with data_type = timestamp(6)
    thanks for help
    Edited by: user8855885 on 2012-08-07 02:00
    Edited by: user8855885 on 2012-08-07 02:01
    Edited by: user8855885 on 2012-08-07 02:01

    /* Formatted on 8/7/2012 4:56:36 PM (QP5 v5.139.911.3011) */
    WITH t AS (SELECT 99 id,
                      'test1' name,
                      '12/08/03 11:38:43' dt,
                      'ok' DES
                 FROM DUAL
               UNION ALL
               SELECT 24,
                      'test2',
                      '12/07/01 11:38:43',
                      'nok'
                 FROM DUAL
               UNION ALL
               SELECT 97,
                      'test2',
                      '11/07/01 11:38:43',
                      'ok'
                 FROM DUAL
               UNION ALL
               SELECT 55,
                      'test2',
                      '11/07/01 11:38:43',
                      'nok'
                 FROM DUAL
               UNION ALL
               SELECT 43,
                      'test3',
                      '12/07/01 11:38:43',
                      'ok'
                 FROM DUAL
               UNION ALL
               SELECT 88,
                      'test3',
                      '10/07/01 11:38:43',
                      'nok'
                 FROM DUAL
               UNION ALL
               SELECT 34,
                      'test4',
                      '12/07/01 11:38:43',
                      'nok'
                 FROM DUAL
               UNION ALL
               SELECT 44,
                      'test4',
                      '09/07/01 11:38:43',
                      'nok'
                 FROM DUAL)
    SELECT *
      FROM (SELECT t.*,
                   RANK ()
                   OVER (PARTITION BY name
                         ORDER BY TO_DATE (dt, 'dd/mm/yy hh:mi:ss') DESC)
                      rk
              FROM t) x
    WHERE 1 = rkoutput:
    99     test1     12/08/03 11:38:43     ok     1
    24     test2     12/07/01 11:38:43     nok     1
    43     test3     12/07/01 11:38:43     ok     1
    34     test4     12/07/01 11:38:43     nok     1

  • Last added record key for UDO

    Hi,
    I create a UDO , how i get the last added record key after i add document ?
    Please Advise. Thanks

    Hi,
    Or try oCompany.GetNewObjectCode().
    Or if this dosent work try the following.... Before adding the record in the before action = True execute a simple query 
    select max(docentry)  from OINV
    And now store that in a variable and execute the same query after the record is added in the beforeaction = false and check if both are same, if not then u have a new record added.
    Hope it helps,
    Vasu Natari.

  • Last Detail Record

    I am not looking forward OnLastRecord but the last detail record before a group change. Specifically I am looking to format the Font for last time a running total prints a field before a group break resets it.
    1
    2
    3
    4
    1
    2
    3

    Hi,
    Here's how it can be done:
    Suppose we wish to change the Font style to Bold only for the last Running total field of the group then,
    Right-click the Running Total field and select Format Field > Font tab > Click on the X-2 icon beside Style and use this formula:
    if not({field_used_in_grouping} = next({field_used_in_grouping})) then
    crBold else
    crregular
    Hope this helps!
    -Abhilash

  • How can I select the random records in u201Cstep loopu201D.

    Hi, Experts,
    I am using step loop to display the data on screen now I want to select the random records from this step loop display. Like 1st 3rd or 5th record, is there any way to select the records,
    like I did in Table control there was a filed in internal table named marked char length 1, I gave this field name in u201Cw/ SelColumnu201D it fill that field with u2018Xu2019 from where I can get the info about the selected records, in this way I can easily perform the operation on that internal table with the help of that marked field.
    Is there any way to select the records in step loop too ?
    Kind Regards,
    Faisal

    thanks for replay shwetali,
    but i just gave you example of random records with 1st 3rd and 5th my goal is not select only these records. i want to select the random mean any records from the step loop display.
    like we can select from the table control. and when select any record it place 'X' in the given internal table field.
    Thanks and kind Regards,
    Faisal

  • URGENT: Selecting only 25 records at a time from a table

    URGENT !!!!
    Hi,
    Im having a RFC which selects records from a table (say table_A) and depending on these selected records, further processing is done within that RFC.
    Now my problem is, this table_A contains around 200 matching records. Due to this entire logical processing consumes lot of time. Hence my RFC is taking huge time to produce result. (apprx 10 mins).
    Can i select these matching records in batch of 25 and display result for these 25 records??
    I'll give this batch size as input to RFC?
    Do anybody have any idea about how to tackle this situation and reduce response time?
    If u hav any better solution than this then pls pls let me know ASAP..
    Regards,
    Amey

    Amey Mogare  ,
                             Do One thing , create  a new importing parameter in your RFC , say current_trans_id. NOw on the first call pass the initial value for current_trans_id.
    then inside the logic .. change the select to
    select upto 25 rows where trnascation id > current_trans_id.
    next time when u call teh rfc.. send the last selected id as a value for current_trans_id.
    i think you can some how use this logic
    Regards
    Sarath

  • How can I get the last / newest record of a table?

    Hi,
    I am developing an ecard app that has a create greeting page, a preview page with edit and send buttons and an edit page.
    I got he insert working, and on the preview page I created a master record page (displays all records), and delete all unecessary designs. Doing it this way also gives me the 'edit' button, so people are able to edit their page.
    How do I get only the last record displayed though? At the moment it loops through all records and displays them in sets of 10. I found the variable that holds the totla count of records ('$totalRows_rs7417_content1'), but how do I (re-)write the script so it ONLY diplays the last record?
    I need to get the ID of this record as I am writing this into the PARAMs of the object and embed tags of my message .swf (this picks up the ID and based on that sucks the greetings text out of the database via another php script.
    Also, from this page, how can I send an email to the sender (ie creator of the message) as well as the recipient? Both email addresses are in the databse so should be part of the erecord returned.
    Thanks,
    Nik

    -----
    OK, So how do I recreate what I got in a non-ADDT list?
    well, when you´re just about to display one certain record, the term "list" is somewhat inappropriate anyway.
    What you´d simply need to do IMHO, is using DW´s native functions to create the basic "SELECT * FROM table_name ORDEr BY ID DESC" query, add "LIMIT 1" manually, and display the desired "dynamic text" placeholders in 1 table cell -- don´t think there were any need for all the fancy stuff
    (sorting, filtering etc) provided by ADDT´s list, as it´s just 1 record.
    It also occurred to me just now that finding the last record that has been added *may-* notbe enough if the site gets used a lot by our executives (I am thinking of a situation where 2 or three peolpe create greetings at the same time and press submit and then getting each others messges rather than their own).
    in this case you might want to additionally store the respective executive´s "kt_login_id" session variable -- because it´s this unique "identifier" which should be used to show the last inserted record of the person who actually inserted it, means adding a "WHERE submitter_id" equals the session variable kt_login_id - clause to the query.
    When creating a non-ADDT list that´s at some point based on detecting a user session, you´ll BTW need to insert at line 1
    I can't seemt o insert it on the page as it is looking for a insert, update or delete bahaviour on the page which of course I don't have
    please search these forums for several posts I made which explain my "use dummy table" approach that´s made for cases when you actually don´t have anything to insert/update/delete, but need "something" for ADDT´s send email behaviours.
    Cheers,
    Günter Schenk
    Adobe Community Expert, Dreamweaver

Maybe you are looking for