To Determine latest 3 entries in a table

Hello All,
I have a table with the stucture
Table A
PK_ID NUMBER,
Name_ID_FK Number,
Entry_Date Date // Where Entry date is date the row is inserted
This might have data like
1,1,12/12/2001
2,1,12/13/2001
3,2,12/14/2001
4,2,12/15/2001
5,4,12/16/2001
6,5,12/17/2001
and so on...
I want to determine the last (latest) 3 unique entries
i.e in the above case that should return
Name_ID_FK 2,4 and 5
Any help is appreciated.
Thanks in advance,
Sudhindra

sql>select * from a;
    PK_ID NAME_ID_FK ENTRY_DATE
        1          1 12/12/2001
        2          1 12/13/2001
        3          2 12/14/2001
        4          2 12/15/2001
        5          4 12/16/2001
        6          5 12/17/2001
6 rows selected.
sql>select distinct name_id_fk
  2    from (select name_id_fk
  3            from a
  4           order by entry_date desc)
  5   where rownum <= 3;
NAME_ID_FK
         2
         4
         5
3 rows selected.
-- What if name_id_fk = 2 has the two most recent dates?
sql>update a set entry_date = entry_date + 10 where name_id_fk = 2;
2 rows updated.
sql>select distinct name_id_fk
  2    from (select name_id_fk
  3            from a
  4           order by entry_date desc)
  5   where rownum <= 3;
NAME_ID_FK
         2
         5
-- Should this now return just 2 rows?  What is the rule?
2 rows selected.

