Complex SQL Query in BPEL DB Adapter

Hi,
Is it possbile to write a complex query in BPEL DB Adapter using "Custom SQL Query"?
I would like to write an IF ELSE condition in the DB Adapter similar what is given below..
IF((SELECT COUNT(*) FROM F5898001 WHERE CT58SRCNME = 'CA_TEST' AND CTJOBNAME = '12345' AND CTEDBT = 'EDBT' AND CTEDSP = 'B') < 1)
BEGIN
insert into f5898001 (CTJOBNAME,CTEDSP,CTEDBT,CT58SRCNME) VALUES (#jobname, #edsp, #edbt, #srcnme)
END.

In a single pass no.
You could use a DB link to perform select and return result to BPEL process variable. Put a switch decision depending on result in variable that calls a DB Adapter to perform the insert.
Wouldn't be in a single transaction and not very elegant, but might be a way round.

Similar Messages

  • Complex sql query

    Hello,
    My question is: I would like to do more complex sql query ( i need to use GROUP BY, HAVING, ORDER BY). Is possible do it with CMP entity bean, or i have to use BMP entity bean or Session bean? Query return about 20-30 items. Can you recommend some design pattern for this situation?
    Thanks in advance
    Daniel H

    Hello,
    My question is: I would like to do more complex sql query ( i need to use GROUP BY, HAVING, ORDER BY). Is possible do it with CMP entity bean, or i have to use BMP entity bean or Session bean? Query return about 20-30 items. Can you recommend some design pattern for this situation?
    Thanks in advance
    Daniel H

  • How to set fetchsize of sql Query when using Database Adapter.

    Hi All,
    I am using DatabaseAdapter to connect to database and retriving huge amount of data.For improvement in the performance I want to set the "fetchsize" of sql query. I know fetchsize can be preset in Java using Jdbc 2.0 API.Please let me know how to set this value in BPEL when using DBAdapter?
    Thanks
    Chandra

    I talked to the developer of the db adapter - and he told me this feature will be available in BPEL PM 10.1.3 (which is supposed to be production later this year, and a public beta soon). If this is an emergency I would recommend going throug Oracle support and have them file an enhancement for 10.1.2.0.2
    hth clemens

  • SQLEception using Complex SQL Query with Java Studio Creator2 Build(060120)

    I am evaluating Java Studio Creator2 for a WEB base application project that will be making SQL queries to an Oracle Database but I have stumble into a problem using complex SQL queries.
    I am getting an SQLException "org.apache.jasper.JasperException: java.lang.RuntimeException: java.sql.SQLException: [sunm][Oracle JDBC Driver][Oracle]ORA-00923: FROM keyword not found where expected". I looks like it cut my sql.
    The SQL that I am trying to execute is
    Select part_name
    from table1, table2
    where table1.part_nbr = table2.part_nbr
    and table2.row_add_dt = (select max(table3.row_add_dt)
    from table3
    where table3.ser_part_id =table2.ser_part_id)
    This is a valid query that it is using 2 different selects to get a part number.
    If posible, point me to the best solution were I will be able to make complex SQL queries like the one above and bigger.
    Is there any way that I can read an SQL query file instead of inserting the SQL query string into the setCommand()?

    I have read that document looking for some anwsers on how to make this kind of query. If I try the query that I have above in the query editor ,the query editor will cut off from the last select that is between ().
    I beleave, there is a work around using the inner joint or outter join command. I will try them to see If I get the corrent result. If not, then I have to keep on asking for possible solutions.
    Anyway, someone in the Creator Team should take a note in adding something like a special criteria in the Add Query Criteria Box for cases like the one I have. The special criteria will be like using another select/from/where to get some result that will be compare.
    Girish, Are you in the Sun Creator Team?

  • Obtain rows from a complex sql query

    Hi to all, please somebody who can help me, my scenario is as next:
    One table like next
    Reservation (table)
    Boat (string)
    reservation (date)
    class (string)
    room1_cod (int)
    room1 (int)
    room2_cod (int)
    room2 (int)
    room3_cod (int)
    room3 (int)
    In room(x)_cod , I am saving a client code, in the room(x) , I am saving the price of the room for that client.
    example:
    LUSITANIA, 2014-3-1, MEDIUM, 0, 0, 145, 345, 0, 0    = client 145 ocupied room 2 paying $345
    LUSITANIA, 2014-2-1, MEDIUM, 145, 345, 0, 0, 0, 0    = client 145 ocupied room 1 paying $345
    LUSITANIA, 2014-1-1, MEDIUM, 0, 0, 0, 0, 145, 345    = client 145 ocupied room 3 paying $345
    how would like to make an sql query for filter :
    (Boat, reservation, class... with a reservation start date and finish date and with an specific "client code" find and filter this code into room1_cod , room2_cod and room3_cod .... and maybe do a temporarly table with rows containing (Boat, Reservation,
    Class, "client_code" , "client_pay" )
    Resulting LIKE:
    LUSITANIA, 2014-3-1, MEDIUM, 145, 345
    LUSITANIA, 2014-2-1, MEDIUM, 145, 345
    LUSITANIA, 2014-1-1, MEDIUM, 145, 345
    Please I need your guidance. TKS

    Please take a look
    SELECT Boat, reservation, class,
    case
    when room1_cod>0 then 1
    when room2_cod>0 then 2
    when room3_cod>0 then 3
    end as Room,
    case
    when room1_cod>0 then room1_cod
    when room2_cod>0 then room2_cod
    when room3_cod>0 then room3_cod
    end as client_code,
    case
    when room1>0 then room1
    when room2>0 then room2
    when room3>0 then room3
    end as client_pay
    from
    SELECT 'LUSITANIA' AS Boat, CONVERT(DATE,'2014-3-1',121) AS reservation, 'MEDIUM' AS class, 0 AS room1_cod, 0 AS room1, 145 AS room2_cod, 345 AS room2, 0 AS room3_cod, 0 AS room3 ---//client 145 ocupied room 2 paying $345
    union all
    SELECT 'LUSITANIA' AS Boat, CONVERT(DATE,'2014-2-1',121) AS reservation, 'MEDIUM' AS class, 145 AS room1_cod, 345 AS room1, 0 AS room2_cod, 0 AS room2, 0 AS room3_cod, 0 AS room3 --//= client 145 ocupied room 1 paying $345
    union all
    SELECT 'LUSITANIA' AS Boat, CONVERT(DATE,'2014-1-1',121) AS reservation, 'MEDIUM' AS class, 0 AS room1_cod, 0 AS room1, 0 AS room2_cod, 0 AS room2, 145 AS room3_cod, 345 AS room3 --//= client 145 ocupied room 3 paying $345
    ) as Reservation
    ----Better table design would eliminate multiple columns for each room/client code/rate like the following
    SELECT
    Boat, reservation, class,
    Room,
    room_cod as client_code,
    room_rate as client_pay
    from
    SELECT 'LUSITANIA' AS Boat, CONVERT(DATE,'2014-3-1',121) AS reservation, 'MEDIUM' AS class, 2 AS room, 145 AS room_cod, 345 AS room_rate ---//client 145 ocupied room 2 paying $345
    union all
    SELECT 'LUSITANIA' AS Boat, CONVERT(DATE,'2014-2-1',121) AS reservation, 'MEDIUM' AS class, 1 AS room, 145 AS room_cod, 345 AS room_rate --//= client 145 ocupied room 1 paying $345
    union all
    SELECT 'LUSITANIA' AS Boat, CONVERT(DATE,'2014-1-1',121) AS reservation, 'MEDIUM' AS class, 3 AS room, 145 AS room_cod, 345 AS room_rate --//= client 145 ocupied room 3 paying $345
    ) as Reservation
    thanks for you effort, best regards

  • How to bind/pass multiple input parameters to sql query in BPEL Process

    Hi,
    I have a requirement to invoke a query on a database table to fetch the records based on the few input values or bind variables?
    I have created a Partner link using the DBAdapter service and have used custom SQL, how can i bind and pass multiple bind/input parameters for these queries.
    foreg: if i have to query employee records based on name , desgination and age, these three params would be my input parameter that i will pass when i invoke the BPEL process, bow how can i pass these parameters to the partner link using DBADAPTER service.
    Please help
    Regards

    If the parameter-value is not known beforehand and cannot be determined based on the process instance data at that time, there are 2 options. Either invoke the PL/SQL procedure you named using a DB adapter service to get the value and pass it to the other DB adapter service. Or create a single service (e.g. PL/SQL procedure) that determines the parameter value, executes the query and returns the result. However, this is more a design issue. Does it makes sense to combine these actions in a separate service and use it or are these actions not related? You furthermore state that additional (DB adapter) services make the process heavier. Do you have strict requirements on the time process-instances may run or other such requirements? If not, I think the design-choice on whether combining these actions in a single service should have more priority.

  • Complex SQL query return type

    I am trying to take a complex query that I was running inside of C# code and turn it into a stored proc so that it performs much better. I was doing quite a bit of looping in C# code because I am most familiar with that but I know it ain't the best way to do things. Basically my query does the following:
    -Query a table to get a set of table names
    -loop over that set of table names to query another table to get some info
    -use that info to query another table to find out if there are any results
    -if there are results add those to a table to be returned to the caller
    The issue I am having is that I don't know how to build the return table so I can pass it back to the calling code. All i need to know is the names of the tables that had at least one hit in my big query.
    I am kind of new to this so I may have some things in here that are completely bizarre. I won't be offended if you rip it apart. I know that my out SYS_REFCURSOR will not work as is. The part that says 'SELECT OBJECTID really needs to be telling me the name of the table and not OBJECTID. I want to jam all these table names into a resultset which would really just be a temp table that I create with one field (TableName).
    I know it's a lot to ask but if anyone can look at any part of this and give me suggestions I would greatly appreciate it. I'm struggling slowly through PL/SQL trying not to do dumb things when there are easier ways. There is probably a way for me to get rid of this loop but I think I need the dynamic part because I am querying different tables. Thank you very much
    CREATE OR REPLACE PROCEDURE "CREAM_USER"."ENVELOPE_INTERSECT"
    pMINX IN NUMBER,
    pMAXX IN NUMBER,
    pMAXY IN NUMBER,
    pMINY IN NUMBER,
    pRESULTSET OUT SYS_REFCURSOR
    AS
    BEGIN
    DECLARE
    SQLSTR VARCHAR2(700);
    theLayerID NUMBER;
    cursor gi is SELECT TABLE_NAME, OWNER FROM VIEW_GEOINDEXER_TABLES WHERE GEOINDEXED = 1;
    theTABLE_NAME varchar2(160);
    qTheTABLE_NAME varchar2(160);
    theOWNER varchar2(32);
    BEGIN
    for iLayer in gi loop
    theTABLE_NAME := iLayer.TABLE_NAME;
    theOWNER := iLayer.OWNER;
    qTheTABLE_NAME := '''' || theTABLE_NAME || '''';
    theOWNER := '''' || theOWNER || '''';
    SQLSTR := 'SELECT LAYER_ID FROM SDE.LAYERS WHERE OWNER = ' || theOWNER || ' AND TABLE_NAME = ' || qTheTABLE_NAME || '';
    EXECUTE IMMEDIATE SQLSTR INTO theLAYERID;
    SQLSTR := 'SELECT OBJECTID FROM ' || theTABLE_NAME || ' WHERE SHAPE IN
    SELECT FID FROM F' || theLayerID || ' WHERE
    (' || pMINX || ' >= EMINX AND ' || pMINX || ' <= EMAXX)
    OR
    (' || pMAXX || ' >= EMINX AND ' || pMAXX || ' <= EMAXX)
    AND
    (' || pMINY || ' >= EMINY AND ' || pMINY || ' <= EMAXY)
    OR
    (' || pMAXY || ' >= EMINY AND ' || pMAXY || ' <= EMAXY)
    OR
    (' || pMINX || ' <= EMINX AND ' || pMAXX || ' >= EMAXX)
    AND
    (' || pMINY || ' <= EMINY AND ' || pMAXY || ' >= EMAXY)
    OPEN pRESULTSET FOR SQLSTR;
    END LOOP;
    END;
    END ENVELOPE_INTERSECT;

    Looks like you are using dynamic sql but, not using bind variables.
    Check out this link (try to ignore the original question) and see how the third post shows you how to use dyanmic sql using the 'USING' construct.
    dynamic SQL
    HTH,
    Rahul.

  • Complex SQL query Tuning

    Hi Fellas,
    I am new to query tuning. I have a big query with a TKPROF details. It is just fetching 34 records and taking approx 30 mins.
    The TKPROF detail will follow after the query.
    SELECT /*+ NO_INDEX(A, RA_CUSTOMER_TRX_N11 ) USE_NL(TYPES,A) */ a.customer_trx_id CUSTOMER_TRX_ID ,
    a.trx_number TRX_NUMBER ,
    A . INTERFACE_HEADER_ATTRIBUTE5 BILLING_STAGE ,
    A . INTERFACE_HEADER_CONTEXT PBRR_TYPE ,
    A . INTERFACE_HEADER_ATTRIBUTE11 BILLING_TERM ,
    NVL ( TL . SEQUENCE_NUM , 1 ) TERM_SEQUENCE_NUMBER ,
    types.type TRX_TYPE ,
    l_types.meaning TRX_TYPE_NAME ,
    TYPES . ACCOUNTING_AFFECT_FLAG OPEN_RECEIVABLE_FLAG ,
    a.trx_date TRX_DATE , SHIP_TO_CUSTOMER_ID SHIP_TO_CUSTOMER_ID ,
    SHIP_TO_CONTACT_ID SHIP_TO_CONTACT_ID ,
    REMIT_TO_ADDRESS_ID REMIT_TO_ADDRESS_ID ,
    A . PRIMARY_SALESREP_ID PRIMARY_SALESREP_ID ,
    B . ACCOUNT_NUMBER CUSTOMER_NUMBER ,
    A . INTERNAL_NOTES INTERNAL_NOTES ,
    A . COMMENTS TRX_COMMENTS ,
    PREVIOUS_CUSTOMER_TRX_ID PREVIOUS_CUSTOMER_TRX_ID ,
    SHIP_TO_SITE_USE_ID SHIP_TO_SITE_USE_ID ,
    NVL ( PRINTING_COUNT , 0 ) PRINTING_COUNT ,
    PRINTING_ORIGINAL_DATE PRINTING_ORIGINAL_DATE ,
    PRINTING_LAST_PRINTED PRINTING_LAST_PRINTED ,
    PRINTING_PENDING PRINTING_PENDING ,
    LAST_PRINTED_SEQUENCE_NUM LAST_PRINTED_SEQUENCE_NUMBER ,
    START_DATE_COMMITMENT START_DATE_COMMITMENT ,
    END_DATE_COMMITMENT END_DATE_COMMITMENT ,
    INITIAL_CUSTOMER_TRX_ID INITIAL_CUSTOMER_TRX_ID ,
    A . INVOICE_CURRENCY_CODE INVOICE_CURRENCY_CODE ,
    A . REASON_CODE CREDIT_HEADER_REASON_CODE_CSTM ,
    A . TERM_ID TERM_ID ,
    A . SHIP_DATE_ACTUAL SHIP_DATE_ACTUAL ,
    A . SHIP_VIA SHIP_VIA ,
    A . WAYBILL_NUMBER WAYBILL_NUMBER ,
    A . PURCHASE_ORDER PURCHASE_ORDER_NUMBER ,
    A . PURCHASE_ORDER_REVISION PURCHASE_ORDER_REVISION ,
    A . PURCHASE_ORDER_DATE PURCHASE_ORDER_DATE ,
    A . CREATED_BY ORDER_CREATED_BY_CSTM ,
    P . DUE_DATE TERM_DUE_DATE_FROM_PS ,
    NVL ( TL . RELATIVE_AMOUNT , 100 ) * ( 100 / NVL ( T . BASE_AMOUNT , 100 ) ) TERM_RELATIVE_AMOUNT ,
    T . NAME TERM_NAME ,
    A . BILL_TO_CUSTOMER_ID BILL_TO_CUSTOMER_ID ,
    A . BILL_TO_CONTACT_ID BILL_TO_CONTACT_ID ,
    A . BILL_TO_SITE_USE_ID BILL_TO_SITE_USE_ID ,
    U_BILL . LOCATION BILL_TO_LOCATION ,
    SUBSTRB ( PARTY . PARTY_NAME , 1 , 50 ) BILL_CUST_NAME ,
    RTRIM ( RPAD ( LOC . ADDRESS1 , 40 ) ) BILL_ADDRESS1 ,
    RTRIM ( RPAD ( LOC . ADDRESS2 , 40 ) ) BILL_ADDRESS2 ,
    RTRIM ( RPAD ( LOC . ADDRESS3 , 40 ) ) BILL_ADDRESS3 ,
    RTRIM ( RPAD ( LOC . ADDRESS4 , 40 ) ) BILL_ADDRESS4 ,
    LOC . CITY BILL_CITY , NVL ( LOC . STATE ,
    LOC . PROVINCE ) BILL_STATE ,
    LOC . POSTAL_CODE BILL_POSTAL_CODE ,
    LOC . COUNTRY BILL_COUNTRY ,
    U_BILL . TAX_REFERENCE BILL_SITE_TAX_REFERENCE ,
    PARTY . TAX_REFERENCE BILL_CUST_TAX_REFERENCE ,
    nvl(amount_line_items_original + decode(a.initial_customer_trx_id, '', 0, nvl(com_adj.amount,0)), to_number(null)) TRX_LINE_AMOUNT ,
    nvl(tax_original, to_number(null)) TRX_TAX_AMOUNT ,
    nvl(freight_original, to_number(null)) TRX_FREIGHT_AMOUNT ,
    p.amount_due_original + decode(a.initial_customer_trx_id, '', 0, nvl(com_adj.amount,0)) TRX_ALL_AMOUNT ,
    DECODE ( : P_ORDER_BY , 'TRX_NUMBER' , A . TRX_NUMBER , 'ADJUSTMENT_NUMBER' , A.TRX_NUMBER , 'CUSTOMER' , SUBSTRB ( PARTY . PARTY_NAME , 1 , 50 ) , 'POSTAL_CODE' , LOC . POSTAL_CODE , A . TRX_NUMBER ) ORDER_BY ,
    LOC . ADDRESS1 BILL_TO_ADDRESS1 ,
    LOC . ADDRESS2 BILL_TO_ADDRESS2 ,
    LOC . ADDRESS3 BILL_TO_ADDRESS3 ,
    LOC . ADDRESS4 BILL_TO_ADDRESS4 ,
    LOC . STATE BILL_TO_STATE ,
    LOC . PROVINCE BILL_TO_PROVINCE ,
    T . description Term_Description ,
    a . org_id ,
    a . created_from
    FROM
    AR_ADJUSTMENTS COM_ADJ,
    AR_PAYMENT_SCHEDULES P,
    RA_CUST_TRX_LINE_GL_DIST REC,
    RA_CUSTOMER_TRX A,
    HZ_CUST_ACCOUNTS B,
    RA_TERMS T,
    RA_TERMS_LINES TL,
    RA_CUST_TRX_TYPES TYPES,
    AR_LOOKUPS L_TYPES,
    HZ_PARTIES      PARTY,
    HZ_CUST_ACCT_SITES A_BILL,
    HZ_PARTY_SITES PARTY_SITE,
    HZ_LOCATIONS LOC,
    HZ_CUST_SITE_USES U_BILL
    WHERE
    A.BILL_TO_CUSTOMER_ID = B.CUST_ACCOUNT_ID AND
    REC.CUSTOMER_TRX_ID = A.CUSTOMER_TRX_ID AND
    REC.LATEST_REC_FLAG = 'Y' AND
    REC.ACCOUNT_CLASS = 'REC' AND
    P.PAYMENT_SCHEDULE_ID + DECODE ( P.CLASS , 'INV' , 0 , '' ) = COM_ADJ.PAYMENT_SCHEDULE_ID (+) AND
    COM_ADJ.SUBSEQUENT_TRX_ID IS NULL AND 'C' = COM_ADJ.ADJUSTMENT_TYPE (+) AND
    A.COMPLETE_FLAG = 'Y' AND
    A.CUST_TRX_TYPE_ID = TYPES.CUST_TRX_TYPE_ID AND
    L_TYPES.LOOKUP_TYPE = 'INV/CM/ADJ' AND
    A.PRINTING_OPTION IN ( 'PRI' , 'REP' ) AND
    L_TYPES.LOOKUP_CODE = DECODE ( TYPES.TYPE , 'DEP' , 'INV' , TYPES.TYPE ) AND
    NVL ( P.TERMS_SEQUENCE_NUMBER , nvl ( TL.SEQUENCE_NUM , 0 ) ) = nvl ( TL.SEQUENCE_NUM , nvl ( p.terms_sequence_number , 0 ) ) AND
    DECODE ( P.PAYMENT_SCHEDULE_ID , '' , 0 , NVL ( T.PRINTING_LEAD_DAYS , 0 ) ) = 0 AND
    A.BILL_TO_SITE_USE_ID = U_BILL.SITE_USE_ID AND
    U_BILL.CUST_ACCT_SITE_ID = A_BILL.CUST_ACCT_SITE_ID AND
    A_BILL.party_site_id = party_site.party_site_id AND
    B.PARTY_ID = PARTY.PARTY_ID AND
    loc.location_id = party_site.location_id AND
    NVL ( P.AMOUNT_DUE_REMAINING , 0 ) <> 0 AND
    A.CUST_TRX_TYPE_ID = 2526 AND
    TYPES.TYPE = 'INV' AND
    A.TERM_ID = TL.TERM_ID (+) AND
    A.TERM_ID = T.TERM_ID (+) AND
    A.CUSTOMER_TRX_ID = P.CUSTOMER_TRX_ID (+) AND
    A.PRINTING_PENDING = 'Y' AND
    NVL ( TL.SEQUENCE_NUM , 1 ) > NVL ( A.LAST_PRINTED_SEQUENCE_NUM , 0 ) and 1 = 1 AND
    NOT EXISTS ( SELECT 'X' from ECE_TP_DETAILS ETD ,
                        ECE_TP_HEADERS ETH
    WHERE
    ETH.TP_HEADER_ID = A_BILL.TP_HEADER_ID AND
    ETD.TP_HEADER_ID = ETH.TP_HEADER_ID AND
    ETD.EDI_FLAG = 'Y' AND
    ETD.DOCUMENT_ID = 'INO' AND
    ETD.DOCUMENT_TYPE = DECODE ( TYPES.TYPE , 'CM' , DECODE ( A.PREVIOUS_CUSTOMER_TRX_ID , NULL , 'OACM' , 'CM' ) , TYPES.TYPE ) ) AND
    A.PRINTING_OPTION IN ( 'PRI' , 'REP' ) AND
    T.NAME not like 'PB%' AND
    1 = 1 AND
    MIT_AR.GE_AR_COAKLEY_ELIGIBLE ( 'NEW' , P.CUSTOMER_TRX_ID , A.ORG_ID ) = 0 AND
    A.org_id = TYPES.org_id UNION SELECT /*+ NO_INDEX(A, RA_CUSTOMER_TRX_N11 ) USE_NL(TYPES,A) */
    a.customer_trx_id , a.trx_number ,
    A . INTERFACE_HEADER_ATTRIBUTE5 ,
    A . INTERFACE_HEADER_CONTEXT ,
    A . INTERFACE_HEADER_ATTRIBUTE11 ,
    NVL ( P . TERMS_SEQUENCE_NUMBER , 1 ) ,
    types.type , l_types.meaning ,
    TYPES . ACCOUNTING_AFFECT_FLAG ,
    a.trx_date ,
    A . SHIP_TO_CUSTOMER_ID ,
    A . SHIP_TO_CONTACT_ID ,
    A . REMIT_TO_ADDRESS_ID ,
    A . PRIMARY_SALESREP_ID ,
    B . ACCOUNT_NUMBER ,
    A . INTERNAL_NOTES ,
    A . COMMENTS ,
    PREVIOUS_CUSTOMER_TRX_ID ,
    SHIP_TO_SITE_USE_ID ,
    NVL ( PRINTING_COUNT , 0 ) ,
    PRINTING_ORIGINAL_DATE ,
    PRINTING_LAST_PRINTED ,
    PRINTING_PENDING ,
    LAST_PRINTED_SEQUENCE_NUM ,
    START_DATE_COMMITMENT ,
    END_DATE_COMMITMENT ,
    INITIAL_CUSTOMER_TRX_ID ,
    A . INVOICE_CURRENCY_CODE ,
    A . REASON_CODE ,
    A . TERM_ID ,
    A . SHIP_DATE_ACTUAL ,
    A . SHIP_VIA ,
    A . WAYBILL_NUMBER ,
    A . PURCHASE_ORDER ,
    A . PURCHASE_ORDER_REVISION ,
    A . PURCHASE_ORDER_DATE ,
    A . CREATED_BY ,
    P . DUE_DATE ,
    NVL ( TL . RELATIVE_AMOUNT , 100 ) * ( 100 / NVL ( T . BASE_AMOUNT , 100 ) ) ,
    T . NAME , A . BILL_TO_CUSTOMER_ID ,
    A . BILL_TO_CONTACT_ID ,
    A . BILL_TO_SITE_USE_ID ,
    U_BILL . LOCATION BILL_TO_LOCATION ,
    SUBSTRB ( PARTY . PARTY_NAME , 1 , 50 ) BILL_CUST_NAME ,
    RTRIM ( RPAD ( LOC . ADDRESS1 , 40 ) ) ,
    RTRIM ( RPAD ( LOC . ADDRESS2 , 40 ) ) ,
    RTRIM ( RPAD ( LOC . ADDRESS3 , 40 ) ) ,
    RTRIM ( RPAD ( LOC . ADDRESS4 , 40 ) ) ,
    LOC . CITY ,
    NVL ( LOC . STATE , LOC . PROVINCE ) ,
    LOC . POSTAL_CODE ,
    LOC . COUNTRY ,
    U_BILL . TAX_REFERENCE ,
    PARTY . TAX_REFERENCE ,
    nvl(amount_line_items_original + decode(a.initial_customer_trx_id, '', 0, nvl(com_adj.amount,0)), to_number(null)) ,
    nvl(tax_original, to_number(null)) ,
    nvl(freight_original, to_number(null)) ,
    p.amount_due_original + decode(a.initial_customer_trx_id, '', 0, nvl(com_adj.amount,0)) ,
    DECODE ( : P_ORDER_BY , 'TRX_NUMBER' ,
    A . TRX_NUMBER , 'ADJUSTMENT_NUMBER' ,
    A.TRX_NUMBER , 'CUSTOMER' ,
    SUBSTRB ( PARTY . PARTY_NAME , 1 , 50 ) , 'POSTAL_CODE' ,
    LOC . POSTAL_CODE , A . TRX_NUMBER ) ORDER_BY ,
    LOC . ADDRESS1 ,
    LOC . ADDRESS2 ,
    LOC . ADDRESS3 ,
    LOC . ADDRESS4 ,
    LOC . STATE ,
    LOC . PROVINCE ,
    T . description ,
    a . org_id ,
    a . created_from
    FROM
    RA_TERMS_LINES TL,
    RA_CUST_TRX_TYPES TYPES,
    AR_LOOKUPS L_TYPES,
         HZ_CUST_ACCOUNTS B,
    HZ_PARTIES      PARTY,
    HZ_CUST_SITE_USES U_BILL,
    HZ_CUST_ACCT_SITES A_BILL,
    HZ_PARTY_SITES PARTY_SITE,
    HZ_LOCATIONS LOC,
    AR_ADJUSTMENTS COM_ADJ,
    RA_CUSTOMER_TRX A,
    AR_PAYMENT_SCHEDULES P,
    RA_TERMS T
    WHERE
    A.BILL_TO_CUSTOMER_ID = B.CUST_ACCOUNT_ID
    AND P.PAYMENT_SCHEDULE_ID + DECODE ( P.CLASS , 'INV' , 0 , '' ) = COM_ADJ.PAYMENT_SCHEDULE_ID (+) AND
    COM_ADJ.SUBSEQUENT_TRX_ID IS NULL AND
    'C' = COM_ADJ.ADJUSTMENT_TYPE (+) AND
    A.COMPLETE_FLAG = 'Y' AND
    A.CUSTOMER_TRX_ID = P.CUSTOMER_TRX_ID AND
    A.CUST_TRX_TYPE_ID = TYPES.CUST_TRX_TYPE_ID AND
    L_TYPES.LOOKUP_TYPE = 'INV/CM/ADJ' AND
    A.PRINTING_OPTION IN ( 'PRI' , 'REP' ) AND
    L_TYPES.LOOKUP_CODE = DECODE ( TYPES.TYPE , 'DEP' , 'INV' , TYPES.TYPE ) AND
    NVL ( T.PRINTING_LEAD_DAYS , 0 ) > 0 AND
    A.BILL_TO_SITE_USE_ID = U_BILL.SITE_USE_ID AND
    U_BILL.CUST_ACCT_SITE_ID = A_BILL.CUST_ACCT_SITE_ID AND
    A_BILL.PARTY_SITE_ID = PARTY_SITE.PARTY_SITE_ID AND
    B.PARTY_ID = PARTY.PARTY_ID AND
    LOC.LOCATION_ID = PARTY_SITE.LOCATION_ID AND
    NVL ( P.TERMS_SEQUENCE_NUMBER , TL.SEQUENCE_NUM ) = TL.SEQUENCE_NUM AND
    NVL ( P.AMOUNT_DUE_REMAINING , 0 ) <> 0 AND
    A.CUST_TRX_TYPE_ID = 2526 AND
    TYPES.TYPE = 'INV' AND
    T.TERM_ID = P.TERM_ID AND
    TL.TERM_ID (+) = T.TERM_ID AND
    A.PRINTING_PENDING = 'Y' AND
    P.TERMS_SEQUENCE_NUMBER > NVL ( A.LAST_PRINTED_SEQUENCE_NUM , 0 ) and 1 = 1 AND
    NOT EXISTS ( SELECT 'X' from
    ECE_TP_DETAILS ETD ,
    ECE_TP_HEADERS ETH
    WHERE
    ETH.TP_HEADER_ID = A_BILL.TP_HEADER_ID AND
    ETD.TP_HEADER_ID = ETH.TP_HEADER_ID AND
    ETD.EDI_FLAG = 'Y' AND
    ETD.DOCUMENT_ID = 'INO' AND
    ETD.DOCUMENT_TYPE = DECODE ( TYPES.TYPE , 'CM' , DECODE ( A.PREVIOUS_CUSTOMER_TRX_ID , NULL , 'OACM' , 'CM' ) , TYPES.TYPE ) ) AND
    NVL ( A.PRINTING_OPTION , 'REP' ) IN ( 'PRI' , 'REP' ) AND
    T.NAME not like 'PB%' AND 1 = 1 AND
    MIT_AR.GE_AR_COAKLEY_ELIGIBLE ( 'NEW' , P.CUSTOMER_TRX_ID , A.ORG_ID ) = 0 AND
    A.org_id = TYPES.org_id
    ORDER BY 60 ASC,68 ASC,2 ASC,3 ASC,4 ASC,5 ASC,44 ASC
    call count cpu elapsed disk query current rows
    Parse 1 0.06 0.08 0 364 0 0
    Execute 1 5.11 4.98 0 0 0 0
    Fetch 18 81.35 1375.73 203636 357435 0 34
    total 20 86.52 1380.80 203636 357799 0 34
    Misses in library cache during parse: 1
    Optimizer mode: ALL_ROWS
    Parsing user id: 25
    Number of plan statistics captured: 1
    Rows (1st) Rows (avg) Rows (max) Row Source Operation
    34 34 34 SORT UNIQUE (cr=569992 pr=227554 pw=0 time=1658548772 us)
    34 34 34 UNION-ALL (cr=569992 pr=227554 pw=0 time=773822949 us)
    34 34 34 FILTER (cr=228022 pr=107808 pw=0 time=773820837 us)
    34 34 34 FILTER (cr=228022 pr=107808 pw=0 time=773820394 us)
    34 34 34 NESTED LOOPS OUTER (cr=228022 pr=107808 pw=0 time=771070499 us)
    34 34 34 NESTED LOOPS (cr=227986 pr=107806 pw=0 time=773799735 us)
    34 34 34 NESTED LOOPS (cr=227882 pr=107806 pw=0 time=773742298 us)
    34 34 34 NESTED LOOPS (cr=227812 pr=107806 pw=0 time=773688021 us)
    34 34 34 NESTED LOOPS (cr=227708 pr=107806 pw=0 time=773636699 us)
    34 34 34 FILTER (cr=227638 pr=107806 pw=0 time=773566978 us)
    4616 4616 4616 NESTED LOOPS OUTER (cr=220679 pr=106836 pw=0 time=823009777 us)
    4610 4610 4610 NESTED LOOPS (cr=206836 pr=102712 pw=0 time=623651066 us)
    4610 4610 4610 NESTED LOOPS (cr=188367 pr=96450 pw=0 time=531982352 us)
    4610 4610 4610 NESTED LOOPS (cr=179145 pr=96449 pw=0 time=530980660 us)
    4610 4610 4610 FILTER (cr=165290 pr=96448 pw=0 time=529149297 us)
    4610 4610 4610 NESTED LOOPS OUTER (cr=165290 pr=96448 pw=0 time=530066499 us)
    4607 4607 4607 NESTED LOOPS (cr=156074 pr=96442 pw=0 time=762671095 us)
    4615 4615 4615 NESTED LOOPS (cr=146842 pr=96441 pw=0 time=751255741 us)
    4615 4615 4615 NESTED LOOPS (cr=137610 pr=96435 pw=0 time=751144978 us)
    4615 4615 4615 NESTED LOOPS (cr=123763 pr=96435 pw=0 time=750949543 us)
    4615 4615 4615 TABLE ACCESS BY INDEX ROWID RA_CUSTOMER_TRX_ALL (cr=114531 pr=96435 pw=0 time=750675597 us)
    *329687 329687 329687 INDEX RANGE SCAN RA_CUSTOMER_TRX_N17 (cr=6232 pr=6046 pw=0 time=31982795 us)(object id 4788)* 4615 4615 4615 TABLE ACCESS BY INDEX ROWID RA_CUST_TRX_TYPES_ALL (cr=9232 pr=0 pw=0 time=239450 us)
    4615 4615 4615 INDEX UNIQUE SCAN RA_CUST_TRX_TYPES_U1 (cr=4617 pr=0 pw=0 time=160139 us)(object id 55731)
    4615 4615 4615 TABLE ACCESS BY INDEX ROWID FND_LOOKUP_VALUES (cr=13847 pr=0 pw=0 time=180337 us)
    4615 4615 4615 INDEX UNIQUE SCAN FND_LOOKUP_VALUES_U1 (cr=9232 pr=0 pw=0 time=135717 us)(object id 491822)
    4615 4615 4615 TABLE ACCESS BY INDEX ROWID RA_TERMS_B (cr=9232 pr=6 pw=0 time=155074 us)
    4615 4615 4615 INDEX UNIQUE SCAN RA_TERMS_B_U1 (cr=4617 pr=0 pw=0 time=60566 us)(object id 55734)
    4607 4607 4607 TABLE ACCESS BY INDEX ROWID RA_TERMS_TL (cr=9232 pr=1 pw=0 time=181115 us)
    4615 4615 4615 INDEX UNIQUE SCAN RA_TERMS_TL_U1 (cr=4617 pr=1 pw=0 time=82153 us)(object id 55755)
    4610 4610 4610 TABLE ACCESS BY INDEX ROWID RA_TERMS_LINES (cr=9216 pr=6 pw=0 time=259864 us)
    4610 4610 4610 INDEX RANGE SCAN RA_TERMS_LINES_U1 (cr=4609 pr=1 pw=0 time=104110 us)(object id 6703)
    4610 4610 4610 TABLE ACCESS BY INDEX ROWID HZ_CUST_SITE_USES_ALL (cr=13855 pr=1 pw=0 time=1350357 us)
    4610 4610 4610 INDEX UNIQUE SCAN HZ_CUST_SITE_USES_U1 (cr=9222 pr=1 pw=0 time=434079 us)(object id 44870)
    4610 4610 4610 TABLE ACCESS BY INDEX ROWID HZ_CUST_ACCT_SITES_ALL (cr=9222 pr=1 pw=0 time=1024466 us)
    4610 4610 4610 INDEX UNIQUE SCAN HZ_CUST_ACCT_SITES_U1 (cr=4612 pr=1 pw=0 time=337477 us)(object id 46883)
    4610 4610 4610 TABLE ACCESS BY INDEX ROWID RA_CUST_TRX_LINE_GL_DIST_ALL (cr=18469 pr=6262 pw=0 time=86754686 us)
    4610 4610 4610 INDEX RANGE SCAN RA_CUST_TRX_LINE_GL_DIST_N6 (cr=13836 pr=4061 pw=0 time=55844483 us)(object id 1510967)
    4616 4616 4616 TABLE ACCESS BY INDEX ROWID AR_PAYMENT_SCHEDULES_ALL (cr=13843 pr=4124 pw=0 time=50689789 us)
    4616 4616 4616 INDEX RANGE SCAN AR_PAYMENT_SCHEDULES_N2 (cr=9233 pr=1494 pw=0 time=15169412 us)(object id 4877)
    34 34 34 TABLE ACCESS BY INDEX ROWID HZ_CUST_ACCOUNTS (cr=70 pr=0 pw=0 time=24450 us)
    34 34 34 INDEX UNIQUE SCAN HZ_CUST_ACCOUNTS_U1 (cr=36 pr=0 pw=0 time=5962 us)(object id 85278)
    34 34 34 TABLE ACCESS BY INDEX ROWID HZ_PARTY_SITES (cr=104 pr=0 pw=0 time=58609 us)
    34 34 34 INDEX UNIQUE SCAN HZ_PARTY_SITES_U1 (cr=70 pr=0 pw=0 time=31189 us)(object id 45527)
    34 34 34 TABLE ACCESS BY INDEX ROWID HZ_LOCATIONS (cr=70 pr=0 pw=0 time=52502 us)
    34 34 34 INDEX UNIQUE SCAN HZ_LOCATIONS_U1 (cr=36 pr=0 pw=0 time=23883 us)(object id 45692)
    34 34 34 TABLE ACCESS BY INDEX ROWID HZ_PARTIES (cr=104 pr=0 pw=0 time=49042 us)
    34 34 34 INDEX UNIQUE SCAN HZ_PARTIES_U1 (cr=70 pr=0 pw=0 time=22106 us)(object id 757879)
    0 0 0 TABLE ACCESS BY INDEX ROWID AR_ADJUSTMENTS_ALL (cr=36 pr=2 pw=0 time=19638 us)
    0 0 0 INDEX RANGE SCAN AR_ADJUSTMENTS_N3 (cr=36 pr=2 pw=0 time=19271 us)(object id 5473)
    0 0 0 NESTED LOOPS (cr=0 pr=0 pw=0 time=9 us)
    0 0 0 INDEX UNIQUE SCAN ECE_TP_HEADERS_U1 (cr=0 pr=0 pw=0 time=7 us)(object id 46119)
    0 0 0 TABLE ACCESS BY INDEX ROWID ECE_TP_DETAILS (cr=0 pr=0 pw=0 time=0 us)
    0 0 0 INDEX UNIQUE SCAN ECE_TP_DETAILS_U2 (cr=0 pr=0 pw=0 time=0 us)(object id 6271)
    0 0 0 FILTER (cr=341970 pr=119746 pw=0 time=883424760 us)
    0 0 0 FILTER (cr=341970 pr=119746 pw=0 time=883424758 us)
    0 0 0 NESTED LOOPS OUTER (cr=341970 pr=119746 pw=0 time=883424757 us)
    0 0 0 NESTED LOOPS (cr=341970 pr=119746 pw=0 time=883424752 us)
    0 0 0 NESTED LOOPS (cr=341970 pr=119746 pw=0 time=883424749 us)
    0 0 0 NESTED LOOPS (cr=341970 pr=119746 pw=0 time=883424742 us)
    0 0 0 NESTED LOOPS (cr=341970 pr=119746 pw=0 time=883424740 us)
    0 0 0 NESTED LOOPS (cr=341970 pr=119746 pw=0 time=883424735 us)
    0 0 0 NESTED LOOPS (cr=341970 pr=119746 pw=0 time=883424731 us)
    0 0 0 NESTED LOOPS (cr=341970 pr=119746 pw=0 time=883424726 us)
    0 0 0 NESTED LOOPS (cr=341970 pr=119746 pw=0 time=883424724 us)
    0 0 0 NESTED LOOPS (cr=341970 pr=119746 pw=0 time=883424721 us)
    0 0 0 NESTED LOOPS (cr=341970 pr=119746 pw=0 time=883424715 us)
    34 34 34 NESTED LOOPS (cr=341900 pr=119746 pw=0 time=882400482 us)
    4615 4615 4615 NESTED LOOPS (cr=123763 pr=93484 pw=0 time=674293279 us)
    4615 4615 4615 TABLE ACCESS BY INDEX ROWID RA_CUSTOMER_TRX_ALL (cr=114531 pr=93484 pw=0 time=674030164 us)
    329687 329687 329687 INDEX RANGE SCAN RA_CUSTOMER_TRX_N17 (cr=6232 pr=6130 pw=0 time=36621510 us)(object id 4788)
    4615 4615 4615 TABLE ACCESS BY INDEX ROWID RA_CUST_TRX_TYPES_ALL (cr=9232 pr=0 pw=0 time=249893 us)
    4615 4615 4615 INDEX UNIQUE SCAN RA_CUST_TRX_TYPES_U1 (cr=4617 pr=0 pw=0 time=169485 us)(object id 55731)
    34 34 34 TABLE ACCESS BY INDEX ROWID AR_PAYMENT_SCHEDULES_ALL (cr=218137 pr=26262 pw=0 time=310068873 us)
    3304 3304 3304 INDEX RANGE SCAN AR_PAYMENT_SCHEDULES_N2 (cr=214841 pr=24355 pw=0 time=283359418 us)(object id 4877)
    0 0 0 TABLE ACCESS BY INDEX ROWID RA_TERMS_B (cr=70 pr=0 pw=0 time=829 us)
    34 34 34 INDEX UNIQUE SCAN RA_TERMS_B_U1 (cr=36 pr=0 pw=0 time=527 us)(object id 55734)
    0 0 0 TABLE ACCESS BY INDEX ROWID HZ_CUST_SITE_USES_ALL (cr=0 pr=0 pw=0 time=0 us)
    0 0 0 INDEX UNIQUE SCAN HZ_CUST_SITE_USES_U1 (cr=0 pr=0 pw=0 time=0 us)(object id 44870)
    0 0 0 TABLE ACCESS BY INDEX ROWID HZ_CUST_ACCT_SITES_ALL (cr=0 pr=0 pw=0 time=0 us)
    0 0 0 INDEX UNIQUE SCAN HZ_CUST_ACCT_SITES_U1 (cr=0 pr=0 pw=0 time=0 us)(object id 46883)
    0 0 0 TABLE ACCESS BY INDEX ROWID HZ_CUST_ACCOUNTS (cr=0 pr=0 pw=0 time=0 us)
    0 0 0 INDEX UNIQUE SCAN HZ_CUST_ACCOUNTS_U1 (cr=0 pr=0 pw=0 time=0 us)(object id 85278)
    0 0 0 TABLE ACCESS BY INDEX ROWID HZ_PARTIES (cr=0 pr=0 pw=0 time=0 us)
    0 0 0 INDEX UNIQUE SCAN HZ_PARTIES_U1 (cr=0 pr=0 pw=0 time=0 us)(object id 757879)
    0 0 0 TABLE ACCESS BY INDEX ROWID HZ_PARTY_SITES (cr=0 pr=0 pw=0 time=0 us)
    0 0 0 INDEX UNIQUE SCAN HZ_PARTY_SITES_U1 (cr=0 pr=0 pw=0 time=0 us)(object id 45527)
    0 0 0 TABLE ACCESS BY INDEX ROWID HZ_LOCATIONS (cr=0 pr=0 pw=0 time=0 us)
    0 0 0 INDEX UNIQUE SCAN HZ_LOCATIONS_U1 (cr=0 pr=0 pw=0 time=0 us)(object id 45692)
    0 0 0 TABLE ACCESS BY INDEX ROWID RA_TERMS_TL (cr=0 pr=0 pw=0 time=0 us)
    0 0 0 INDEX UNIQUE SCAN RA_TERMS_TL_U1 (cr=0 pr=0 pw=0 time=0 us)(object id 55755)
    0 0 0 TABLE ACCESS BY INDEX ROWID RA_TERMS_LINES (cr=0 pr=0 pw=0 time=0 us)
    0 0 0 INDEX RANGE SCAN RA_TERMS_LINES_U1 (cr=0 pr=0 pw=0 time=0 us)(object id 6703)
    0 0 0 TABLE ACCESS BY INDEX ROWID FND_LOOKUP_VALUES (cr=0 pr=0 pw=0 time=0 us)
    0 0 0 INDEX UNIQUE SCAN FND_LOOKUP_VALUES_U1 (cr=0 pr=0 pw=0 time=0 us)(object id 491822)
    0 0 0 TABLE ACCESS BY INDEX ROWID AR_ADJUSTMENTS_ALL (cr=0 pr=0 pw=0 time=0 us)
    0 0 0 INDEX RANGE SCAN AR_ADJUSTMENTS_N3 (cr=0 pr=0 pw=0 time=0 us)(object id 5473)
    0 0 0 NESTED LOOPS (cr=0 pr=0 pw=0 time=0 us)
    0 0 0 INDEX UNIQUE SCAN ECE_TP_HEADERS_U1 (cr=0 pr=0 pw=0 time=0 us)(object id 46119)
    0 0 0 TABLE ACCESS BY INDEX ROWID ECE_TP_DETAILS (cr=0 pr=0 pw=0 time=0 us)
    0 0 0 INDEX UNIQUE SCAN ECE_TP_DETAILS_U2 (cr=0 pr=0 pw=0 time=0 us)(object id 6271)
    Elapsed times include waiting on following events:
    Event waited on Times Max. Wait Total Waited
    ---------------------------------------- Waited ---------- ------------
    row cache lock 6 0.00 0.00
    SQL*Net message to client 18 0.00 0.00
    SQL*Net more data to client 2 0.00 0.00
    gc current block 2-way 5375 0.02 3.76
    gc cr grant 2-way 49175 0.05 24.09
    db file sequential read 203636 0.51 1289.70
    gcs drm freeze in enter server mode 7 0.28 1.30
    latch: KCL gc element parent latch 4 0.00 0.00
    gc remaster 2 0.07 0.12
    latch: gcs resource hash 26 0.00 0.00
    latch free 1 0.00 0.00
    gc cr block 2-way 9 0.00 0.00
    latch: cache buffers chains 1 0.00 0.00
    SQL*Net message from client 18 0.00 0.00
    Thanks in Advance..

    Hi,
    it looks like your problem stems from the wrong choice of the driving table:
    4615 4615 4615            NESTED LOOPS (cr=123763 pr=96435 pw=0 time=750949543 us)
    4615 4615 4615              TABLE ACCESS BY INDEX ROWID RA_CUSTOMER_TRX_ALL (cr=114531 pr=96435 pw=0 time=750675597 us)
    329687 329687 329687       INDEX RANGE SCAN RA_CUSTOMER_TRX_N17 (cr=6232 pr=6046 pw=0 time=31982795 us)(object id 4788) 4615 4615 4615 TABLE ACCESS BY INDEX ROWID RA_CUST_TRX_TYPES_ALL (cr=9232 pr=0 pw=0 time=239450 us)
    4615 4615 4615              INDEX UNIQUE SCAN RA_CUST_TRX_TYPES_U1 (cr=4617 pr=0 pw=0 time=160139 us)(object id 55731)
    4615 4615 4615              TABLE ACCESS BY INDEX ROWID FND_LOOKUP_VALUES (cr=13847 pr=0 pw=0 time=180337 us)Here, you are spending 750 seconds retriveing 329k rows, 98% of which are rejected on the next step. And the reason this is happening is because the optimizer underestimates the cardinatlity of this step by a factor of x50 (6.2k estimated, 329k actual value). Find out why this is happening or post relevant diagnostic information here (predicates, columns stats etc.), and fix this -- this should make your query twice as fast.
    The second half is coming from the SORT UNIQUE step. This looks weird: why on Earth it would take over 800 seconds to sort 34 rows?! Maybe there is something in the plan that I'm missing -- it's really hard to read because it's not formatted. Please obtain a plan with rowsource stats using dbms_xplan.display_cursor and post it here (google dbms_xplan.display_cursor allstats last if not sure how to do that).
    Best regards,
    Nikolay

  • Complex SQL query help

    Hi,
    I was wondering if I could have some help with the query I have below for this general data set, please? I need to do it in a single SQL statement. We're currently running Oracle 10g. The piece I'm struggling with is identifying that the person has all the items in a collection and to include the collection in the resulting collection of items.
    persons_items
    person     item
    Ted          cup
    Ted          saucer
    Ted          plate
    Ted          fork
    Alice          book
    Alice          thimble
    Alice          knife
    Alice          cup
    Joe          cup
    Joe          saucer
    Joe          plate
    Joe          knife
    Joe          fork
    Joe          spoon
    Jessica     spatula
    Jessica     dish
    collections
    collection_name     item
    crockery     cup
    crockery     saucer
    crockery     plate
    cutlery          knife
    cutlery          fork
    cutlery          spoon
    Query:
    What single items and collections does Ted have?     fork, crockery
    What single items and collections does Alice have?     book, thimble, knife, cup
    What single items and collections does Joe have?     crockery, cutlery
    What single items and collections does Jessica have?     spatula, dish
    Thanks in advance.
    Pat

    Expanding on Brendan's solution...
    For 11g:
    SQL> ed
    Wrote file afiedt.buf
      1  WITH person_items AS (
      2          SELECT 'Ted' person, 'cup' item FROM DUAL UNION
      3          SELECT 'Ted', 'saucer' FROM DUAL UNION
      4          SELECT 'Ted', 'plate' FROM DUAL UNION
      5          SELECT 'Ted', 'fork' FROM DUAL UNION
      6          SELECT 'Alice', 'book' FROM DUAL UNION
      7          SELECT 'Alice', 'thimble' FROM DUAL UNION
      8          SELECT 'Alice', 'knife' FROM DUAL UNION
      9          SELECT 'Alice', 'cup' FROM DUAL UNION
    10          SELECT 'Joe', 'cup' FROM DUAL UNION
    11          SELECT 'Joe', 'saucer' FROM DUAL UNION
    12          SELECT 'Joe', 'plate' FROM DUAL UNION
    13          SELECT 'Joe', 'knife' FROM DUAL UNION
    14          SELECT 'Joe', 'fork' FROM DUAL UNION
    15          SELECT 'Joe', 'spoon' FROM DUAL UNION
    16          SELECT 'Jessica', 'spatula' FROM DUAL UNION
    17          SELECT 'Jessica', 'dish' FROM DUAL
    18  ), collections AS (
    19          SELECT 'crockery' collection_name, 'cup' item FROM DUAL UNION
    20          SELECT 'crockery', 'saucer' FROM DUAL UNION
    21          SELECT 'crockery', 'plate' FROM DUAL UNION
    22          SELECT 'cutlery', 'knife' FROM DUAL UNION
    23          SELECT 'cutlery', 'fork' FROM DUAL UNION
    24          SELECT 'cutlery', 'spoon' FROM DUAL
    25  ), person_item_colls AS (
    26          SELECT pi.person, pi.item, co.collection_name,
    27                 Count(*) OVER (PARTITION BY pi.person, co.collection_name) n_col
    28            FROM person_items pi
    29            LEFT JOIN collections co
    30              ON co.item = pi.item
    31  )
    32  select person, listagg(collection_name,',') within group (order by collection_name) as collections
    33  from (
    34        SELECT DISTINCT person, collection_name
    35        FROM   person_item_colls
    36        WHERE  collection_name IS NOT NULL
    37        AND    n_col > 1
    38        UNION ALL
    39        SELECT person, item
    40        FROM   person_item_colls
    41        WHERE  collection_name IS NULL
    42        OR     n_col = 1
    43       )
    44* group by person
    SQL> /
    PERSON  COLLECTIONS
    Alice   book,cup,knife,thimble
    Jessica dish,spatula
    Joe     crockery,cutlery
    Ted     crockery,forkFor 10g:
    SQL> ed
    Wrote file afiedt.buf
      1  WITH person_items AS (
      2          SELECT 'Ted' person, 'cup' item FROM DUAL UNION
      3          SELECT 'Ted', 'saucer' FROM DUAL UNION
      4          SELECT 'Ted', 'plate' FROM DUAL UNION
      5          SELECT 'Ted', 'fork' FROM DUAL UNION
      6          SELECT 'Alice', 'book' FROM DUAL UNION
      7          SELECT 'Alice', 'thimble' FROM DUAL UNION
      8          SELECT 'Alice', 'knife' FROM DUAL UNION
      9          SELECT 'Alice', 'cup' FROM DUAL UNION
    10          SELECT 'Joe', 'cup' FROM DUAL UNION
    11          SELECT 'Joe', 'saucer' FROM DUAL UNION
    12          SELECT 'Joe', 'plate' FROM DUAL UNION
    13          SELECT 'Joe', 'knife' FROM DUAL UNION
    14          SELECT 'Joe', 'fork' FROM DUAL UNION
    15          SELECT 'Joe', 'spoon' FROM DUAL UNION
    16          SELECT 'Jessica', 'spatula' FROM DUAL UNION
    17          SELECT 'Jessica', 'dish' FROM DUAL
    18  ), collections AS (
    19          SELECT 'crockery' collection_name, 'cup' item FROM DUAL UNION
    20          SELECT 'crockery', 'saucer' FROM DUAL UNION
    21          SELECT 'crockery', 'plate' FROM DUAL UNION
    22          SELECT 'cutlery', 'knife' FROM DUAL UNION
    23          SELECT 'cutlery', 'fork' FROM DUAL UNION
    24          SELECT 'cutlery', 'spoon' FROM DUAL
    25  ), person_item_colls AS (
    26          SELECT pi.person, pi.item, co.collection_name,
    27                 Count(*) OVER (PARTITION BY pi.person, co.collection_name) n_col
    28            FROM person_items pi
    29            LEFT JOIN collections co
    30              ON co.item = pi.item
    31  )
    32  select person, ltrim(sys_connect_by_path(collection_name,','),',') as collections
    33  from (
    34        select person, collection_name, row_number() over (partition by person order by collection_name) as rn
    35        from (
    36              SELECT DISTINCT person, collection_name
    37              FROM   person_item_colls
    38              WHERE  collection_name IS NOT NULL
    39              AND    n_col > 1
    40              UNION ALL
    41              SELECT person, item
    42              FROM   person_item_colls
    43              WHERE  collection_name IS NULL
    44              OR     n_col = 1
    45             )
    46        )
    47  where connect_by_isleaf = 1
    48  connect by person = prior person and rn = prior rn + 1
    49* start with rn = 1
    SQL> /
    PERSON  COLLECTIONS
    Alice   book,cup,knife,thimble
    Jessica dish,spatula
    Joe     crockery,cutlery
    Ted     crockery,fork

  • Very Complex SQL Query

    Hi,
    I have three tables
    master(m_id m_name)
    1 m1
    2 m2
    3 m3
    t1(m_id, int_type)
    1 INT(2)
    1 INT(3)
    t2(m_id, char_type)
    1 CHAR(3)
    1 CHAR(4)
    master table has m_id as a primary key, while this is foreign key in tables t1 and t2. Now i want to get the result like this.
    m1 INT(2) INT(3) CHAR(3) CHAR(4)
    [All these rows should be converted into columns]
    Thanx in Advance

    Hi,
    Here's a sample based on pivoting:TEST.SQL>CREATE TABLE MASTER
      2  (
      3       M_ID NUMBER PRIMARY KEY,
      4       M_NAME VARCHAR2(5)
      5  );
    Table created.
    TEST.SQL>INSERT INTO MASTER VALUES (1,'m1');
    1 row created.
    TEST.SQL>INSERT INTO MASTER VALUES (2,'m2');
    1 row created.
    TEST.SQL>INSERT INTO MASTER VALUES (3,'m3');
    1 row created.
    TEST.SQL>
    TEST.SQL>CREATE TABLE T1
      2  (
      3       M_ID NUMBER REFERENCES MASTER(M_ID),
      4       INT_TYPE VARCHAR2(10)
      5  );
    Table created.
    TEST.SQL>
    TEST.SQL>INSERT INTO T1 VALUES (1,'INT(2)');
    1 row created.
    TEST.SQL>INSERT INTO T1 VALUES (1,'INT(3)');
    1 row created.
    TEST.SQL>INSERT INTO T1 VALUES (2,'INT(1)');
    1 row created.
    TEST.SQL>INSERT INTO T1 VALUES (3,'INT(1)');
    1 row created.
    TEST.SQL>INSERT INTO T1 VALUES (3,'INT(2)');
    1 row created.
    TEST.SQL>INSERT INTO T1 VALUES (3,'INT(4)');
    1 row created.
    TEST.SQL>INSERT INTO T1 VALUES (3,'INT(8)');
    1 row created.
    TEST.SQL>
    TEST.SQL>CREATE TABLE T2
      2  (
      3       M_ID NUMBER REFERENCES MASTER(M_ID),
      4       CHAR_TYPE VARCHAR2(10)
      5  );
    Table created.
    TEST.SQL>
    TEST.SQL>INSERT INTO T2 VALUES (1,'CHAR(3)');
    1 row created.
    TEST.SQL>INSERT INTO T2 VALUES (1,'CHAR(4)');
    1 row created.
    TEST.SQL>INSERT INTO T2 VALUES (3,'CHAR(9)');
    1 row created.
    TEST.SQL>INSERT INTO T2 VALUES (3,'CHAR(10)');
    1 row created.
    TEST.SQL>
    TEST.SQL>
    TEST.SQL>COMMIT;
    Commit complete.
    TEST.SQL>
    TEST.SQL>SELECT M_NAME, MAX(V1), MAX(V2), MAX(V3), MAX(V4), MAX(V5), MAX(V6)
      2  FROM
      3       (
      4       SELECT
      5            M_NAME,RN,DECODE(RN,1,VAL) V1,DECODE(RN,2,VAL) V2,DECODE(RN,3,VAL) V3,DECODE(RN,4,VAL) V4,DECODE(RN,5,VAL) V5,DECODE(RN,6,VAL) V6
      6       FROM
      7                 (
      8                 SELECT M_NAME, VAL, ROW_NUMBER() OVER (PARTITION BY M_NAME ORDER BY VAL) RN
      9                 FROM
    10                           (
    11                           SELECT MASTER.M_NAME, T1.INT_TYPE VAL
    12                           FROM MASTER, T1
    13                           WHERE T1.M_ID=MASTER.M_ID
    14                           UNION ALL
    15                           SELECT MASTER.M_NAME, T2.CHAR_TYPE VAL
    16                           FROM MASTER, T2
    17                           WHERE T2.M_ID=MASTER.M_ID
    18                           ) SRC
    19                 )
    20       ) TS
    21  GROUP BY M_NAME
    22  ;
    M_NAM MAX(V1)    MAX(V2)    MAX(V3)    MAX(V4)    MAX(V5)    MAX(V6)
    m1    CHAR(3)    CHAR(4)    INT(2)     INT(3)
    m2    INT(1)
    m3    CHAR(10)   CHAR(9)    INT(1)     INT(2)     INT(4)     INT(8)Note that this will work only if the max number of columns is 6.
    It's possible (I already did it) to build a function that returns a dynamix result set from this query build dynamically. I'll let you do so!
    Regards,
    Yoann.

  • Continuation-Please help with a complex sql query

    Hi all,
    Thanks a lot for your suggestions and inputs in the last thread of this post.
    With the help of your suggested approach,i went ahead and was able to get some more data in the format as needed..I worked on gradually adding one table after another exactly as the data is required stepwise.But,still I am facing issues with displaying them.
    there are many issues I am not able to do and would appreciate if you please have a look at my latest modified SELECT given below and help me writing it to get the display as needed.
    Below,is the attempted query i tried out using your inputs.But,am stuck and need your help in writing it.
    **Here,there is 1 ->MANY lines for the t_objective_id--->This has many learning_record_ids which i want to group by the unique objective_id.
    Also,prior to that,there is a tplan_id---->which has MANY t_objective_id's
    SELECT DECODE (LAG (firstname, 1, 0) OVER (ORDER BY firstname),
                   firstname, ' ',
                   firstname
                  ) firstname,
           DECODE (LAG (emplid, 1, 0) OVER (ORDER BY emplid),
                   emplid, ' ',
                   emplid
                  ) emplid,
           DECODE (LAG (tplan_name, 1, 0) OVER (ORDER BY tplan_name),
                   tplan_name, ' ',
                   tplan_name
                  ) tplan_name,
                  tplan_id,
                  DECODE (LAG (activities, 1, 0) OVER (ORDER BY activities),
                   activities, ' ',
                   activities
                  ) activities,
                  activities,
                  --activities,
           DECODE (LAG (t_objective_id, 1, 0) OVER (ORDER BY t_objective_id),
                   t_objective_id, ' ',
                   t_objective_id
                  ) t_objective_id,           
                   completed_activities,required_credits,
          DECODE (LAG (learning_record_id, 1, 0) OVER (ORDER BY learning_record_id),
                   learning_record_id, ' ',
                   learning_record_id
                  ) learning_record_id,           
                   catalog_item_name,catalog_item_type           
      FROM (SELECT test_cp.firstname, test_op.emplid, tp.tplan_name,tp.tplan_id,FN_TP_GET_CMPLTD_ACT_CNT(tp.tplan_id,test_lp.lp_person_id,'1862') activities,
                   test_tpo.t_objective_id,
                   (  fn_tp_obj_comp_req_act_cdt (test_lp.lp_person_id,
                                                  tp.tplan_id,
                                                  test_tpo.t_objective_id,
                                                  tp.is_credit_based
                    + fn_tp_obj_comp_opt_act_cdt (test_lp.lp_person_id,
                                                  tp.tplan_id,
                                                  test_tpo.t_objective_id,
                                                  tp.is_credit_based
                   ) completed_activities,test_tpo.required_credits,lr.learning_record_id,lr.catalog_item_name,lr.catalog_item_type
              FROM test_learning_plan test_lp,
                   test_training_plan tp,
                   test_person test_cp,
                   test_org_person test_op,test_tp_learning_activity test_tplplr,
                   test_tp_objective test_tpo,test_learning_record lr,test_train_obj_activity tpobjact
             WHERE test_lp.lp_person_id = '1862188559'
               AND test_cp.person_id = test_lp.lp_person_id
               AND tp.tplan_id = 'tplan200811200632352287621599'
               AND test_tpo.t_objective_id = tpobjact.t_objective_id
               AND test_lp.LP_CATALOG_HIST_ID = tp.tplan_id
               AND test_tplplr.tp_lp_lr_id =test_lp.learning_plan_id
               AND test_tplplr.activity_lp_lr_id = lr.learning_record_id
               AND lr.LR_CATALOG_HISTORY_ID = tpobjact.activity_id
               AND test_op.o_person_id = test_cp.person_id
               AND test_tpo.tplan_id = tp.tplan_id)
    If we see the outer SELECT ---then one of the main issues is for EACH t_objective_id----->There are n learning_record_ids.
    But,this select only shows 1 learning_record_id for each objective_id which is wrong.
    Similarly,the data displayed isnot proper.
    Below is the way I am getting the data now from the above SELECT.
    Note:- FIRSTNAME is not correctly displayed.
    FIRSTNAME     EMPLID     TPLAN_NAME     TPLAN_ID     ACTIVITIES     ACTIVITIES_1  
                      001                TP1          tplan1          5          5
    TESTNAME                                              tplan1           5          
    Continuation of the other columns of the same rows--**couldnt paste it properly so.
    T_OBJECTIVE_ID     COMPLETED_ACTIVITIES     REQUIRED_CREDITS     LEARNING_RECORD_ID        CATALOG_ITEM_NAME     CATALOG_ITEM_TYPE
              obj1               1                         5                         lr1                            C1                          Course
                    obj2               4                        4          

    Something like this might solve your problem ->
    satyaki>
    satyaki>select * from v$version;
    BANNER
    Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod
    PL/SQL Release 10.2.0.1.0 - Production
    CORE    10.2.0.1.0      Production
    TNS for Linux: Version 10.2.0.1.0 - Production
    NLSRTL Version 10.2.0.1.0 - Production
    Elapsed: 00:00:00.00
    satyaki>
    satyaki>
    satyaki>select * from emp;
         EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO
          7521 WARD       SALESMAN        7698 22-FEB-81     226.88        500         30
          7654 MARTIN     SALESMAN        7698 28-SEP-81       1815       1400         30
          7788 SCOTT      ANALYST         7566 19-APR-87     598.95                    20
          7839 KING       PRESIDENT            17-NOV-81       7260                    10
          7844 TURNER     SALESMAN        7698 08-SEP-81       2178          0         30
          7876 ADAMS      CLERK           7788 23-MAY-87     159.72                    20
          7900 JAMES      CLERK           7698 03-DEC-81     1379.4                    30
          7902 FORD       ANALYST         7566 03-DEC-81    5270.76                    20
          7934 MILLER     CLERK           7782 23-JAN-82     1887.6                    10
          7566 Smith      Manager         7839 23-JAN-82       1848          0         10
          7698 Glen       Manager         7839 23-JAN-82       1848          0         10
         EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO
          7599 BILLY      ANALYST         7566 10-JUN-09       4500                    30
    12 rows selected.
    Elapsed: 00:00:00.00
    satyaki>
    satyaki>
    satyaki>
    satyaki>
    satyaki>select decode(lag(job,1,null) over(order by job),'CLERK','CK',
      2                                                   'SALESMAN', 'SM',
      3                                                   'ANALYST','AL',
      4                                                   'BO') res
      5  from emp;
    RE
    BO
    AL
    AL
    AL
    CK
    CK
    CK
    BO
    BO
    BO
    SM
    RE
    SM
    12 rows selected.
    Elapsed: 00:00:00.01
    satyaki>
    satyaki>
    satyaki>select MAX(decode(lag(job,1,null) over(order by job),'CLERK','CK',
      2                                                   'SALESMAN', 'SM',
      3                                                   'ANALYST','AL',
      4                                                   'BO')) res
      5  from emp;
    select MAX(decode(lag(job,1,null) over(order by job),'CLERK','CK',
    ERROR at line 1:
    ORA-30483: window  functions are not allowed here
    Elapsed: 00:00:00.01
    satyaki>
    satyaki>select MAX(res)
      2  from (
      3          select decode(lag(job,1,null) over(order by job),'CLERK','CK',
      4                                                           'SALESMAN', 'SM',
      5                                                           'ANALYST','AL',
      6                                                           'BO') res
      7          from emp
      8     );
    MA
    SM
    Elapsed: 00:00:00.00
    satyaki>
    satyaki>Regards.
    Satyaki De.

  • View object editor for complex sql query in expert mode

    I find not convenient that the editor is modal so we cannot browse tables or procedure functions definition and that it is not possible to close it even the statement is not correct (to search for exact naming etc ...).
    May a way will be to show a list of corresponding database objects during typing like the java edit with the classes

    I filed enhancement request # 6062896 to track this suggestion. thanks.
    At a minimum, by production, we plan to re-enable the query-builder that was offered in 10.1.3, which should help with formulating queries in terms of table/column names.

  • Build dynamic SQL query in Database Adapter.

    Hi All,
    I have a requirement to build dynamic sql query at Database Adapter.
    My BPEL process is getting search spec as input from siebel. I need to process this searchspec in BPEL and need to build the SQL query dynamically at Database Adapter to fetch the records from DB.
    it goes like this....
    1. Sieble Search Spec: city1 OR city2 OR city3 OR city4 .....
    I need to build query as
    select * from S_ADDR_PER where city like 'city1' OR city like 'city2' OR city like 'city3' OR city like 'city4' ......
    2. Siebel Search spec: city1 AND country1 AND state1....
    I need to build query as
    Select * from S_ADDR_PER where city like 'city1' AND country like 'country1' AND state like 'state1' ....
    3. Siebel Search spec: state
    I need to build query as
    select * from S_ADDR_PER where state like '%state%';
    Is it feasible in Database Adapter? if its Yes.
    Any guidelines to achieve this?
    Thank you
    Chandra

    Hi All,
    I have a requirement to build dynamic sql query at Database Adapter.
    My BPEL process is getting search spec as input from siebel. I need to process this searchspec in BPEL and need to build the SQL query dynamically at Database Adapter to fetch the records from DB.
    it goes like this....
    1. Sieble Search Spec: city1 OR city2 OR city3 OR city4 .....
    I need to build query as
    select * from S_ADDR_PER where city like 'city1' OR city like 'city2' OR city like 'city3' OR city like 'city4' ......
    2. Siebel Search spec: city1 AND country1 AND state1....
    I need to build query as
    Select * from S_ADDR_PER where city like 'city1' AND country like 'country1' AND state like 'state1' ....
    3. Siebel Search spec: state
    I need to build query as
    select * from S_ADDR_PER where state like '%state%';
    Is it feasible in Database Adapter? if its Yes.
    Any guidelines to achieve this?
    Thank you
    Chandra

  • Custom query conditions in db adapter

    Hello,
    How to execute custom query in bpel db adapter?
    For example :
    1. select * from employee where status = 'N' and address is not null
    2. select * from empoyee where substatus = 'Y' and phone is null
    after where keyword - query part is dynamic, parameter combinations and count is unlimited ( depends on business logic ). It is not possible to implement each condition parameter as parameter in db adapter configuration.
    Db adatper query has to be something like  : select * from employee where #condition# , but #condtion# need to be interpretated as condition not string with quotes..
    best Regards,
    Uldis

    Say your complete query looks like this -
    select SUBJECT_NAME from RELATION
                where PID_GID_SSID IN (select PARTY_KEY from  xref WHERE row_id='123')
    1). In assign activity, write out the query part which comes after where clause and assign to variable query
    (select PARTY_KEY from  xref WHERE row_id='123') , replace the value of row_id from input value in assign.
    2. Now open DB adapter and pass the rest of query as input -
    select SUBJECT_NAME from RELATION
                where PID_GID_SSID IN #query

  • Repair Problem with Excel Removing All But First External SQL Query Tables

    We have an Excel 2007 XLSX workbook that we created for doing some business analysis from the SQL database that contains data from our MRP system. It started as a single sheet with one data connection running a SQL query. The data comes into
    the spreadsheet as a table and there are some basic row and column sums being done.
    After getting the first version up and running without problems, we decided to add some additional sheets to the same workbook. Since each of these additional sheets had only slight variations to the SQL query and the formatting was all to be the same, we
    simply copied the first sheet and modified the data connection for each successive sheet (each copy automatically created a copy of the data connection as well). Everything seemed to be up and running properly, so we saved and closed the workbook.
    A little bit later, the same workbook was re-opened and Excel reported that "Excel found unreadable content in '<file name>'. Do you want to recover the contents of this workbook? If you trust the source of this workbook, click Yes." If Yes
    is clicked, then Excel indicates the repairs that were made. Only one was made for each sheet beyond the first and that was "Removed Part: /xl/queryTables/queryTable2.xml part. (External data range)". The result was that the data connection
    was deleted from every sheet other than the first and only static data left in place.
    We thought it might have actual been some corruption, so we deleted the static sheets and recreated them the same way. After saving and closing, re-opening the file resulted in the same problem. It seems that Excel is somehow saving data that it can't read
    when it opens. I would think it could be something in the SQL query, but all sheets have essentially the same SQL query, so I'm not sure why the first sheet survives. As a result, I think it must be something that it does in copying the sheet and the corresponding
    data connections, but I'm not sure.
    I think this might be some kind of bug, but would like to know if anyone else has encountered a similar problem and found a solution. A few internet searches didn't give me much to check out.
    Thanks in advance,
    indyvql

    KR,
    Thanks for your response. I don't think it applies in this case since the file was created brand new in Excel 2007 and used in the same. There was no 3rd-party program involved. I'm also not migrating from 2003 to 2010 as the stellarinfo.com article
    suggests. Some of the other suggestions there also don't make any difference as all the data links are lost on opening the worksheet, so copying and pasting doesn't do any good.
    I tried to create a simple example by creating a new workbook with a sheet with a very simple SQL query ("select * from X where a > b"), but in that example it worked fine. I'm guessing there is something key about the much more complex SQL query
    in my original file with the problem, but I'm not sure why it would only affect copies of the data connection and not the original data connection itself.
    Thanks,
    indyvql

Maybe you are looking for

  • Need help in conversion of pdf file to itab and d details abt SMARTFORMS

    hello all how can i retrieve data from  pdf  file (instead of a txt format) to an internal table for an BDC application . anyone of you please help me in SMARTFORMS .

  • Is BI Content supposed to be activated in DEV and then transported

    Hi experts, Is BI Content supposed to be activated in DEV first and then is transported from DEV to QAS and PRD? Or Is BI content activated in each system independently: DEV, QAS, and PRD? Iu2019m trying to transport a BI Content activation from DVE

  • Display only latest record

    Post Author: rlivermore CA Forum: Formula (CR v10 Pro) How can I display only the lastest record if there are multiple entries for a specific job number (SO number). I have 2 groups {tblSO.SONumber} and {tblSONotes.LastModified}.  I tried sorting Las

  • Field in CRM_DNO_MONITOR

    Hi All, I have Successfully Configured SLA and the Contrat picking and all r working fine. But when i Create a ticket and check the same ticket in CRM_DNO_MONITOR it is showing a Display START CUSTOMER REQUIREMENT = TODAY'S DATA END CUSTOMER REQUIREM

  • The gift of music: burning a disc for friends

    I wonder if anyone can advise me here. I want to burn a music CD and give it as a gift to my friend. Can anyone advise me on where I stand, legally, in the following cases: 1. I own the music, my friend does not. 2. I own the music, my friend also ow