SQL - Retriving Last 6 rows from Join of 2 Tables

Hi All
I want to retrive the last 6 rows from a query which has join of 2 tables
Thanks in adv.
Junu

Hi,
Check this.
SELECT e.empno, e.deptno, e.sal,e.rn
FROM (
     SELECT ROWNUM rn, a.empno, b. deptno, a.sal,
          ( SELECT COUNT(1) FROM scott.emp c, scott.dept d
                    WHERE c.deptno = d.deptno ) max_rn
FROM scott.emp a, scott.dept b
          WHERE a.deptno = b.deptno
     ) e
WHERE rn BETWEEN max_rn -6 AND max_rn     
ORDER BY rn     
Regards
K.Rajkumar

Similar Messages

  • How do I get last 15 rows from a select query

    Hello,
    I am writing a query, and I need to filter the last 15 rows of from the select query.
    For example: Select id, name,join_date from emp order by join_date;
    So I need to get the last 15 rows from emp table so that we can who are the last 15 employees joined the organization. I want to show the result in ascending order, otherwise I would have taken first 15 rows with order by clause as descinding order.
    Thanks in advance,
    Param.

    SQL> select ename, join_date from test222
    2 where rownum < 6
    3 order by join_date
    4 /
    ENAME JOIN_DATE
    CNC5461177794 14-FEB-02
    OR91551178218 14-FEB-02
    TUB9894676495 14-FEB-02
    SG15431177117 14-FEB-02
    PIB9384674017 14-FEB-02
    SQL> SELECT * from
    2 (select ename, join_date from test222
    3 order by join_date desc)
    4 WHERE rownum < 6
    5 /
    ENAME JOIN_DATE
    II61111208401 19-MAR-02
    II61451208038 18-MAR-02
    PI20014184983 01-MAR-02
    BNF2221148037C 28-FEB-02
    UO78902581 18-FEB-02
    Can you see the problem now ?
    You can test this concept with your own table.
    have a good day,
    Sri

  • ORA-22905: cannot access rows from a non-nested table item in Table func

    I am using a table function in Oracle 8.1.7.4.0. I did declare an object type and a collection type like this:
    CREATE TYPE t_obj AS OBJECT ...
    CREATE TYPE t_tab AS TABLE OF t_obj;
    My table function returns t_tab and is called like this:
    SELECT ... FROM TABLE (CAST (my_pkg.table_fnc AS t_tab)) ...
    This works pretty well as long as I run it in the same schema that owns the function and the 2 types. As soon as I run this query from another schema, I get an ORA-22905: cannot access rows from a non-nested table item error, even though I granted execute on both the types and the function to the other user and I created public synonyms for all 3 objects.
    As soon as I specify the schema name of t_tab in the cast, the query runs fine:
    SELECT ... FROM TABLE (CAST (my_pkg.table_fnc AS owner.t_tab)) ...
    I don't like to have a schema hard coded in a query, therefore I'd like to do this without the schema. Any ideas of how to get around this error?

    Richard,
    your 3 statements are correct. I'll go ahead and log a TAR.
    Both DESCs return the same output when run as the other user. And, running the table function directly in SQL*Plus (SELECT my_pkg.table_fnc FROM dual;) also returns a result and no errors. The problem has to be in the CAST function.
    Thanks for your help.

  • ORA-22905: cannot access rows from a non-nested table item

    Hi All,
    This is the overview of the query used in the package.
    select ename,empno,sal,deptno from
    (select deptno from dept) a,
    (select ename,empno,sal from emp1) b
    where empno in (select * from table (pkg1.fun1('empno')))
    and a.deptno=b.deptno
    union
    select ename,empno,sal,deptno from
    (select deptno from dept) c,
    (select ename,empno,sal from emp2) d
    where empno in (select * from table (pkg1.fun1('empno')))
    and c.deptno=d.deptno
    Here the pkg1.fun1 will convert the string ('empno') into table form. ('empno') is the input parameter to the package and is a string of emp numbers.
    compilation is successful. when this is executed the below error pops up
    "ORA-22905: cannot access rows from a non-nested table item"
    Is there any problem with the table function which i am using in this query
    could anyone guide me to the solution.
    Thanks All

    I have used
    CREATE OR REPLACE
    type tab_num as table of number;
    select * from table (cast(pkg1.fun1('empno')) as tab_num))
    This throws an error during compilation itself
    "PL/SQL: ORA-00932: inconsistent datatypes:expected number got varchar2

  • Oracle error ORA-22905: cannot access rows from a non-nested table item

    Oracle error ORA-22905: cannot access rows from a non-nested table item
    Creating a report using oracle plsql code .
    Getting error ;
    Oracle error ORA-22905: cannot access rows from a non-nested table item
    when I am trying to pass data in clause in pl sql proc
    basically I have a proc which takes 2 parameters(a and b)
    proc (
    P_a varchar2,
    p_b varchar2,
    OUT SYS_REFCURSOR
    culprit code which is giving me  the error and on google they say cast it but I dont know how to do it in my context
    --where  id in (
    --        SELECT * FROM THE (SELECT p_cd_common.get_table_from_string(P_a) FROM dual)
    --        union
    --        SELECT * FROM THE (SELECT p_cd_common.get_table_from_string(P_b) FROM dual)
    data sample returned from this :SELECT * FROM THE (SELECT p_cd_common.get_table_from_string(P_a) FROM dual)
    'Abc','def',
    data sample returned from this;SELECT * FROM THE (SELECT p_cd_common.get_table_from_string(P_b) FROM dual)
    'fgd','fth',
    Any answers ?
    How to pass data in clause in a better way

    Why are you creating a duplicate post? I already asked you to post p_cd_common.get_table_from_string. In particular what is function return type and where it is declared. As I already mentioned, most likely function return type is declared in the package and therefore is PL/SQL type. And TABLE operator can only work with SQL types.
    SY.

  • SP which returns error cannot access rows from a non-nested table item.

    Dear Experts
    I have an SP which gives error " cannot access rows from a non-nested table item ". But here the strange thing is, it works fine with one query. But I write union query with another table, only then it gives error.
    CREATE OR REPLACE PROCEDURE SP_MONTHLYSALESUMMARY (
    P_TRANSACTIONMONTH VARCHAR2,
    P_LEDGERID VARCHAR2,
    O_RESULTSET OUT TYPES.CURSORTYPE)
    AS
    BEGIN
    OPEN O_RESULTSET FOR
    -- POINT OF SALE
    SELECT
    L.DESCRIPTION LEDGERNAME,
    LS.DESCRIPTION LEDGERSUBGROUPNAME,
    C.CORPORATENO
    || E.EMPLOYEENO
    || AF.AFFILIATEMEMBERNO
    || M.MEMBERNO
    AS ID,
    C.NAME || E.NAME || AF.NAME || M.NAME AS NAME,
    SUM(CASE
    WHEN PB.EMPLOYEE_ID IS NOT NULL
    AND T.ISCREDITTRANSACTIONMODE = 2
    THEN
    PB.TAXAMOUNT
    ELSE
    0
    END)
    EMPLOYEEDEBITCARDTAXAMOUNT,
    L.PRINTNO
    FROM POINTOFSALEBILL PB
    INNER JOIN POINTOFSALEBILLDETAIL PD
    ON PB.POINTOFSALEBILL_ID = PD.POINTOFSALEBILL_ID
    INNER JOIN TRANSACTIONTYPE TY
    ON TY.TRANSACTIONTYPE_ID = PB.TRANSACTIONTYPE_ID
    INNER JOIN TRANSACTIONMODE T
    ON T.TRANSACTIONMODE_ID = PB.TRANSACTIONMODE_ID
    INNER JOIN LEDGER L
    ON L.LEDGER_ID = PD.LEDGER_ID
    INNER JOIN LEDGERSUBGROUP LS
    ON LS.LEDGERSUBGROUP_ID = L.LEDGERSUBGROUP_ID
    LEFT JOIN CORPORATE C
    ON C.CORPORATE_ID = PB.CORPORATE_ID
    LEFT JOIN EMPLOYEE E
    ON E.EMPLOYEE_ID = PB.EMPLOYEE_ID
    LEFT JOIN AFFILIATEMEMBER AF
    ON AF.AFFILIATEMEMBER_ID = PB.AFFILIATEMEMBER_ID
    LEFT JOIN MEMBER M
    ON M.MEMBER_ID = PB.MEMBER_ID
    WHERE TY.ISDEBIT = 1 AND PB.FLAG = 0 AND T.ISROOMNO = 2
    AND (P_TRANSACTIONMONTH IS NULL
    OR P_TRANSACTIONMONTH =
    TO_CHAR (PB.BILLDATE, 'fmMONTH-YYYY'))
    AND (P_LEDGERID IS NULL
    OR L.LEDGER_ID IN
    ( (SELECT COLUMN_VALUE
    FROM TABLE(GET_ROWS_FROM_LIST1 (
    P_LEDGERID,
    GROUP BY L.DESCRIPTION,
    LS.DESCRIPTION,
    C.CORPORATENO
    || E.EMPLOYEENO
    || AF.AFFILIATEMEMBERNO
    || M.MEMBERNO,
    C.NAME || E.NAME || AF.NAME || M.NAME,
    L.PRINTNO;
    END SP_MONTHLYSALESUMMARY;
    GET_ROWS_FROM_LIST1 is a function, which i am using to pass " IN " to oracle. There is no problem with this, since it works fine with one query
    REATE OR REPLACE FUNCTION BCLUB1868.GET_ROWS_FROM_LIST1
    (L IN LONG DEFAULT NULL, SEP IN VARCHAR2 DEFAULT ',')
    RETURN MYVARCHARTABLE1 PIPELINED
    AS
    L_POS INT := 1;
    L_NEXT INT;
    L_PART VARCHAR(500);
    BEGIN
    SELECT INSTR( L, SEP, L_POS) INTO L_NEXT FROM DUAL;
    WHILE (L_NEXT>0)
    LOOP
    SELECT SUBSTR(L, L_POS, L_NEXT - L_POS) INTO L_PART FROM DUAL;
    PIPE ROW(L_PART);
    SELECT L_NEXT + 1, INSTR( L, SEP, L_POS)
    INTO L_POS, L_NEXT FROM DUAL;
    END LOOP;
    SELECT SUBSTR(L, L_POS) INTO L_PART FROM DUAL;
    PIPE ROW(L_PART);
    RETURN;
    END;
    Request help from you all experts in the forum

    Here it is
    CREATE OR REPLACE PROCEDURE SP_GRCS (
    P_TRANSACTIONMONTH VARCHAR2,
    P_LEDGERID VARCHAR2,
    O_RESULTSET OUT TYPES.CURSORTYPE)
    AS
    BEGIN
    OPEN O_RESULTSET FOR
    -- Point of sale
    SELECT *
    FROM ( SELECT L.DESCRIPTION LEDGERNAME,
    LS.DESCRIPTION LEDGERSUBGROUPNAME,
    C.CORPORATENO
    || E.EMPLOYEENO
    || AF.AFFILIATEMEMBERNO
    || M.MEMBERNO
    AS ID,
    C.NAME || E.NAME || AF.NAME || M.NAME AS NAME,
    SUM(CASE
    WHEN PB.EMPLOYEE_ID IS NULL
    AND T.ISCREDITTRANSACTIONMODE = 1
    THEN
    PB.BILLAMOUNT
    ELSE
    0
    END)
    MEMBERDEBITAMOUNT,
    L.PRINTNO
    FROM POINTOFSALEBILL PB
    INNER JOIN POINTOFSALEBILLDETAIL PD
    ON PB.POINTOFSALEBILL_ID = PD.POINTOFSALEBILL_ID
    INNER JOIN TRANSACTIONTYPE TY
    ON TY.TRANSACTIONTYPE_ID = PB.TRANSACTIONTYPE_ID
    INNER JOIN TRANSACTIONMODE T
    ON T.TRANSACTIONMODE_ID = PB.TRANSACTIONMODE_ID
    INNER JOIN LEDGER L
    ON L.LEDGER_ID = PD.LEDGER_ID
    INNER JOIN LEDGERSUBGROUP LS
    ON LS.LEDGERSUBGROUP_ID = L.LEDGERSUBGROUP_ID
    LEFT JOIN CORPORATE C
    ON C.CORPORATE_ID = PB.CORPORATE_ID
    LEFT JOIN EMPLOYEE E
    ON E.EMPLOYEE_ID = PB.EMPLOYEE_ID
    LEFT JOIN AFFILIATEMEMBER AF
    ON AF.AFFILIATEMEMBER_ID = PB.AFFILIATEMEMBER_ID
    LEFT JOIN MEMBER M
    ON M.MEMBER_ID = PB.MEMBER_ID
    WHERE TY.ISDEBIT = 1 AND PB.FLAG = 0 AND T.ISROOMNO = 2
    AND (P_TRANSACTIONMONTH IS NULL
    OR P_TRANSACTIONMONTH =
    TO_CHAR (PB.BILLDATE, 'fmMONTH-YYYY'))
    AND (P_LEDGERID IS NULL
    OR L.LEDGER_ID IN
    ( (SELECT COLUMN_VALUE
    FROM TABLE(GET_ROWS_FROM_LIST1 (
    P_LEDGERID,
    GROUP BY L.DESCRIPTION,
    LS.DESCRIPTION,
    C.CORPORATENO
    || E.EMPLOYEENO
    || AF.AFFILIATEMEMBERNO
    || M.MEMBERNO,
    C.NAME || E.NAME || AF.NAME || M.NAME,
    L.PRINTNO
    UNION ALL
    -- Guest Registration
    SELECT L.DESCRIPTION LEDGERNAME,
    LS.DESCRIPTION LEDGERSUBGROUPNAME,
    C.CORPORATENO
    || E.EMPLOYEENO
    || AF.AFFILIATEMEMBERNO
    || M.MEMBERNO
    AS ID,
    C.NAME || E.NAME || AF.NAME || M.NAME AS NAME,
    SUM(CASE
    WHEN PB.EMPLOYEE_ID IS NULL
    AND T.ISCREDITTRANSACTIONMODE = 1
    THEN
    PB.BILLAMOUNT
    ELSE
    0
    END)
    MEMBERDEBITAMOUNT,
    L.PRINTNO
    FROM GUESTREGISTRATION PB
    INNER JOIN GUESTREGISTRATIONDETAIL PD
    ON PB.GUESTREGISTRATION_ID = PD.GUESTREGISTRATION_ID
    INNER JOIN TRANSACTIONTYPE TY
    ON TY.TRANSACTIONTYPE_ID = PB.TRANSACTIONTYPE_ID
    INNER JOIN TRANSACTIONMODE T
    ON T.TRANSACTIONMODE_ID = PB.TRANSACTIONMODE_ID
    INNER JOIN LEDGER L
    ON L.LEDGER_ID = PD.LEDGER_ID
    INNER JOIN LEDGERSUBGROUP LS
    ON LS.LEDGERSUBGROUP_ID = L.LEDGERSUBGROUP_ID
    LEFT JOIN CORPORATE C
    ON C.CORPORATE_ID = PB.CORPORATE_ID
    LEFT JOIN EMPLOYEE E
    ON E.EMPLOYEE_ID = PB.EMPLOYEE_ID
    LEFT JOIN AFFILIATEMEMBER AF
    ON AF.AFFILIATEMEMBER_ID = PB.AFFILIATEMEMBER_ID
    LEFT JOIN MEMBER M
    ON M.MEMBER_ID = PB.MEMBER_ID
    WHERE TY.ISDEBIT = 1 AND PB.FLAG = 0 AND T.ISROOMNO = 2
    AND (P_TRANSACTIONMONTH IS NULL
    OR P_TRANSACTIONMONTH =
    TO_CHAR (PB.BILLDATE, 'fmMONTH-YYYY'))
    AND (P_LEDGERID IS NULL
    OR L.LEDGER_ID IN
    ( (SELECT COLUMN_VALUE
    FROM TABLE(GET_ROWS_FROM_LIST1 (
    P_LEDGERID,
    GROUP BY L.DESCRIPTION,
    LS.DESCRIPTION,
    C.CORPORATENO
    || E.EMPLOYEENO
    || AF.AFFILIATEMEMBERNO
    || M.MEMBERNO,
    C.NAME || E.NAME || AF.NAME || M.NAME,
    L.PRINTNO)
    ORDER BY PRINTNO;
    END SP_GRCS;
    I have even tried adding L.Ledger_ID in select statement.

  • Retrive last inserted  record  from database table

    Hi,
    some body inserting a record into table 'A' through some procedure/java program.i want to retrive the last inserted record from database table.records are not stored in order.Can any body help me.

    In general, unless you are storing a timestamp as part of the row or you have some sort of auditing in place, Oracle has no idea what the "first" or "last" record in a table is. Nor does it track when a row was inserted.
    - If you have the archived logs from the point in time when the row was inserted, you could use LogMiner to find the timestamp
    - If the insert happened recently (i.e. within hours), you may be able to use flashback query to get the value
    - If you're on 10g or later and the table was built with ROWDEPENDENCIES and the insert happened in the last few days and you can deal with a granularity of a few seconds and you don't need 100% accuracy, you could get the ORA_ROWSCN of the row and convert that to a timestamp.
    If this is something you contemplate needing, you need to store the data in the row or set up some sort of auditing.
    Justin

  • SQL query - select one row from each date group

    Hi,
    I have data as follows.
    Visit_Date Visit_type Consultant
    05/09/2009 G name1
    05/09/2009 G name2
    05/09/2009 G name3
    06/09/2009 I name4
    07/09/2009 G name5
    07/09/2009 G name6
    How to select data as follows
    05/09/2009 G name1
    06/09/2009 G name4
    07/09/2009 G name5
    i.e one row from every visit_date
    Thanks,
    MK Nathan
    Edited by: k_murali on Oct 7, 2009 10:44 PM

    Are you after this (one row per date per visit_type)
    with dd as (select to_date('05/09/2009','MM/DD/YYYY') Visit_Date, 'G' Visit_type, 'name1' Consultant from dual
                union all
                select to_date('05/09/2009','MM/DD/YYYY') Visit_Date, 'G' Visit_type, 'name2' Consultant from dual
                union all
                select to_date('05/09/2009','MM/DD/YYYY') Visit_Date, 'G' Visit_type, 'name3' Consultant from dual
                union all
                select to_date('06/09/2009','MM/DD/YYYY') Visit_Date, 'G' Visit_type, 'name4' Consultant from dual
                union all
                select to_date('07/09/2009','MM/DD/YYYY') Visit_Date, 'G' Visit_type, 'name5' Consultant from dual
                union all
                select to_date('07/09/2009','MM/DD/YYYY') Visit_Date, 'G' Visit_type, 'name6' Consultant from dual
                union all
                select to_date('07/09/2009','MM/DD/YYYY') Visit_Date, 'F' Visit_type, 'name7' Consultant from dual)
    select trunc(visit_date) visit_date, visit_type, min(consultant)
    from   dd
    group by trunc(visit_date), visit_type
    order by trunc(visit_date);
    VISIT_DAT V MIN(C
    09/MAY/09 G name1
    09/JUN/09 G name4
    09/JUL/09 G name5
    09/JUL/09 F name7or are you after only one row per date?:
    with dd as (select to_date('05/09/2009','MM/DD/YYYY') Visit_Date, 'G' Visit_type, 'name1' Consultant from dual
                union all
                select to_date('05/09/2009','MM/DD/YYYY') Visit_Date, 'G' Visit_type, 'name2' Consultant from dual
                union all
                select to_date('05/09/2009','MM/DD/YYYY') Visit_Date, 'G' Visit_type, 'name3' Consultant from dual
                union all
                select to_date('06/09/2009','MM/DD/YYYY') Visit_Date, 'G' Visit_type, 'name4' Consultant from dual
                union all
                select to_date('07/09/2009','MM/DD/YYYY') Visit_Date, 'G' Visit_type, 'name5' Consultant from dual
                union all
                select to_date('07/09/2009','MM/DD/YYYY') Visit_Date, 'G' Visit_type, 'name6' Consultant from dual
                union all
                select to_date('07/09/2009','MM/DD/YYYY') Visit_Date, 'F' Visit_type, 'name7' Consultant from dual)
    select trunc(visit_date) visit_date, min(visit_type) visit_type, min(consultant)
    from   dd
    group by trunc(visit_date)
    order by trunc(visit_date);
    VISIT_DAT V MIN(C
    09/MAY/09 G name1
    09/JUN/09 G name4
    09/JUL/09 F name5

  • Delete a Row from a Nested Data Table

    Hi... Good day...
    I have a dataTable nested within another DataTable.
    I want to delete a Row from the nested DataTable on clicking a remove Button at the Last column of the Nested DataTable, Jus in the browser.
    How to accomplish this using JavaScript?
    Here is my Code Snippet..
    <h:dataTable value="#{myListBean.rpts}" binding="#{myListBean.parent}" var="item" headerClass ="header" rowClasses = "whiteRow,grayRow" cellspacing="0" cellpadding="4" border = "0"  id = "dataTable">
    <h:column>
         <h:graphicImage id="expand" value="static/images/plus.gif" onclick="showNested(this);" rendered="#{item.schedImgurl=='static/images/scheduled.gif'}"/>
          <div id="#{item.rptId}">
          <h:dataTable id="orderLines" binding="#{myListBean.child}"  value="#{item.scheduleList}" var="schedItem" style="display: none;">
          <h:column>
           <h:outputText value="#{schedItem.scheduleID}" style="display:none;" />
             <f:facet name="header" >
                   <h:outputText value="Scheduled Criteria"/>
              </f:facet>
             <h:outputText value="#{schedItem.scheduleCriteriaName}" />
             <span onMouseOver="JavaScript:showScheduleTooltip(event,'#{schedItem.toolTipCriteraia}')" onMouseOut="JavaScript:hideTooltip()">...</span>
          </h:column>
            <h:column>
           <f:facet name="header" >
          <h:outputText value="Frequency"/>
          </f:facet>
           <h:outputText value="#{schedItem.scheduleFrequency}" />
          </h:column>
          <h:column>
           <f:facet name="header" >
           <h:outputText value="On"/>
           </f:facet>
           <h:outputText value="#{schedItem.scheduleStartDate}" />
          </h:column>
          <h:column>
            <input type="button" value="Remove"onclick=" Remove();"/>
          </h:column>
    </h:dataTable>
    </div>
    </h:column>
    </h:dataTable>Whatz that I need to do inside the Remove() JavaScript Function ?

    Ram_J2EE_JSF wrote:
    How to accomplish this using JavaScript?Using Javascript? Well, you know, Javascript runs at the client side and intercepts on the HTML DOM tree only. The JSF code is completely irrelevant. Open your JSF page in your favourite webbrowser and view the generated HTML source. Finally just base your Javascript function on it.

  • Need to fetch the last record/row from a table

    Hi,
    I have a requirement like fetching the closing balance of the last record of the table and inserting the same into the opening balance of the immediatetly created next record.
    In simple words, I need to fetch a value from the last row of the record.
    For example, I use the below query,
    select rownum, empno, ename from emp
    where rownum = (select count(rownum) from emp);
    But, the above query does not return any of the record.
    Hence, need help on this.
    Regards,
    Shivakumar A
    Edited by: shiva on Mar 27, 2011 10:14 PM

    Rows in a table are inherently unordered. In order to introduce the concept of "first" and "last", you would need to specify how to order the rows which would require that there was one or more columns in the table that you could order by in order to determine the "last" record.
    In your closing balance example, if we assume that there is something like a BALANCE_DATE column,
    SELECT account_number, balance_amount, balance_date
      FROM (
        SELECT account_number,
               balance_amount,
               balance_date,
               rank() over (partition by account_number order by balance_date desc) rnk
          FROM table_of_balances )
    WHERE rnk = 1Justin

  • Need to update column in table from joining the two tables

    Hi
    i want to update a table by joining two table but i m getting the below error
    single row subquery returns more than one value.
    i m using following query
    update table2
    set deposite_date=(select a.deposit_date from table1 a,table2 b
    where a.ban=b.ban and (a.deposit_date between b.cy_start_date and b.due_date))
    table2
    ban due_date cy_start_date deposite_date
    100 5/6/2011 22/05/2011
    101 7/11/2011 22/05/2011
    102 5/25/2011 22/05/2011
    table1
    ban      deposit_date
    100      04/12/2011
    100      04/15/2011
    102      03/25/2011
    101      04/26/2011
    101      05/06/2011
    101      07/26/2011
    can i insert latest deposit_date from table1 which lies between b.cy_start_date and b.due_date
    Thanks

    Hi,
    Welcome to the forum!
    876466 wrote:
    Hi
    i want to update a table by joining two table but i m getting the below error
    single row subquery returns more than one value.Exactly!
    (   SELECT  a.deposit_date
        FROM    ...returns all deposit_dates.
    (   SELECT  MAX (a.deposit_date)
        FROM    ...would return only the latest one.
    Also, you need to correlate the sub-query to the main query, so that it only looks for rows with the same ban.
    i m using following query
    update table2
    set deposite_date=(select a.deposit_date from table1 a,table2 b
    where a.ban=b.ban and (a.deposit_date between b.cy_start_date and b.due_date))
    table2
    ban due_date cy_start_date deposite_date
    100 5/6/2011 22/05/2011
    101 7/11/2011 22/05/2011
    102 5/25/2011 22/05/2011
    table1
    ban      deposit_date
    100      04/12/2011
    100      04/15/2011
    102      03/25/2011
    101      04/26/2011
    101      05/06/2011
    101      07/26/2011
    can i insert latest deposit_date from table1 which lies between b.cy_start_date and b.due_datePerhaps you need something like this:
    UPDATE  table2     b
    SET     deposit_date     =
             (    SELECT  MAX (a.deposit_date)
                   FROM      table1           a
               WHERE      a.ban          = b.ban
               AND      a.deposit_date BETWEEN b.cy_start_date
                               AND     b.due_date
    ;Notice that table1 is the only table in the FROM clause of the sub-query, but the sub-query is correlated to table2, so we can reference columns from table2 in the sub-query.
    Whenever you have a problem, post CREATE TABLE and INSERT statements for your sample data.
    Also post the results you want from that data, and an explanation of how you get those results from that data, with specific examples.
    If you're asking about a DML statement, such as UPDATE, the CREATE TABLE and INSERT statements should re-create the tables as they are before the DML, and the results will be the contents of the changed table(s) when everything is finished.
    Always say which version of Oracle you're using.
    Edited by: Frank Kulash on Aug 1, 2011 1:58 PM

  • Display columns as rows from non-unique key table

    Hi OTN/Users, I hope you can assist me
    Given a table:
    create table t (a varchar2(30), b int, c date );
    with this data within:
    insert into t values ( a1, 40, to_date( '01-Dec-2012'));
    insert into t values ( a1, 50, to_date( '01-Dec-2012'));
    insert into t values ( a1, 60, to_date( '01-Dec-2012'));
    insert into t values ( b1, 10, to_date( '01-Dec-2012'));
    insert into t values ( b1, 20, to_date( '01-Dec-2012'));
    insert into t values ( b1, 30, to_date( '01-Dec-2012'));
    insert into t values ( c1, 60, to_date( '01-Dec-2012'));
    insert into t values ( c1, 70, to_date( '01-Dec-2012'));
    insert into t values ( c1, 80, to_date( '01-Dec-2012'));
    - I want to output the columns for each of 'a' as a single row e.g:
    a1 40 50 60 01-Dec-2012
    b1 10 20 30 01-Dec-2012
    I've almost got it right, but the 'a' col repeats 4 times for each row of output:
    a1 40
    a1 50
    a1 60
    a1 01-Dec-2012
    -I want to supress repeat output of the first column 'a' but display the rest in a straight line.
    I've tried various things (Pivot, Rollup etc), but the fact i'm keying on a table with non unique rows has complicated things perhaps.
    Any help would be much appreciated

    Hi,
    Pre-11g this is how you would do it :[11.2] Pri @ Bepripd1 > !cat t.sql
    with t(a,b,c) as (
         select  'a1', 40, to_date( '01-Dec-2012') from dual union all
         select  'a1', 50, to_date( '01-Dec-2012') from dual union all
         select  'a1', 60, to_date( '01-Dec-2012') from dual union all
         select  'b1', 10, to_date( '01-Dec-2012') from dual union all
         select  'b1', 20, to_date( '01-Dec-2012') from dual union all
         select  'b1', 30, to_date( '01-Dec-2012') from dual union all
         select  'c1', 60, to_date( '01-Dec-2012') from dual union all
         select  'c1', 70, to_date( '01-Dec-2012') from dual union all
         select  'c1', 80, to_date( '01-Dec-2012') from dual
    ------ end of sample data ------
    select
         a
         ,max(decode(n,1,b,null)) q1
         ,max(decode(n,2,b,null)) q2
         ,max(decode(n,3,b,null)) q3
         ,c
    from (
         select a, b, c, row_number() over (partition by a order by b) n
         from t
    group by a,c
    order by a,c
    [11.2] Pri @ Bepripd1 > @t
    A          Q1         Q2         Q3 C
    a1         40         50         60 01/12/2012 00:00:00
    b1         10         20         30 01/12/2012 00:00:00
    c1         60         70         80 01/12/2012 00:00:00------
    From 11g onward, you would :[11.2] Pri @ Bepripd1 > !cat t.sql
    with t(a,b,c) as (
         select  'a1', 40, to_date( '01-Dec-2012') from dual union all
         select  'a1', 50, to_date( '01-Dec-2012') from dual union all
         select  'a1', 60, to_date( '01-Dec-2012') from dual union all
         select  'b1', 10, to_date( '01-Dec-2012') from dual union all
         select  'b1', 20, to_date( '01-Dec-2012') from dual union all
         select  'b1', 30, to_date( '01-Dec-2012') from dual union all
         select  'c1', 60, to_date( '01-Dec-2012') from dual union all
         select  'c1', 70, to_date( '01-Dec-2012') from dual union all
         select  'c1', 80, to_date( '01-Dec-2012') from dual
    ------ end of sample data ------
    select a,q1,q2,q3,c
    from (
         select a, b, c, row_number() over (partition by a order by b) n
         from t
    pivot (
         max(b)
         for n in (
              1 as q1
              ,2 as q2
              ,3 as q3
    order by a,c
    [11.2] Pri @ Bepripd1 > @t
    A          Q1         Q2         Q3 C
    a1         40         50         60 01/12/2012 00:00:00
    b1         10         20         30 01/12/2012 00:00:00
    c1         60         70         80 01/12/2012 00:00:00Edited by: Nicosa on Nov 9, 2012 2:42 PM

  • ERROR:-22905:ORA-22905: cannot access rows from a non-nested table item

    Hi. I have some code running on oracle 9i that gives me above error.
    Following are my type declarations.
    drop type_obj_tea_icore_glr;
    drop type_tbl_tea_icore_glr;
    create or replace type type_obj_tea_icore_glr as object (
    ns_comp_id_parent number,
    ns_comp_id_child number,
    serv_item_id number,
    glr_display_order number,
    exchange_carrier_circuit_id varchar2(53)
    show err
    create or replace type type_tbl_tea_icore_glr as table of type_obj_tea_icore_glr;
    show err
    I have a function in an oracle package tea_icore_pkg with following signature.
    FUNCTION sortpvcdesign (
    p_document_number serv_req_si.document_number%TYPE,
    p_pvc_serv_item_id serv_item.serv_item_id%TYPE,
    p_orig_port_serv_item_id serv_item.serv_item_id%TYPE
    RETURN type_tbl_tea_icore_glr
    If I run following from SQL prompt, it works fine.
    SELECT * FROM TABLE (tea_icore_pkg.sortpvcdesign(255082,2782636,2723752) );
    But when I use that within another procedure of the same packge, as follows:
    FOR glr_rec IN
    (SELECT *
    FROM TABLE
    (CAST
    (sortpvcdesign (c_pvcs.order_nbr,
    c_pvcs.pvc_serv_item_id,
    c_pvcs.orig_port_serv_item_id
    ) AS type_tbl_tea_icore_glr
    LOOP
    At the runtime, I got the above error message ( as referred to in subject ). I took all of the suggestions on CAST without much success. The user/schema is only one schema here. I mean, no permissions issue. I granted all on above types to public.
    Can someone help?

    Bil,
    I tried both with CAST and without CAST with specifying the colums. The same error.
    OR glr_rec IN
    (SELECT ns_comp_id_parent, ns_comp_id_child, serv_item_id,
    glr_display_order, exchange_carrier_circuit_id
    FROM TABLE
    (CAST
    (sortpvcdesign (c_pvcs.order_nbr,
    c_pvcs.pvc_serv_item_id,
    c_pvcs.orig_port_serv_item_id
    ) AS type_tbl_tea_icore_glr
    LOOP

  • MS SQL server 2008 - Bulk copy from XML to DB table throws bcp_init error

    I have MS SQL server 2008 installed ,
    Windows version - Windows 7 Professional with SP1
    Doing a bulk copy process using the SQL library function bcp_init function in c++ throws xml error and its not inserting the data into the tables.
    Error received ,
    XML Datatransfer error: XML data or another error occurred while reading file 'd:\temp\scripts\dbtoolscripts\table_data.xml':
    But this works in others machines with the same windows version and SQL version.

    We are using the same SQL lib function bcp_init , we have written a separate class to load the ODBC32.dll , sqlncli.dll  and use the  bcp_init and other SQL functions from that. As i mentioned earlier we all use the same application build , but
    its not working in my machine only other machines its working.
    There is no provision to load XML files when you use the BCP interface in sqlncli.dll. You can of course load XML files, but the BCP API does not know that it is XML, it only sees a number of bytes.
    So that error message is not coming from the BCP API, but somewhere else. Maybe your own code in reaction to some error from the BCP API. But without any clue of that error message, we can't help you.
    I think you will need to do some debugging or by some other means improve your diagnostics.
    Erland Sommarskog, SQL Server MVP, [email protected]

  • Page having PL/SQL process and Automatic Row Process for 2 different tables

    Hi,
    I have a page containing 2 regions A & B.
    Region-A content would be updated to table T1(PK : Ticket#).
    Region-B content would be inserted into table T2(PK: Attachment# ; FK: Ticket#).
    Region-B is used for uploading a file content into T2.
    Since I cannot use 2 DML processes on the page for 2 different tables with a common column, so I have a PL/SQL process to update the record into T1 and an Automatic Row Process(DML) for inserting into T2.
    Now the issue is in Region-B when I select a file using 'Browse' button and click on Upload button to fire the Automatic Row Process, the success message is displayed but the file is not uploaded into the table. But when I moved the entire Region-B and the Automatic Row Process to a different page and clicking Upload is working fine and inserting the record into the table along with the file content.
    An item P10_TICKET_NUMBER with source type as Database column with source value as TICKET_NUMBER is used in Region-A.
    I have gone through the forums and found some of the threads below
    Re: 2 Automated Row Processes for one page?
    Re: Error when trying to create 2 Forms on same page on 2 tables with ID as
    where people facing similar issues but here I have followed the solution provided(with one PL/SQL process and other Automatic process) in the threads but still issue persists.
    Can anyone throw some light on this please.
    Thanks,
    Raj.

    Hi Teku,
    You just have a look at this thread, where u can find a solution for your problem.
    INSERTING Records into Second table based on First table Primary Key
    hope this helps.
    Bye,
    Srikavi

Maybe you are looking for

  • Portal rendering errors using IE10

    Hi, We've recently patched all of our SAP systems (portal and backend) and checked we're at the requisite level for IE10 support in the PAM. We're currently in the process of testing all of our applications for IE10 compliance and have identified two

  • Apple keyboard not working with iPad

    I recently bought an Apple Bluetooth keyboard to use with my iPad and the first day it worked fine but then the next day it wouldn't work at all except to let me put in my pass code, search things on the iPad, and bring the keyboard up and down with

  • N:1 Multimapping

    Hi, all can someone explain me  SAP BPM Pattern BpmPatternCollectMessage? transformation Step use OM BpmPatternCollectMerge with MM BpmPatternCollectMerge Mapping source is of occurence 0... unbound  and target ist of occurence 1 But in graphic mappi

  • Adding Audio and Video Content to EPUB from InDesign CS5.5 | CS5.5 Design Premium Feature Tour | Adobe TV

    In this episode, Colin Fleming shows how simple it is to embed audio and video content in InDesign CS5.5 for export to EPUB. http://adobe.ly/xT0jKh

  • Support project  (more details)

    Hi Guys,    I am fresher in BW, I have client round in support project    What type off question they will ask, I need more details about tickets,    How many types off ticket? Anybody Immediately I need answer me Regards Ravi