Similar Messages

  • SELECT statement INTO TABLE - what determines order of entries in in.table?

    Hello everyone.
    I habe a question: A select statement selects multiple entries from a data base table that are put into an internal table.
    The question : What determines the order of the entries in the internal table? 
    And what might change the order all of a suddden in that internal table?
    Could reorg. activities/archiving (even ongoing) cause any different result in the order as before ?
    I do not mean different entries but I rather refer to the sorting of the internal table, if it is not explicitly stated in the select or in parts of the coding.
    Any explaination is appreciated!
    Thanks!
    CN.

    Hi,
    Its not the order in which you specify the fields in the where clause that affects the sequence of the selection.
    What I said is that by default the entries are sorted by the primary key of the table.
    I did a bit of a test out of curiosity and after seeing so many different replies on the thread. I was surprised to see the results of the different scenarios.
    REPORT  Z_TEST_SELECTION                        .
    tables vbap.
    data it_vbap type table of vbap.
    data it_vbak type table of vbak.
    select-options s_vbeln for vbap-vbeln.
    select-options s_posnr for vbap-posnr.
    "======================================================================
    "s_vbeln has following entries
    " 5
    " 6
    " 1
    " 2
    "s_posnr has following entries.
    " 10
    " Sorts in ascending order by VBELN and POSNR by default
    select * up to 20 rows from vbap into table it_vbap.
    refresh it_vbap.
    "Below two cases are absolutely unreliable and I would suggest that a
    "sort should be mentioned after these statements. So when we use select
    "options specify values with the in clause, the sort is not reliable.
    "Strangely , neither the sequence is not determined by the order in
    "which the data was entered in the select option nor in the sequence of
    "the primary key. And this is still a mystery to me about the order in which
    "the entries are selected.
    " Any answeres anyone ?
    "1.
    select * from vbap into table it_vbap
    where  vbeln in ('0000000002','0000000006','0000000005','0000000001')
    and posnr in s_posnr.
    * Resulted in
    *900  |0000000001|000010|
    *900  |0000000005|000010|
    *900  |0000000006|000010|
    *900  |0000000002|000010|
    "=========================================================
    "2.
    select * from vbap into table it_vbap
    where  posnr in s_posnr
    and    vbeln in s_vbeln.
    * Resulted in
    *900  |0000000002|000010|
    *900  |0000000001|000010|
    *900  |0000000005|000010|
    *900  |0000000006|000010|
    refresh it_vbap.
    "Here the orders were selected in Decending order of Sales order, however
    "the posnr is not mentioned in the order by clause, hence posnr was
    "still in ascending order.
    select * from vbap into table it_vbap
    where posnr in s_posnr and vbeln in s_vbeln
    order by vbeln descending.
    write : 'done!'.
    refresh it_vbap.
    "Here the orders were selected in Descending order of Sales order.
    select * from vbak into table it_vbak where vbeln in s_vbeln
    order by vbeln descending.
    " The entries were selected in the ascending order of the key VBELN and
    " POSNR, so it seems that for all entries does not affect the selection of records.
    select * from vbap into table it_vbap
    for all entries in it_vbak
    where vbeln = it_vbak-vbeln
    and   posnr in s_posnr.
    Of course if you change the order of the primary key (in the transparent table, not in the where clause), the entries will be sorted as per the changed key.
    But we seldom change the the sequence of the primary key. In most cases we normally add a new field and make it a key field in addition to the existing key fields.
    I hope this helps you.
    There might be many more cases and examples than given above, which you can try and reply on this post. Lets see what we come up with.
    regards,
    Advait

  • How to find latest entry in the table according to time

    how to find latest entry in the table according to the time
    is there any function module to do so
    \[removed by moderator\]
    Regards
    Shashi
    Edited by: Jan Stallkamp on Aug 25, 2008 4:39 PM

    Hi,
    If you want to read the entry from an internal table,
    sort the internal table in the descending order by the time and
    delete adjacent duplicates by comparing the fields other than time and the internal table will have the latest record.
    Suggestion: instead of only time try to have one more field called date with the time combination
    Regards,
    Ramesh

  • How to find latest Revison of material in table?

    We managed our material change with CO & revision.
    Now I have requirement to prepare one report for material, in that report I ned latest rev & CO assigned to material.
    Where can I find latest rev of materials in table. I know we can use table AEOI, but this table display all rev. I need only & only latest rev.
    I have 38,000 materials handy, now I need to find latest rev of these 38,000 materials.
    Please help.
    Tom.

    I guess the question would be is this a one time report or something ongoing?
    If you can use MS Access you can create this report by downloading the AEOI table to access.
    Then create a copy of the table structure only and set the key field as Material.
    Set up a query to append the unique material numbers with the highest version number into the new table.
    Modify the query and repeat with highest version number minus 1.  Repeat until the version number is 1.
    Once  a material has been copied into the table, no further entries will be allowed because the material number is the key filed.  You should wind up with a table containing the information you want.
    You might be able to do this with a single query by using the MAX fuction on version number field.
    Once you create this, it should be easy to throw in some macros, add a button to a form, and be able to repeat the process at any time with the click of a button.
    If you need to do this in SAP, you'd have to create a Z table, have an ABAP'er write the correct query to copy over the records from the AEOI table and than base reports on this Z table.
    FF

  • BPM question - Delete all entries in a table and Insert data

    Hello,
    I'm new to PI BPM and have this scenario to implement.
    I'm supposed to delete all entries in a table and then insert data to the same table.
    Is BPM necessary? If it is, what is the best practice to implement my scenario?
    I want to make sure that all entries were successfully deleted before insert data.
    How do I check if all entries in the table were deleted successfully before
    initiating insert.
    Thank you.
    -Won

    Hi Won,
    Yes, this is possible without BPM.
    You have to create 2 separate mappings, each for deleting & inserting the records.
    You will have 2 different Interface mappings as well.
    But in ID, create a single interface determination.
    In that, configure Deletion interface mapping first and in the second row, configure Insertion interface mapping.
    Please make sure that the parameter 'Mainain Order At runtime' is checked in interface determination.
    -Supriya.

  • BEx analyzer 7.x: How to determine first data column in result table?

    Hi,
    we are upgrading to 2004s where the SAPBEXqueries Sheet is not used anymore. Instead there is BExRepositorySheet. Is there a way to determine first data column in result table?
    Regards, Josko.

    hi dinesh......
    please use  'INTO TABLE' instead of  ' INTO CORRESPONDING FIELDS OF TABLE '.  and use ' FOR ALL ENTRIES '............based on relavence ....coz when I set break point  over the select queries it took me long time to execute .......
    becoz u didnt used the for all entries  in select query ............ u r read statement
    READ TABLE I_KNA1 INTO WA_KNA1 WITH KEY KUNNR = WA_KNVV-KUNNR.  ..... 
    it is reading wrong kunnr value .............even it took long time to execute ......
    use sort where ever required...
    use for all entries .......and check weather the table initial or not......
    use sy-subrc check where ever reqired ..............

  • 0HR_PT_1 Extraction issue No entry in HR table T569R for 0106

    hi experts,
    we are implementing Time and Labour cube, and trying to extracting from ECC to BW cube (0PT_C01).
    when i extract data from 0HR_PT_1 data source the PSA Request is in yellow for a long time. when i check the issue in ECC. Its giving the below issues.
    No entry in HR table T569R for 0106
    No entry in HR table T569R for 0105
    No personal work schedule for personnel number 00013090
    No personal work schedule for personnel number 00013145
    No personal work schedule for personnel number 00500009
    No personal work schedule for personnel number 00500026
    How to rectify the above issues and the load make successful?
    Regards
    venuscm

    Hi,
    Do the below things, as per the note 696836.
    By using the view V_T569R, maintain the retroactive categories
    1. 05 'Earliest time data carry-over'
    2. 06 'Latest time data carry-over'
    which constitute the time frame, in which the system will perform an extraction. Outside this time frame, the system will neither select any data nor calculate any delta. SAP recommends that you choose a period that includes approximately the current year. You have to maintain and update this time window periodically.
    Regards,
    Anil Kumar Sharma. P

  • Posting thro J1IEX error -  DEBIT ENTRY missing in table J_1ITRNACC

    Hello,
    While posting Cenvat Credit in J1IEX, the system is giving error as "Customization for DEBIT ENTRY missing in table J_1ITRNACC".
    - "The customization details have not been maintained for this company".
    When checked thro' SPRO>IMG>Logistics-General>Tax on goods movement>India>Account determination>Specify excise accounts per excise transaction', it shows all debit, credit entries for excise transaction type 'GRPO'.
    Kindly suggest me what else need to be checked? Part I entry is already done during MIGO.
    Thanking in advance.
    Regards
    Ratna

    Dear Velu & Pankaj Singh,
    Thanks for your prompt reply.
    We already had setting for the following.
    GRPO CR Credit CLEAR CENVAT clearing account
    GRPO DR Debit ONHOLD CENVAT on hold account
    GRPO DR Debit RG23AED RG 23 AED account
    GRPO DR Debit RG23AT1 RG 23 AT1 Account
    GRPO DR Debit RG23BED RG 23 BED account
    GRPO DR Debit RG23ECS RG 23 ECS Account
    The above is also maintained for sub trn type 'IP'
    But we do not have any entry for SED account i.e. record does not exist for GRPO DR Debit RG23SED RG 23 SED Account.
    The excise entries contains BED account, AED account, ECS account and AT1 account as SHE CEss account.
    Pl. suggest.
    Regards
    Ratna

  • Entry missing in table TMC6 for unit KOEIN

    Hi,
    The STD Info structure S071 us marked for VA (Sales Order), it does not allows to change as it is SAP STD. I have a scenarios where these tables to be updated for VB (Contract). So i thought of creating a new table copying the existing S071, while i try to Update Rules for the new Info structure, i am getting the below copied error.
    I went through some of the links, but those are not giving the right answer. All comments are welcome...thanks
    Entry missing in table TMC6 for unit KOEIN:
    Message no. M2108
    Diagnosis
    The system attempts to determine the unit of the source field for field
    KAWRT. The data element KOEIN was defined as part of this process.
    The system now searches table TMC6 for this data element in order to
    define the two-character units ID. However, no appropriate entry could
    be found.

    Hi,
    It says -
    Solution
    This is corrected in the Support Package for Release 4.70, 5.00, 6.00, 6.02 and 6.03.
    We are running on 6.00 , how ever i will go through the Correction Instruction... thanks

  • Unable to display data no entry in the table without using Model clause

    Hi,
    I've an urgent requirement described below :
    The previously posted Question has been answerted using Model Clause:
    Is there any way out to solve it without using Model clause:
    I've a table named as "sale" consisting of three columns : empno, sale_amt and sale_date.
    (Please ref. The table script with data as given below)
    Now if I execute the query :
    "select trunc(sale_date) sale_date, sum(sale_amt) total_sale from sale group by trunc(sale_date) order by 1"
    then it displays the data for the dates of which there is an entry in that table. But it does not display data for the
    date of which there is no entry in that table.
    If you run the Table script with data in your schema, then u'll see that there is no entry for 28th. Nov. 2009 in
    sale table. Now the above query displays data for rest of the dates as its are in sale table except for 28th. Nov. 2009.
    But I need its presence in the query output with a value of "sale_date" as "28th. Nov. 2009" and that of "total_sale" as
    "0".
    Is there any means to get the result as I require?
    Please help ASAP.
    Thanks in advance.
    Create table script with data:
    CREATE TABLE SALE
    EMPNO NUMBER,
    SALE_AMT NUMBER,
    SALE_DATE DATE
    SET DEFINE OFF;
    Insert into SALE
    (EMPNO, SALE_AMT, SALE_DATE)
    Values
    (100, 1000, TO_DATE('12/01/2009 10:20:10', 'MM/DD/YYYY HH24:MI:SS'));
    Insert into SALE
    (EMPNO, SALE_AMT, SALE_DATE)
    Values
    (100, 1000, TO_DATE('11/30/2009 10:21:04', 'MM/DD/YYYY HH24:MI:SS'));
    Insert into SALE
    (EMPNO, SALE_AMT, SALE_DATE)
    Values
    (100, 1000, TO_DATE('11/29/2009 10:21:05', 'MM/DD/YYYY HH24:MI:SS'));
    Insert into SALE
    (EMPNO, SALE_AMT, SALE_DATE)
    Values
    (100, 1000, TO_DATE('11/26/2009 10:21:06', 'MM/DD/YYYY HH24:MI:SS'));
    Insert into SALE
    (EMPNO, SALE_AMT, SALE_DATE)
    Values
    (100, 1000, TO_DATE('11/25/2009 10:21:07', 'MM/DD/YYYY HH24:MI:SS'));
    Insert into SALE
    (EMPNO, SALE_AMT, SALE_DATE)
    Values
    (200, 5000, TO_DATE('11/27/2009 10:23:06', 'MM/DD/YYYY HH24:MI:SS'));
    Insert into SALE
    (EMPNO, SALE_AMT, SALE_DATE)
    Values
    (200, 4000, TO_DATE('11/29/2009 10:23:08', 'MM/DD/YYYY HH24:MI:SS'));
    Insert into SALE
    (EMPNO, SALE_AMT, SALE_DATE)
    Values
    (200, 3000, TO_DATE('11/24/2009 10:23:09', 'MM/DD/YYYY HH24:MI:SS'));
    Insert into SALE
    (EMPNO, SALE_AMT, SALE_DATE)
    Values
    (200, 2000, TO_DATE('11/30/2009 10:23:10', 'MM/DD/YYYY HH24:MI:SS'));
    Insert into SALE
    (EMPNO, SALE_AMT, SALE_DATE)
    Values
    (300, 7000, TO_DATE('11/24/2009 10:24:19', 'MM/DD/YYYY HH24:MI:SS'));
    Insert into SALE
    (EMPNO, SALE_AMT, SALE_DATE)
    Values
    (300, 5000, TO_DATE('11/25/2009 10:24:20', 'MM/DD/YYYY HH24:MI:SS'));
    Insert into SALE
    (EMPNO, SALE_AMT, SALE_DATE)
    Values
    (300, 3000, TO_DATE('11/27/2009 10:24:21', 'MM/DD/YYYY HH24:MI:SS'));
    Insert into SALE
    (EMPNO, SALE_AMT, SALE_DATE)
    Values
    (300, 2000, TO_DATE('11/29/2009 10:24:22', 'MM/DD/YYYY HH24:MI:SS'));
    Insert into SALE
    (EMPNO, SALE_AMT, SALE_DATE)
    Values
    (300, 1000, TO_DATE('11/30/2009 10:24:22', 'MM/DD/YYYY HH24:MI:SS'));
    COMMIT;
    Any help will be needful for me
    Regards,

    select sale_date,sum(sale_amt) total_sale
    from
    select empno,0 sale_amt,(sale_date + ao.rn) sale_date
    from
    select empno,sale_amt,sale_date ,(t.nxt_dt - t.sale_date) diff
    from
    select empno
    ,sale_amt,trunc(sale_date) sale_date
    ,trunc(nvl(lead(sale_date) over (partition by 1 order by sale_date),sale_date)) nxt_dt
    from sale
    ) t
    where (t.nxt_dt - t.sale_date) >1
    ) rec,(select rownum rn from user_objects where rownum<=200) ao
    where ao.rn <=(rec.diff-1)
    union all
    select empno,sale_amt,trunc(sale_date) sale_date
    from sale
    group by sale_date
    order by 1;
    ~~~~Guess this will serve the purpose...
    Cheers Arpan

  • How to schedule a job poles for a entry in a table.

    Hi All ,
    I have to schedule a job which runs somw stored procedures only on sunday and monday of a week at 3 AM in the morning.
    The condition is that another application puts an entry into a table around 3 AM (some times before and some times late), now my should query
    count in the table and runs the procs , however once its done ,it should do it monday and then it should not run till next sunday.
    I have successfully created a job which keeps failing till it receives the entry and successfully runs the proc once the entry is there...
    However how to schedule this thing ..correctly I need help , I want this job to disable itself on sunday once the procs are run wake up next morning
    run again..and then next week ..
    Following is the job ,
    BEGIN
    -- Job defined entirely by the CREATE JOB procedure.
    DBMS_SCHEDULER.create_job (
    job_name => 'TESTING_FIRST_JOB',
    job_type => 'PLSQL_BLOCK',
    job_action => 'BEGIN SEODS.test_procedure_11292011(); END;',
    start_date => SYSTIMESTAMP,
    repeat_interval => 'freq=minutely; byminute=5',
    end_date => NULL,
    enabled => TRUE,
    comments => 'Job defined entirely by the CREATE JOB procedure.');
    end;
    =====================================================
    and following is the proc...
    create or replace
    procedure
    test_procedure_11292011 as
    var number;
    begin
    insert into SEODS.job_status
    values (systimestamp, 'JOB TEST_PROCEDURE_11292011 HAS STARTED SUCCESSFULLY');
    commit;
    SELECT COUNT(*)
    INTO var
    FROM seods.student_weekend_status;
    if var=0 then
    raise_application_error(-20101, 'There is no record in the student_weekend_status table');
    else
    all_realtime();
    end if;
    insert into SEODS.job_status
    values (systimestamp, 'JOB TEST_PROCEDURE_11292011 HAS FINISHED SUCCESSFULLY');
    commit;
    end;
    Thanks in advance , please help

    Hi,
    This is a known issue in the previous SQL Server version and has been fixed in SQL Server 2012. You should be able to set 90 minutes and the change would be saved in SQL Server 2012.
    http://connect.microsoft.com/SQLServer/feedback/details/346786/ssms-does-not-support-job-schedule-frequencies-greater-than-60-minutes
    Currently, you may schedule the job to occur every 1 hour or you may refer to the suggestion by Latheesh to get around this.
    Thank you for your understanding.
    Tracy Cai
    TechNet Community Support

  • How to delete the entries in CDCLS table

    Hi All,
    There is a potential issue with the size of table CDCLS on production system. We need to reduce or manage the amount of data in this table. CDCLS is a cluster table and cannot be reduced directly,
    So first we were reduced the entries in CDHDR and then from CDPOS tables.
    After that we tried to reduce the entries from CDCLS table through the program - RSSCD7RE, We are facing the error as - no data available and No active archive key was selected.
    Please help me out, how to proceed further in this task. Thanks in advance.

    Go to DB15 and see the archiving object.
    You can also use the archiving object CHANGEDOCU, please refer SAP documents for that.
    Transaction is
    SARA -> Archive object CHANGEDOCU
    Message was edited by:
            ANIRUDDHA DAS

  • How to delete the NULL entries in nest table

    Hi,
    After I used a loop and open/fetch cursor populated the object table
    I found there are random NULL entries in my object table (nest table)
    The data look like this
    NULL NULL           NULL
    NULL NULL           NULL
    123     03-MAY-04     ACTIVE
    NULL NULL           NULL
    NULL NULL           NULL
    234     21-MAY-04     ACTIVE
    NULL NULL           NULL
    345     11-MAY-04     ACTIVE
    NULL NULL           NULL
    How can I get rid of those NULL entries in my nest table? So it can become
    123     03-MAY-04     ACTIVE
    234     21-MAY-04     ACTIVE
    345     11-MAY-04     ACTIVE
    Additional info:
    create type myType as object
    (id NUMBER (10,0),
    eff_date date,
    status VARCHAR2(17)
    create type myNestTab as table of myType;
    I have tried Delete procedure in following two ways.
    Version 1:
    FOR i IN l_my_nest_tab.FIRST..l_my_nest_tab.LAST
    LOOP
    IF l_my_nest_tab(i).id IS NULL THEN
    l_curr_event_tb.DELETE(i);
                   END IF;                         
    END LOOP;
    Version 2:
    FOR i IN l_my_nest_tab.FIRST..l_my_nest_tab.LAST
    LOOP
    IF l_my_nest_tab(i) IS NULL THEN
    l_curr_event_tb.DELETE(i);
                   END IF;                         
    END LOOP;
    Both of them give me the error “no data found.” And only left me the first NOT NULL entry in the table.
    123     03-MAY-04     ACTIVE
    Thanks in avdance.

    Hi Vishnu,
    u can write a report program for this and in that use the event  :
    AT NEW <field-name> ( use primary key)
    your statements
    ENDAT
    for eg.
    loop at itab ( herfe itab must be of type of table for which u want to track new entries)
    at new matnr
    write:/ new record
    endat
    endloop.
    schedule this report in background to run in every 5 or 10 mins as per your requirement and hence changes can be tracked.
    regards
    Vinod

  • Error message while running the payroll-no entry found in table t54c1

    Hi All,
    Im getting this error-error message while running the payroll-no entry found in table t54c1.What is the reason for this error?
    How doi solve it?

    Hello
    From the description of your problem above, your cumulation calender has not been generated for that month.
    Please take necessary action to ensure that calendar type is assigned to pay period in payroll area, by maintaining the payroll period which is represented by Date modifier and period modifier in view maintainance for table T54C1.
    Entries for this table are generated with the program RPUCTP10. (You should not have to create manual entries in this table.)
    Thanks and Kind Regards
    Ramana

  • Error in Sales Order idoc - No appropriate entry found in table ADRT

    Hello SAP Gurus,
    When I am retriggering a sales order idoc I am getting the following error message.
    No appropriate entry found in table ADRT
    How can I resolve this issue.
    Thanks,
    Narayan.

    Hello Lakshmipati,
    I have checked up the customer master. There was no communication method maintained for any of the customers.
    But even then the order idoc is triggered to our XI system.
    For the one that failed I have maintained the communication method and retriggered again. It had the same failure message.
    Any helpful thoughts is highly appreciated.
    Thanks & Regards,
    Narayan.

Maybe you are looking for

  • IMac G4 (Flat Panel) Hard Drive Replacement Cost

    I have an 800 mHz iMac G4 (Flat Panel) that was originally purchased in approximately 2002 or 2003. The hard drive has died. I have three questions: What is an approximate cost for a new hard drive? Where can a hard drive for this computer be purchas

  • I don't think I like iTunes 8.......

    I have compiled some discs and uploaded them before iTunes came out. Now the songs on those compliations have been split up. Is there any way to put them back together. The songs do not drag where they should be. Is there any way to revert back to iT

  • HT201303 How do I recover the answers to my security questions?

    Need to find the answers to my security questions. I forgot that answer to one of them.

  • Flash Player 11 problems installing, PLEASE help

    Hi there, please can you help? I want to install flash player 11 onto a mac G5 OS X powerPC version 10.4.11 and a mobile Samsung galaxy ace android 2.3.6. Neither machine will let me do this and without flash player 11 I cannot BBC iplayer, 4 on dema

  • Vi server exe

    Hi Experts, It has been some time since I tinkered with VI Server, and cannot seem to do what I want. In a nutshell, I want to be able to access the controls of a VI that will be running in a compiled exe (called watchdog.exe) from another VI, in thi