Select another column in same table in CF query

Hello..
I'm curious as to the best way to run this query..
When my CF template page loads I would like it to grab a number value in one column and match it up to the table ID in the same table and display the name for that ID.
Do I have to make another table to do this...? QoQ's?
Thanks!

Hi Dan..
Sorry I wasnt clearer.. here is a picture.
Thanks for the help!

Similar Messages

  • Copy from column  to another column in same table

    Hi,
    Working on EBS Version : 11.5.10.2
    Table Name : ASO_QUOTE_HEADERS_ALL
    COLUMNS :
    QUOTE_STATUS_ID NUMBER
    ATTRIBUTE6 VARCHAR2(240 BYTE);
    Need to copy from quote_status_id to attribute6 for that quote_header_id
    example if quote_status_id = 10 then it should copy in attribute6 = 10 for quote_header_id = 69312
    again if changed to quote_status_id = 10077 then it should replace with attribute6 = 10077
    for quote_header_id = 69312
    i wrote a procedure posted below:
    CREATE OR REPLACE procedure SLC_STATUS_CAPTURE (p_quote_header_id IN number) is
    BEGIN
    UPDATE aso_quote_headers_all SET attribute6 = quote_status_id
    WHERE quote_header_id = p_quote_header_id;
    end SLC_STATUS_CAPTURE;
    then calling this trigger through table level
    BEGIN
    SLC_STATUS_CAPTURE(:OLD.QUOTE_HEADER_ID);
    END;
    it is giving an error.
    Please i need some help.
    Thanks and Regards
    Vijay

    John Spencer wrote:
    As others have mentioned, you cannot change column values in a statement level trigger. Also, you cannot
    update the table that the trigger is firing on. If I understand correctly, you want to copy the value of
    quote_status_id into attribute6 whenever a row is inserted or updated. If that is correct, then you only need a simple trigger like:
    create trigger trigg
    before insert or update of aso_quote_headers_all
    for each row
    begin
    :new.attribute6 = :new.quote_status_id;
    end;Thought why anybody would want to do this is beyond me - you already have the info
    in your attribute6 column - what's the point in simply copying it to another column in
    the same table?
    Paul...
    John

  • How can I find number of columns in a table using a query

    Hi
    I want to find the number of columns in a table using a query.
    Any help in this regard is much appreciated.
    Cheers

    Hi,
    This is the output i get when i executed ur query
    OWNER     OBJECT_NAME     OBJECT_TYPE
    SMLDBO     T2311_SURVEY_QUESTIONS     TABLE
    select OWNER,OBJECT_NAME,OBJECT_TYPE
    from all_objects
    where OBJECT_NAME = 'T2311_SURVEY_QUESTIONS';
    works Fine.
    But if i use the below query it returns : 0
    SELECT count(*) FROM user_tab_columns
    WHERE table_name =upper( 'T2311_SURVEY_QUESTIONS');
    What could be problem.
    The table exists + name of the table is correct + it resides in my schema itself.
    Any guess !!!

  • Select sql running very long when made to select many columns from a table

    Hi,
    I am using an Oracle DB 10g. I have a table with 250 columns and I need to select 200 columns out of them. The table holds around 0.1 million rows. But when I run the select sql it takes 15 mins to return .1 million rows. Where as if I select only 10 or 15 columns the sql runs in less than a minute returning same number of rows. My sql looks like below:
    select p.col1,p.col2,.......,p.col200
    from table Parent p;
    The table also has a Primary key Index but it does not seem to be using it even when I forced an Index hint. Could you pls help?

    961796 wrote:
    I am using an Oracle DB 10g. I have a table with 250 columns and I need to select 200 columns out of them. The table holds around 0.1 million rows. But when I run the select sql it takes 15 mins to return .1 million rows. Where as if I select only 10 or 15 columns the sql runs in less than a minute returning same number of rows. My sql looks like below:
    As Sven points out, it is likely that most of your time is network (and client) time. You are sending 20 times as much data (based on column counts, at any rate) across the network, and concerned that it's taking 15 times as long.
    If you're testing from SQL*Plus then setting the arraysize to a value larger than the default might help.
    If you have control over the SQL*Net settings then you may get some benefit by adjusting the SDU sizes at both ends of the link.
    If you have control over the tcp configuration (transmit and receive buffer sizes) then you may get some benefit by adjusting these.
    Simple test, by the way, if you're on SQL*Plus
    set autotrace traceonly statistics
    select ...This will dump your data in the bit bucket as it arrives giving you
    a) the database time plus network time
    b) some statistics including the volume of data down the network and the number of network round-trips that Oracle saw.
    Regards
    Jonathan Lewis
    http://jonathanlewis.wordpress.com
    Author: <b><em>Oracle Core</em></b>

  • Compare two records column by column in same table

    Hi All,
    I have an address table with ID as the PK but there can be more than one ID. I need to take the latest address record and first see if that same ID has a previous address record and if so, compare each column to see if the data has changed. I only need to compare the latest record with the next latest record.
    I cannot figure this out at all. I can of course use the MAX function to get the latest address records but do not know how I can pull the next latest address record that matches the ID and compare each column.
    Sample table:
    ID street city state zip effective_date
    1 123 main chicago IL 60111 3-7-2012
    2 34 N 13th new york NY 18374 3-7-2012
    3 15 N main Dallas TX 47389 3-7-2012
    1 89 N main chicago IL 60111 1-5-2012
    1 16 East St columbus OH 47382 12-10-2011
    2 34 N 13th new york NY 18374 10-7-2011
    2 15 S Elm new york NY 18374 09-1-2011
    3 15 N main Dallas TX 47389 10-4-2011
    SO...in the table above using today as the latest record date, based of my criteria I would only want to pull record ID 1 and 2 since the next most recent record for those IDs have had at least one of their address data change. ID 3 has exactly the same address data so I would not consider that in my criteria.
    Can i do this with SQL only or will i need to create a procedure or function?
    Any help is appreciated
    Ben

    Ben C wrote:
    Hi All,
    I have an address table with ID as the PK but there can be more than one ID. You mean there can be more than one row with tha same ID, right? (There can be more than one ID, also.)
    I need to take the latest address record and first see if that same ID has a previous address record and if so, compare each column to see if the data has changed. I only need to compare the latest record with the next latest record.
    ... Sample table:Whenever you have a problem, please post CREATE TABLE and INSERT statements for the sample data, and the exact results you want from that data.
    Explain, using specific examples, how you get those results from that data.
    Always say which version of Oracle you're using.
    Can i do this with SQL only or will i need to create a procedure or function?You don't need PL/SQL, if I understand your requirements. You can do something like this:
    WITH     got_analytics     AS
         SELECT     id, street, city, state, zip, effective_date
         ,     COUNT (*)     OVER ( PARTITION BY  id )          AS cnt
         ,     ROW_NUMBER () OVER ( PARTITION BY  id
                                   ORDER BY          effective_date     DESC
                           )                           AS r_num
         FROM    table_x
    --     WHERE     ...     -- If you need any filtering, put it here
    SELECT    id, street, city, state, zip, effective_date
    FROM       got_analytics
    WHERE       cnt     > 1
    AND       r_num     <= 2
    ORDER BY  id
    ,            effective_date
    ;This will show the most recent 2 rows for each id that has 2 (or more) rows.
    You can modify it to only include ids where there was some change in street, city, state or zip between those 2 rows.
    WITH     got_analytics     AS
         SELECT     id, street, city, state, zip, effective_date
         ,     COUNT (*)     OVER ( PARTITION BY  id )          AS cnt
         ,     ROW_NUMBER () OVER ( PARTITION BY  id
                                   ORDER BY          effective_date     DESC
                           )                           AS r_num
         FROM    table_x
    --     WHERE     ...     -- If you need any filtering, put it here
    ,     got_unique_cnts     AS
         SELECT     id, street, city, state, zip, effective_date
         ,     COUNT (DISTINCT '~' || street) OVER (PARTITION BY id)     AS street_cnt
         ,     COUNT (DISTINCT '~' || city)   OVER (PARTITION BY id)     AS city_cnt
         ,     COUNT (DISTINCT '~' || state)  OVER (PARTITION BY id)     AS state_cnt
         ,     COUNT (DISTINCT '~' || zip)    OVER (PARTITION BY id)     AS zip_cnt
         FROM       got_analytics
         WHERE       cnt     > 1
         AND       r_num     <= 2
    SELECT    id, street, city, state, zip, effective_date
    FROM       got_unique_cnts
    WHERE       street_cnt     > 1
    OR       city_cnt     > 1
    OR       state_cnt     > 1
    OR       zip_cnt     > 1
    ORDER BY  id
    ,            effective_date
    ;This assumes that street, city, state and zip are all VARCHAR2s. If any of those columns is NULL on one row, and not NULL on the other row for that ID, that counts as a difference.
    Edited by: Frank Kulash on Mar 7, 2012 3:37 PM
    Edited by: Frank Kulash on Mar 7, 2012 3:41 PM

  • Create ViewCriteria comparing two columns from same table

    Does anyone know how I can create a ViewCriteria where clause that compares two columns from the same table?
    For example if I had two integer columns (MINSAL and MAXSAL) and wanted to see if they are equal. I would normally do the following SQL below.
    SELECT * FROM EMPL
    WHERE MINSAL = MAXSAL

    It works, but it is not ideal.
    Setup a Transient column that performs a groovy evaluation of MINSAL=MAXSAL and then my ViewCriteria evaluates the column to true and I set Query Execution Mode to Both.

  • Select the column details for table in one column as varchar2(32000)

    Oracle Version: ORACLE 10G R 2
    Problem : Select the column details for some tables and want to store the same in VARCHAR2(32000) separated by ||
    Example:
    TABLE_NAME COLUMN_NAME DATA_TYPE
    EMP EMPNO NUMBER
    EMP ENAME VARCHAR2
    EMP JOB VARCHAR2
    EMP MGR NUMBER
    EMP HIREDATE DATE
    EMP SAL NUMBER
    EMP COMM NUMBER
    EMP DEPTNO NUMBER
    EMP RN NUMBER
    Want to store
    COLUMNS (empno||ename||job||mgr||hiredate||sal||comm||deptno||rn);

    SQL> var cur refcursor
    SQL> declare
    ctx number;
    begin
    ctx := dbms_xmlgen.newcontext('select * from emp');
    dbms_xmlgen.setNullHandling(ctx, dbms_xmlgen.empty_tag);
    open :cur for select * from xmltable('for $i in ROW return string-join($i/*, "||")' passing dbms_xmlgen.getxmltype(ctx).extract('ROWSET/ROW'));
    end;
    PL/SQL procedure successfully completed.
    SQL> print cur
    COLUMN_VALUE                                                                                       
    7369||SMITH||CLERK||7902||17.12.1980 00:00:00||800||||20                                           
    7499||ALLEN||SALESMAN||7698||20.02.1981 00:00:00||1600||300||30                                    
    7521||WARD||SALESMAN||7698||22.02.1981 00:00:00||1250||500||30                                     
    7566||JONES||MANAGER||7839||02.04.1981 00:00:00||2975||||20                                        
    7654||MARTIN||SALESMAN||7698||28.09.1981 00:00:00||1250||1400||30                                  
    7698||BLAKE||MANAGER||7839||01.05.1981 00:00:00||2850||||30                                        
    7782||CLARK||MANAGER||7839||09.06.1981 00:00:00||2450||||10                                        
    7788||SCOTT||ANALYST||7566||19.04.1987 00:00:00||3000||||20                                        
    7839||KING||PRESIDENT||||17.11.1981 00:00:00||5000||||10                                           
    7844||TURNER||SALESMAN||7698||08.09.1981 00:00:00||1500||0||30                                     
    7876||ADAMS||CLERK||7788||23.05.1987 00:00:00||1100||||20                                          
    7900||JAMES||CLERK||7698||03.12.1981 00:00:00||950||||30                                           
    7902||FORD||ANALYST||7566||03.12.1981 00:00:00||3000||||20                                         
    7934||MILLER||CLERK||7782||23.01.1982 00:00:00||1300||||10                                         
    14 rows selected.

  • How to get the selected rows & columns in the table?

    hi everybody,
                         In my application the table is kept inside the event structure.I select the cells  in the table (using mouse) on running time.How to get the selected number of rows & columns in that table?

    Hello,
    You can fill selected values of the table by writing to it or the corresponding property using a property node - the table is just a 2D array of strings.  I think for your "disable" question you are referring to the shortcut menu (when you right click).  If you are using LabVIEW 8.x, you can edit or disable that shortcut menu - just right click on your table at edit time and choose Advanced >> Run-Time Shortcut Menu.
    Best Regards,
    JLS
    Best,
    JLS
    Sixclear

  • How do I select rows from the same table that have multiple occurances

    Hi Everybody,
    I am trying to select records from a detail table by grouping it. The table has more than 1 million records and the query is not performing well. The basic question is how to select a distinct record from a table which matches all values in one column and any in the other.
    desc SCV
    ID NUMBER PK (ID + SCRID)
    SCRID NUMBER FK(SC)
    ID SCRID
    1 1
    2 1
    3 1
    4 2
    5 2
    6 3
    7 4
    8 4
    desc PROJECTS
    ID NUMBER PK
    NAME VARCHAR2(100)
    ID NAME
    1 PROJECT1
    2 PROJECT2
    3 PROJECT3
    4 PROJECT4
    desc PJS
    ID NUMBER
    PROID NUMBER FK (PROJECTS)
    SCRID NUMBER FK (SCV(SCRID + SCVID)
    SCVID NUMBER
    ID PROID SCRID SCVID
    1 1 1 1
    2 1 1 2
    3 1 2 5
    4 1 3 6
    5 1 4 7
    6 2 1 3
    7 2 2 4
    8 2 2 5
    9 2 4 7
    There are over 1 million records in PJS.
    desc TBP
    SCRID NUMBER
    SCVID NUMBER
    SCRID SCVID
    1 1
    1 2
    1 3
    2 4
    2 5
    3 6
    4 7
    4 8
    The requirement is to select projects that have matching SCRID, SCVID from TBP such that
    all distinct SCRID should match and within that and any SCVID match will do. (A "AND" between each SCRID and an "OR" for each SCVID in that SCRID like 'SCRID = 1 AND (SCVID = 1 OR SCVID = 2 OR SCVID = 3) AND SCRID = 2 AND (SCVID =....)
    So, for the sample data it should return us PROID = 1
    I have few queries written for this:
    SELECT PROID FROM PJS,TBP WHERE TBP.SCVID = PJS.SCVID AND TBP.SCRID = 1
    INTERSECT
    SELECT PROID FROM PJS,TBP WHERE TBP.SCVID = PJS.SCVID AND TBP.SCRID = 2
    INTERSECT
    SELECT PROID FROM PJS,TBP WHERE TBP.SCVID = PJS.SCVID AND TBP.SCRID = 3
    INTERSECT
    SELECT PROID FROM PJS,TBP WHERE TBP.SCVID = PJS.SCVID AND TBP.SCRID = 4
    This query performs well but the cost is very high, hardcoding, sorting.
    The 2nd option is to:
    SELECT pjs.PROID proid
    FROM TBP tbp,
    PJS pjs
    WHERE pjs.SCVID = tbp.SCVID
    AND pjs.SCRID = tbp.SCRID
    GROUP BY pjs.PROID
    HAVING COUNT(DISTINCT pjs.SCRID) = (SELECT COUNT(DISTINCT SCRID ) FROM TBP)
    This has a low cost but runs slowly.
    One more way I tried was with the IN operator like
    SELECT DISTINCT PROID FROM PJS A,TBP T WHERE T.SCRID = 1 AND T.SCVID = A.SCVID
    AND PROID IN (SELECT PROID FROM PJS A,TBP T WHERE T.SCRID = 2 AND T.SCVID = A.SCVID
    AND PROID IN (...SO ON with each DISTINCT SCRID.
    Again this involves too much of sorting.
    Any help will be appriciated.
    Thanks in advance.

    Hi Andrew,
    Use DELETE t_itab statement inside the loop.
    I have modified your code. It is perfectly working.See bellow  -
    LOOP AT it_zmember01 INTO wa_zmember01.
    WRITE: / wa_zmember01-mnumber UNDER 'NUMBER',
    wa_zmember01-mname UNDER 'NAME',
    wa_zmember01-mdob UNDER 'DOB'.
    WRITE / '-----------------------------------------------------------------'.
    DELETE it_zmember01.               " Modified
    ENDLOOP.
    DELETE it_zmember01. statement inside the loop will delete the current row of the table.
    Regards
    Pinaki

  • Want to update a column with  different column of same table

    I have a table TEST_CDR which is having 250 millions records also having partition by hash.
    there are two column in this table ID and CDR_ID want to update ID in CDR_ID but it take too much time like 24 hours or more.
    please suggest me a simple way.

    Hi,
    You can try this. but be careful about your backups and go through CTAS,nologging and parallel concepts...
    Please refer this link.
    http://www.dba-oracle.com/t_fast_batch_updates_ctas.htm
    http://www.dba-oracle.com/t_nologging_append.htm
    http://www.dba-oracle.com/t_efficient_update_sql_dml_tips.htm
    http://eriglen.blogspot.in/2011/08/ctas-create-table-as-select-with.html
    1.CTAS with nologging and parallel..
    SQL> create table test_cdr_1(id,cdr_id) nologging parallel as select id,id from test_cdr;
    2.rebuild indexes, triggers and constraints to test_cdr_1
    3.rename test_cdr to bkup_test_cdr;
    4.rename test_cdr_1 to test_cdr;
    Regards,
    ragunath.

  • Selecting only columns from a table

    Could someone help me with a SQL query to select all the columns in a table as an array?

    Hello
    if you are on 9i upward, you can define a pl/sql table of the rowtype and use bulk collect to populate that table, otherwise, you can create separate array types for each column:
    create table dt_test_tab (id number, col1 varchar2(10));
    insert into dt_test_tab values(1, 'X');
    insert into dt_test_tab values(2, 'X');
    insert into dt_test_tab values(3, 'X');
    insert into dt_test_tab values(4, 'X');
    insert into dt_test_tab values(5, 'X');
    commit;
    declare
        type tt_TestTab is table of dt_test_tab%rowtype index by binary_integer;
        type tt_TestTabid IS TABLE OF dt_test_tab.id%type index by binary_integer;
        type tt_TestTabcol1 IS TABLE OF dt_test_tab.col1%type index by binary_integer;
        lt_TestTab     tt_TestTab;
        lt_Id          tt_TestTabId;
        lt_col1        tt_TestTabcol1;
    begin
        --use the rowtype array
        select
             id,
             col1
        BULK COLLECT INTO
             lt_testtab
        FROM
            dt_test_tab;
       --use the individual column arrays
        select
             id,
             col1
        BULK COLLECT INTO
             lt_id,
             lt_col1
        FROM
            dt_test_tab;
        dbms_output.put_line('loop through the rowtype array');
        FOR x in lt_TestTab.FIRST..lt_TestTab.LAST LOOP
           dbms_output.put_line(to_char(lt_TestTab(x).id)||to_char(lt_TestTab(x).col1));
        end loop;
        dbms_output.put_line('loop through the two column arrays');
        FOR x in lt_Id.FIRST..lt_Id.LAST LOOP
           dbms_output.put_line(to_char(lt_Id(x))||to_char(lt_Col1(x)));
        end loop;
    END;

  • Re: Getting the current selected Row & column in adf table

    Hi,
    Our requirement is we have to retrieve both the current row and column in adf table.
    We can retrieve the current row in the backing bean like
    ViewObject view = getViewObject();
    view.getCurrentRow();
    Is it possible to retrieve selected column id or column header text as we are getting for row?
    please give some sample.
    Thanks,
    Vijay.
    Edited by: Vijayakumar Palanisamy on Jun 23, 2011 2:23 AM

    Hi John
    My exact requirement,
    1.     We have collection of records in adf table
    2.     Each cell in the row has commandImageLink (Like adding approver, edit approver, delete etc)
    Currently it’s working fine for me in the commandImageLink listener, but the problem is table is refreshed 2 time,
    while selecting a record and commandImageLink is clicked, I have to avoid 2 times refreshing,
    If it’s possible to identify a particular cell in the row selection, I can avoid the image link listener.
    we are using Jdeveloper11.1.1.4.0
    Please suggest.
    Thanks,
    Vijay

  • Selecting a column in a table according to a value

    Hi,
    I am stuck with a little problem here. I need to fill a column according to a value. I have an xml that will looks like this to make it simple. (Don't worry, I am not managing the status of a stock of Candy ;-))
    I have to build in rtf.
    <?xml version="1.0" encoding="UTF-8" ?>
    - <ROWSET>
    - <ROW>
    <CANDY>Bubble gum</CANDY>
    <SUGAR>SUGARFREE</SUGAR>
    </ROW>
    - <ROW>
    <CANDY>Cinnamon candy</CANDY>
    <SUGAR>SUGARFREE</SUGAR>
    </ROW>
    - <ROW>
    <CANDY>Gummy candy</CANDY>
    <SUGAR>SUGAR</SUGAR>
    </ROW>
    </ROWSET>
    I have a table with two columns. I need to put on the left column the "SUGARFREE" candy and on the right the "SUGAR" candy.
    Is there any condition to make this happen. Or do I have to make two table of one column next to the other and adapt a if condition in the for-each.
    best regards,
    Olivier

    Have the same table structure with two columns and have for-each with conditions in each of your columns.
    Say in column1: <?for-each:ROW[SUGAR='SUGARFREE']?>text to print <?end for-each?>
    column2: <?for-each:ROW[SUGAR='SUGAR']?>text to print <?end for-each?>
    Hope this is what you want

  • UPDATE involving the same table in sub query

    DB version: 11.2
    We have a table called SHP_GC_TRACK which has around 8 million records with partitions . In the below UPDATE, it is updating a column based on the SELECT on the same table in a subquery.
    UPDATE shp_gc_track a
       SET f_tran_proc  = 'Y'
    WHERE last_update_date <
              (SELECT MAX (last_update_date)
                 FROM shp_gc_track b
                WHERE a.shp_trx_rowid = b.shp_trx_rowid
                  AND a.c_shp_inst = b.c_shp_inst
                  AND a.f_tran_proc  = b.f_tran_proc
                  AND b.f_ltr_received = 'D'
                  AND f_rec_code IN ('G', 'W')
                  AND b.f_rec_status = 'B'
                  AND b.c_shp_inst = :b1
       AND a.c_shp_inst = :b1
       AND a.f_ltr_received = 'D'             -----------------> part of composite index
       AND a.f_tran_proc  = 'N'              -----------------> part of composite index
       AND a.f_rec_code IN ('G', 'W')      --------------> part of composite index 
       AND a.f_rec_status = 'B';              -----------------> part of composite index  This UPDATE takes a long time to execute and sometime get hung.
    We have a composite index on four columns f_ltr_received, f_rec_code, f_rec_status, f_tran_proc . Explain plan shows this composite index is being used.
    Is there anyway to rewrite this query or any other suggestions ?

    Steve_74 wrote:
    DB version: 11.2
    We have a table called SHP_GC_TRACK which has around 8 million records with partitions . In the below UPDATE, it is updating a column based on the SELECT on the same table in a subquery.
    UPDATE shp_gc_track a
    SET f_tran_proc  = 'Y'
    WHERE last_update_date <
    (SELECT MAX (last_update_date)
    FROM shp_gc_track b
    WHERE a.shp_trx_rowid = b.shp_trx_rowid
    AND a.c_shp_inst = b.c_shp_inst
    AND a.f_tran_proc  = b.f_tran_proc
    AND b.f_ltr_received = 'D'
    AND f_rec_code IN ('G', 'W')
    AND b.f_rec_status = 'B'
    AND b.c_shp_inst = :b1
    AND a.c_shp_inst = :b1
    AND a.f_ltr_received = 'D'             -----------------> part of composite index
    AND a.f_tran_proc  = 'N'              -----------------> part of composite index
    AND a.f_rec_code IN ('G', 'W')      --------------> part of composite index 
    AND a.f_rec_status = 'B';              -----------------> part of composite index  This UPDATE takes a long time to execute and sometime get hung.
    We have a composite index on four columns f_ltr_received, f_rec_code, f_rec_status, f_tran_proc . Explain plan shows this composite index is being used.
    Is there anyway to rewrite this query or any other suggestions ?Tuning updates with subqueries can be hard :(. Sadly my suggestions below are of the try-it-and-see-what-happens variety - nothing certain
    First, check the index. Is it bitmap or b-tree? If b-tree see if the most restrictive columns are listed first - this can help with b-tree index efficiency. Also if b-tree a composite bitmap for columns with lots of repeating values instead might help
    Its a correlated subquery so you can't just run the subquery first putting the result into a scalar varaiable and using the variable in the SQL instead. You can try putting the results of the subuqery w/join keys in a GTT first using the GTT in the SQL to see if I/O is reduced overall during both operations.
    Do you have the licence for the parallel query option? Using parallel DML (this must be turned on manually) might help. Check the docs for the ALTER SESSION command to do this. Also, the PARALLEL_INDEX() hint might help
    Post the execution plan of the SQL

  • Cost of using subquery vs using same table twice in query

    Hi all,
    In a current project, I was asked by my supervisor what is the cost difference between the following two methods. First method is using a subquery to get the name field from table2. A subquery is needed because it requires the field sa_id from table1. The second method is using table2 again under a different alias to obtain table2.name. The two table2 are not self-joined. The outcome of these two queries are the same.
    Using subquery:
    select a.sa_id R1, b.other_field R2,
    (select b.name from b
    where b.b_id = a.sa_id) R3
    from table1 a, table2 b
    where ...Using same table twice (table2 under 2 different aliases)
    select a.sa_id R1, b.other_field R2, c.name R3
    from table1 a, table2 b, table2 c
    where
    c.b_id = a.sa_id,
    and ....Can anyone tell me which version is better and why? (or under what circumstances, which version is better). And what are the costs involved? Many thanks.

    pl/sql novice wrote:
    Hi all,
    In a current project, I was asked by my supervisor what is the cost difference between the following two methods. First method is using a subquery to get the name field from table2. A subquery is needed because it requires the field sa_id from table1. The second method is using table2 again under a different alias to obtain table2.name. The two table2 are not self-joined. The outcome of these two queries are the same.
    Using subquery:
    Using same table twice (table2 under 2 different aliases)
    Can anyone tell me which version is better and why? (or under what circumstances, which version is better). And what are the costs involved? Many thanks.In theory, if you use the scalar "subquery" approach, the correlated subquery needs to be executed for each row of your result set. Depending on how efficient the subquery is performed this could require significant resources, since you have that recursive SQL that needs to be executed for each row.
    The "join" approach needs to read the table only twice, may be it can even use an indexed access path. So in theory the join approach should perform better in most cases.
    Now the Oracle runtime engine (since Version 8) introduces a feature called "filter optimization" that also applies to correlated scalar subqueries. Basically it works like an in-memory hash table that caches the (hashed) input values to the (deterministic) correlated subquery and the corresponding output values. The number of entries of the hash table is fixed until 9i (256 entries) whereas in 10g it is controlled by a internal parameter that determines the size of the table (and therefore can hold different number of entries depending on the size of each element).
    If the input value of the next row corresponds to the input value of the previous row then this optimization returns immediately the corresponding output value without any further action. If the input value can be found in the hash table, the corresponding output value is returned, otherwise execute the query and keep the result combination and eventually attempt to store this new combination in the hash table, but if a hash collision occurs the combination will be discarded.
    So the effectiveness of this clever optimization largely depends on three different factors: The order of the input values (because as long as the input value doesn't change the corresponding output value will be returned immediately without any further action required), the number of distinct input values and finally the rate of hash collisions that might occur when attempting to store a combination in the in-memory hash table.
    In summary unfortunately you can't really tell how good this optimization is going to work at runtime and therefore can't be properly reflected in the execution plan.
    You need to test both approaches individually because in the optimal case the optimization of the scalar subquery will be superior to the join approach, but it could also well be the other around, depending on the factors mentioned.
    Regards,
    Randolf
    Oracle related stuff blog:
    http://oracle-randolf.blogspot.com/
    SQLTools++ for Oracle (Open source Oracle GUI for Windows):
    http://www.sqltools-plusplus.org:7676/
    http://sourceforge.net/projects/sqlt-pp/

Maybe you are looking for