Pivot help on an heirarchy query

I've looked through the FAQs but I'm just not seeing it.
I have the following:
A query returning:
o_id   oname            typ  prnt
118    Pension Admin    PRC    91
91     Retirement       LOB   218
218    Benefits         SEG    81
119    Plan Mgmt        PRC    91
91     Retirement       LOB   218
218    Benefits         SEG    81
142    RCS Software     PRC    93
93     Risk Software    LOB   221
221    Risk Services    SEG    81I want the following:
ID  PRC              LOB               SEG
118 Pension Admin    Retirement        Benefits
119 Plan Mgmt        Retirement        Benefits
142 RCS Software     Risk Software     Risk ServicesVersion:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
PL/SQL Release 11.2.0.1.0 - Production
CORE 11.2.0.1.0 Production
TNS for Linux: Version 11.2.0.1.0 - Production
NLSRTL Version 11.2.0.1.0 - Production
Sample data:
create table t1( o_id  number(3)
               , oname varchar2(20)
               , typ  varchar2(3)
               , prnt number(3)
insert into t1 values(118,   'Pension Admin',  'PRC',  91);
insert into t1 values(91,    'Retirement',     'LOB', 218);
insert into t1 values(218,   'Benefits',       'SEG',  81);
insert into t1 values(119,   'Plan Mgmt',      'PRC',  91);
insert into t1 values(142,   'RCS Software',   'PRC',  93);
insert into t1 values(93,    'Risk Software',  'LOB', 221);
insert into t1 values(221,   'Risk Services',  'SEG',  81);I can't get past:
select level lvl, org.*
  from t1 org
  start with typ = 'PRC'
  connect by  o_id =  prior prnt;I've tried this, but of course it returns only one row:
select *
  from (
         select lvl, oname
           from ( 
                  select level lvl, t1.*
                    from t1
                   start with typ = 'PRC'
                 connect by  o_id =  prior prnt
pivot ( min(oname)  for lvl in (1  prc, 2 lob, 3 seg) )What am I missing, any help would be appreciated.

Hi,
Thanks for posting the CREATE TABLE and INSERT statements; that's very helpful!
Use CONNECT_BY_ROOT to remember the starting point that led to each row of output, which is what identifies which row of output each row of the CONNECT BY results corresponds to.
Sorry, I'm not at an Oracle 11 system now. This works in Oracle 10 (and up):
WITH     connect_by_results     AS
     SELECT     LEVEL               AS lvl
     ,      oname
       ,     CONNECT_BY_ROOT     o_id     AS starting_o_id
     FROM     t1
       START WITH     typ      = 'PRC'
     CONNECT BY     o_id      =  PRIOR prnt
SELECT       starting_o_id
,       MIN (CASE WHEN lvl = 1 THEN oname END)     AS prc
,       MIN (CASE WHEN lvl = 2 THEN oname END)     AS lob
,       MIN (CASE WHEN lvl = 3 THEN oname END)     AS seg
FROM       connect_by_results
GROUP BY  starting_o_id
ORDER BY  starting_o_id
;You can adapt the main query to use SELECT ... PIVOT.
Do you really need separate columns for the different levels? If not, you could do something like this:
SELECT     CONNECT_BY_ROOT     o_id     AS starting_o_id
,      REPLACE ( SYS_CONNECT_BY_PATH ( RPAD (oname, 15)
                          , '`'
          , '`'
          )     AS prc_lob_seg
FROM     t1
WHERE     CONNECT_BY_ISLEAF     = 1
START WITH     typ      = 'PRC'
CONNECT BY     o_id      =  PRIOR prnt
ORDER BY     starting_o_id
;Ouptut:
STARTING_O_ID PRC_LOB_SEG
          118 Pension Admin  Retirement     Benefits
          119 Plan Mgmt      Retirement     Benefits
          142 RCS Software   Risk Software  Risk ServicesNot only is this simpler, it works for any number of levels.
Edited by: Frank Kulash on Jan 16, 2012 4:25 PM
Added SYS_CONNECT_BY_PATH alternative.

Similar Messages

  • Power Query; How do I reference a Power Pivot table from a Power Query query

    Hi,
    It's pretty awesome how you can define Extract Transform and Load processes within Power Query without having to type in a single line of code. However how do I reference a Power Pivot table from a Power Query query to avoid me repeatedly accessing
    the same data source (CSV) file with a view to increasing performance?
    We are aware of the reference sub menu option with Power Query. However the new query created by the "reference" option still seems to refresh data from the data source (CSV) rather than just referencing the base query. Is this understanding
    correct? There does seem to be a lot of hard disk activity when re-running the new query which is based on a base query rather than a data source.  So we were hoping the new query would just need to reference the base query in memory rather than rescanning
    the hard disk. Is there any way to ensure that the reference query just rescans the base query in memory?
    Kind Regards,
    Kieran.
    Kieran Patrick Wood http://www.innovativebusinessintelligence.com http://uk.linkedin.com/in/kieranpatrickwood http://kieranwood.wordpress.com/

    Hi Kieran,
    This sounds like something to be suggested for a future release. At the present time, Power Query will always re-run the entire Power Query query when refreshed. The Reference feature is analogous to a SQL view whereby the underlying query is always re-executed
    when it's queried, or in this case refreshed. Even something like using the Power Query cache to minimise the amount of data re-read from the disk would be helpful for performance but the cache is only used for the preview data and stored locally.
    It would be a good idea to suggest this feature to the Power BI team via the feedback smiley face.
    Regards,
    Michael Amadi
    Please use the 'Mark as answer' link to mark a post that answers your question. If you find a reply helpful, please remember to vote it as helpful :)
    Website: http://www.nimblelearn.com, Twitter:
    @nimblelearn
    Hi Michael, 
    Glad to hear from you about this.  And thanks to Kieran for bringing a very good valid point to debate. Will be glad to see this in future release. 
    - please mark correct answers

  • Pivoting help required

    Hi
    I am using oracle 11.2.0 version
    I need help in pivoting the values.
    Here is the sample data looks like
    with t as
    SELECT 'JACK' C1,'RAM' C2,'LAND' C3,'KELLY' C4, 'WORKING' STATUS FROM DUAL
    UNION ALL
    SELECT 'JAMES' C1,'NAREN' C2,'SRUJAN' C3,'GANA' C4, 'WORKING' STATUS FROM DUAL
    UNION ALL
    SELECT 'MARK' C1,'SAM' C2,'BIKAS' C3,'MALLI' C4, 'NOT WORKING' STATUS FROM DUAL
    SELECT * FROM T
    I need a way to pivot the data in below query output format
    WITH T2 AS
    SELECT 'JACK' AS CUSTOMER,'WORKING' STATUS FROM DUAL
    UNION
    SELECT 'RAM' AS CUSTOMER,'WORKING' STATUS FROM DUAL
    UNION
    SELECT 'LAND' AS CUSTOMER,'WORKING' STATUS FROM DUAL
    UNION
    SELECT 'KELLY' AS CUSTOMER,'WORKING' STATUS FROM DUAL
    UNION
    SELECT 'JAMES' AS CUSTOMER,'WORKING' STATUS FROM DUAL
    UNION
    SELECT 'NAREN' AS CUSTOMER,'WORKING' STATUS FROM DUAL
    UNION
    SELECT 'SRUJAN' AS CUSTOMER,'WORKING' STATUS FROM DUAL
    UNION
    SELECT 'MARK' AS CUSTOMER,'NOT WORKING' STATUS FROM DUAL
    UNION
    SELECT 'SAM' AS CUSTOMER,'NOT WORKING' STATUS FROM DUAL
    UNION
    SELECT 'BIKAS' AS CUSTOMER,'NOT WORKING' STATUS FROM DUAL
    UNION
    SELECT 'MALLI' AS CUSTOMER,'NOT WORKING' STATUS FROM DUAL
    SELECT * FROM T2
    Need help in building the query.
    Thanks,
    Naren

    Hi,
    Naren wrote:
    Reasion, I have to implement similar sought of logic in Informatica (ETL tool). So first i thought of checking how we can build that logic in the database which for sure helps me out with Informatica.Are you saying that you need a solution that works in Informatica? Sorry, I don't know anything about Informatica. Maybe an Informatica forum would be a better palce to ask that question, rather than an Oracle forum.
    Here's an Oracle cross-join solution:
    WITH     cntr     AS
         SELECT     ROWNUM     AS n
         FROM     all_objects     -- or any table with at least 4 rows
         WHERE     ROWNUM     <= 4
    SELECT       CASE
               WHEN  c.n = 1  THEN  t.c1
               WHEN  c.n = 2  THEN  t.c2
               WHEN  c.n = 3  THEN  t.c3
               WHEN  c.n = 4  THEN  t.c4
           END     AS customer
    ,       t.status
    FROM       cntr  c
    CROSS JOIN     t
    ORDER BY  customer
    ;Perhaps this will be easy to convert for Infiormatica.
    Instead of generating a counter-table on the fly (which is easy to do in Oracle, at least in version 9.1 and higher), you might consider creating a real cntr table. that has, say, 100 rows, and only one column, n, with values 1, 2, 3, ..., 100.

  • Help needed for writing query

    help needed for writing query
    i have the following tables(with data) as mentioned below
    FK*-foregin key (SUBJECTS)
    FK**-foregin key (COMBINATION)
    1)SUBJECTS(table name)     
    SUB_ID(NUMBER) SUB_CODE(VARCHAR2) SUB_NAME (VARCHAR2)
    2           02           Computer Science
    3           03           Physics
    4           04           Chemistry
    5           05           Mathematics
    7           07           Commerce
    8           08           Computer Applications
    9           09           Biology
    2)COMBINATION
    COMB_ID(NUMBER) COMB_NAME(VARCHAR2) SUB_ID1(NUMBER(FK*)) SUB_ID2(NUMBER(FK*)) SUB_ID3(NUMBER(FK*)) SUBJ_ID4(NUMBER(FK*))
    383           S1      9           4           2           3
    384           S2      4           2           5           3
    ---------I actually designed the ABOVE table also like this
    3) a)COMBINATION
    COMB_ID(NUMBER) COMB_NAME(VARCHAR2)
    383           S1
    384           S2
    b)COMBINATION_DET
    COMBDET_ID(NUMBER) COMB_ID(FK**) SUB_ID(FK*)
    1               383          9
    2               383          4
    3               383          2
    4               383          3
    5               384          4
    6               384          2          
    7               384          5
    8               384          3
    Business rule: a combination consists of a maximum of 4 subjects (must contain)
    and the user is less relevant to a COMB_NAME(name of combinations) but user need
    the subjects contained in combinations
    i need the following output
    COMB_ID COMB_NAME SUBJECT1 SUBJECT2      SUBJECT3      SUBJECT4
    383     S1     Biology Chemistry      Computer Science Physics
    384     S2     Chemistry Computer Science Mathematics Physics
    or even this is enough(what i actually needed)
    COMB_ID     subjects
    383           Biology,Chemistry,Computer Science,Physics
    384           Chemistry,Computer Science,Mathematics,Physics
    you can use any of the COMBINATION table(either (2) or (3))
    and i want to know
    1)which design is good in this case
    (i think SUB_ID1,SUB_ID2,SUB_ID3,SUB_ID4 is not a
    good method to link with same table but if 4 subjects only(and must) comes
    detail table is not neccessary )
    now i am achieving the result by program-coding in C# after getting the rows from oracle
    i am using oracle 9i (also ODP.NET)
    i want to know how can i get the result in the stored procedure itsef.
    2)how it could be designed in any other way.
    any help/suggestion is welcome
    thanks for your time --Pradeesh

    Well I forgot the table-alias, here now with:
    SELECT C.COMB_ID
    , C.COMB_NAME
    , (SELECT SUB_NAME
    FROM SUBJECTS
    WHERE SUB_ID = C.SUB_ID1) AS SUBJECT_NAME1
    , (SELECT SUB_NAME
    FROM SUBJECTS
    WHERE SUB_ID = C.SUB_ID2) AS SUBJECT_NAME2
    , (SELECT SUB_NAME
    FROM SUBJECTS
    WHERE SUB_ID = C.SUB_ID3) AS SUBJECT_NAME3
    , (SELECT SUB_NAME
    FROM SUBJECTS
    WHERE SUB_ID = C.SUB_ID4) AS SUBJECT_NAME4
    FROM COMBINATION C;
    As you need exactly 4 subjects, the columns-solution is just fine I would say.

  • HELP TO TUNE THIS QUERY

    Hi,
    I'm using below query in procedure.It's taking more more time can some one help to tune this query or advice to rewrite the query.
    Databse :10.1
    SELECT   'Reading Comprehension' TEST_NAME,T.TEST_END_DATE TEST_SESSION_DATE,
            C.POOL_VERSION_ID, I.CREATED_ON POOL_CREATED_DT,
            C.ITEM_ID, C.ITEM_RESPONSE_ID, S.STUDENT_ID_PK, C.RESPONSE_KEY, C.IS_CORRECT RESPONSE_IS_CORRECT,
            T.SCORE SCALE_SCORE, C.RESPONSE_DURATION, P.ITEM_KEY,
            T.TEST_SESSION_DETAIL_ID, SYSDATE CREATED_ON
           -- BULK COLLECT INTO TV_PSYCHO_DET
            FROM
            CAT_ITEM_PARAMETER P, CAT_ITEM_USER_RESPONSE C, TEST_SESSION_DETAIL T,
            TEST_SESSION S, ITEM_POOL_VERSION I, TEST_DETAIL D
            ,INSTITUTION E
            WHERE  TRUNC(T.TEST_END_DATE) BETWEEN TO_DATE('01-11-09','dd-mm-yy') AND TO_DATE('30-11-09','dd-mm-yy')
            AND D.TEST_NAME =  'Reading Comprehension'
            AND T.TEST_SESSION_STATUS_ID = 3
            AND I.POOL_AVAILABILITY='Y'
            AND P.PRETEST=0 AND C.RESTART_FLAG=0
            AND T.TEST_DETAIL_ID = D.TEST_DETAIL_ID
            AND S.TEST_SESSION_ID = T.TEST_SESSION_ID
            AND C.TEST_SESSION_DETAIL_ID = T.TEST_SESSION_DETAIL_ID
            AND S.INSTITUTION_ID=E.INSTITUTION_ID
            AND SUBSTR(E.INSTITUTION_ID_DISPLAY,8,3) <> '000'
            AND I.ITEM_ID = C.ITEM_ID
            AND P.ITEM_ID = I.ITEM_ID;expln plan
    Plan hash value: 3712814491                                                                                                      
    | Id  | Operation                                 | Name                        | Rows  | Bytes | Cost (%CPU)| Time     | Pstart|
    Pstop |                                                                                                                          
    |   0 | SELECT STATEMENT                          |                             | 50857 |  7151K| 93382   (1)| 00:18:41 |       |
          |                                                                                                                          
    |*  1 |  FILTER                                   |                             |       |       |            |          |       |
          |                                                                                                                          
    |*  2 |   HASH JOIN                               |                             | 50857 |  7151K| 93382   (1)| 00:18:41 |       |
          |                                                                                                                          
    |   3 |    PARTITION HASH ALL                     |                             |  2312 | 23120 |    25   (0)| 00:00:01 |     1 |
        5 |                                                                                                                          
    |*  4 |     TABLE ACCESS FULL                     | CAT_ITEM_PARAMETER          |  2312 | 23120 |    25   (0)| 00:00:01 |     1 |
        5 |                                                                                                                          
    |*  5 |    HASH JOIN                              |                             | 94938 |    12M| 93356   (1)| 00:18:41 |       |
          |                                                                                                                          
    |*  6 |     TABLE ACCESS FULL                     | ITEM_POOL_VERSION           |  9036 |   132K|    30   (0)| 00:00:01 |       |
          |                                                                                                                          
    |*  7 |     TABLE ACCESS BY GLOBAL INDEX ROWID    | CAT_ITEM_USER_RESPONSE      |     9 |   279 |    18   (0)| 00:00:01 | ROWID |
    ROWID |                                                                                                                          
    |   8 |      NESTED LOOPS                         |                             | 45349 |  5270K| 93325   (1)| 00:18:40 |       |
          |                                                                                                                          
    |*  9 |       HASH JOIN                           |                             |  4923 |   423K| 11377   (1)| 00:02:17 |       |
          |                                                                                                                          
    |* 10 |        INDEX FAST FULL SCAN               | INSTI_ID_NAME_COUN_DISP_IDX |  8165 |   111K|    18   (0)| 00:00:01 |       |
          |                                                                                                                          
    |* 11 |        HASH JOIN                          |                             |  4923 |   355K| 11359   (1)| 00:02:17 |       |
          |                                                                                                                          
    |* 12 |         TABLE ACCESS BY GLOBAL INDEX ROWID| TEST_SESSION_DETAIL         |  4107 |   148K|  6804   (1)| 00:01:22 | ROWID |
    ROWID |                                                                                                                          
    |  13 |          NESTED LOOPS                     |                             |  4923 |   278K|  6806   (1)| 00:01:22 |       |
          |                                                                                                                          
    |* 14 |           INDEX RANGE SCAN                | TEST_DETAIL_AK_1            |     1 |    21 |     2   (0)| 00:00:01 |       |
          |                                                                                                                          
    |* 15 |           INDEX RANGE SCAN                | TEST_SESSION_DETAIL_FK2_I   | 39737 |       |   102   (0)| 00:00:02 |       |
          |                                                                                                                          
    |  16 |         PARTITION HASH ALL                |                             |  1672K|    25M|  4546   (1)| 00:00:55 |     1 |
        5 |                                                                                                                          
    |  17 |          TABLE ACCESS FULL                | TEST_SESSION                |  1672K|    25M|  4546   (1)| 00:00:55 |     1 |
        5 |                                                                                                                          
    |* 18 |       INDEX RANGE SCAN                    | CAT_ITEM_USER_RESP_IDX1     |    18 |       |     3   (0)| 00:00:01 |       |
          |                                                                                                                          
    Predicate Information (identified by operation id):                                                                              
       1 - filter(TO_DATE('01-11-09','dd-mm-yy')<=TO_DATE('30-11-09','dd-mm-yy'))                                                    
       2 - access("P"."ITEM_ID"="I"."ITEM_ID")                                                                                       
       4 - filter("P"."PRETEST"=0)                                                                                                   
       5 - access("I"."ITEM_ID"="C"."ITEM_ID")                                                                                       
       6 - filter("I"."POOL_AVAILABILITY"='Y')                                                                                       
       7 - filter(TO_NUMBER("C"."RESTART_FLAG")=0)                                                                                   
       9 - access("S"."INSTITUTION_ID"="E"."INSTITUTION_ID")                                                                         
      10 - filter(SUBSTR("E"."INSTITUTION_ID_DISPLAY",8,3)<>'000')                                                                   
      11 - access("S"."TEST_SESSION_ID"="T"."TEST_SESSION_ID")                                                                       
      12 - filter(TRUNC(INTERNAL_FUNCTION("T"."TEST_END_DATE"))>=TO_DATE('01-11-09','dd-mm-yy') AND "T"."TEST_SESSION_STATUS_ID"=3   
                  AND TRUNC(INTERNAL_FUNCTION("T"."TEST_END_DATE"))<=TO_DATE('30-11-09','dd-mm-yy'))                                 
      14 - access("D"."TEST_NAME"='Reading Comprehension')                                                                           
      15 - access("T"."TEST_DETAIL_ID"="D"."TEST_DETAIL_ID")                                                                         
      18 - access("C"."TEST_SESSION_DETAIL_ID"="T"."TEST_SESSION_DETAIL_ID")                                                         
    43 rows selected.Edited by: user575115 on Dec 18, 2009 12:31 AM

    When you see something like ...
       7 - filter(TO_NUMBER("C"."RESTART_FLAG")=0)                                                                                    It means that Oracle had to do a conversion for you since you aren't using the proper data type in your query.
    That would mean IF there is an index on that column, it won't be useable...

  • Need help in the sql query

    i have a table
    source table :
    create table order(name_a varchar2(100),intid number);
    insert into order values('a',1);
    insert into order values('b',1);
    insert into order values('c',1);
    insert into order values('d',1);
    insert into order values('e',2);
    insert into order values('f',2);
    insert into order values('g',2);
    i need a query , which result in the below output :
    if i look for intid=1 the query should give a,b,c,d
    if i look for intid=2 the query should give f,g
    Thanks

    Hi,
    781649 wrote:
    i have a table
    source table :
    create table order(name_a varchar2(100),intid number);
    insert into order values('a',1);
    insert into order values('b',1);
    insert into order values('c',1);
    insert into order values('d',1);
    insert into order values('e',2);
    insert into order values('f',2);
    insert into order values('g',2);Thanks for posting the CREATE TABLE and INSERT statements; it's very helpful.
    i need a query , which result in the below output :
    if i look for intid=1 the query should give a,b,c,d
    if i look for intid=2 the query should give f,gDid you mean <b>e</b>,f,g ?
    That's called String Aggregation , and how to do it depends on your version of Oracle, and your exact requirements.
    See thie following page for several techniques:
    http://www.oracle-base.com/articles/10g/StringAggregationTechniques.php
    If you're using Oracle 10 (or higher), and it's important that the name_a's be in order in the output, then you can do this:
    WITH     got_r_num     AS
         SELECT     name_a
         ,     intid
         ,     ROW_NUMBER () OVER ( PARTITION BY  intid
                                   ORDER BY          name_a
                           )         AS r_num
         FROM     order_table                    -- ORDER is not a good table name
         WHERE     intid     IN (1)                    -- Optional
    SELECT     intid
    ,     SUBSTR ( SYS_CONNECT_BY_PATH (name_a, ',')
                , 2
                )     AS name_a_list
    FROM     got_r_num
    WHERE     CONNECT_BY_ISLEAF     = 1
    START WITH     r_num     = 1
    CONNECT BY     r_num     = PRIOR r_num + 1
         AND     intid     = PRIOR intid
    ;Starting in Oracle 11.2, LISTAGG is better.
    This does not assume that you are getting the output only for a single intid at a time. You can get any number of them in a suingle query. Of course, that number can be 1 if that's what you want.
    Edited by: Frank Kulash on Mar 24, 2011 12:19 PM

  • Need help in writing the query

    The initial data is as follows:
    ID PARENT_ID
    =============
    1 NULL
    2 NULL
    3 2
    4 6
    5 3
    6 7
    7 4
    8 NULL
    but output is as follows in such way that Parent_Id should be displayed in ID place
    ID PARENT_ID
    =============
    1 NULL
    2 NULL
    3 2
    8 NULL
    6 8
    4 6
    5 3
    7 4
    Can any one help how resolve this query?
    Thanks in advance

    Your output is inconsistent with initial data :) But
    select * from t start with parent_id is null connect by parent_id = prior id;

  • Need help in the following query

    Hi ,
    I have the following query to pull out data for a particular timeperiod( campaign)
    select
    da.acct_nr,smas.mrkt_id,smas.fld_sls_cmpgn_perd_id,smas.acct_key,smas.upln_acct_key,smas.awrd_sls_amt,smas.fld_net_sls_amt
    ,smas.stf_ind,da1.acct_nr,
    smas.fld_sls_cmpgn_perd_id,smas.acct_key,smas.upln_acct_key,smas.awrd_sls_amt,smas.fld_net_sls_amt
    ,smas.stf_ind,da1.acct_nr,prev.*
    From cdw.sum_mrkt_acct_sls smas
    join codi.dim_acct da
    on (smas.mrkt_id = da.mrkt_id and smas.acct_key = da.acct_key)
    join codi.dim_acct da1
    on (smas.mrkt_id = da1.mrkt_id and smas.UPLN_acct_key = da1.ACCT_KEY)
    where smas.acct_key in
    select dwnln_acct_key from codi.dim_ldrshp_genlgy
    where mrkt_id = 66 and fld_sls_cmpgn_perd_id = 20100304
    and root_upln_acct_key = (select acct_key from codi.dim_acct where acct_nr = '0032622' and mrkt_id = 66)
    and
    smas.mrkt_id = 66
    and smas.fld_sls_cmpgn_perd_id = 20100304
    and smas.sls_org_key <> -100
    order by 1
    So this query will pull out data for campaign 20100304
    No i want to modify the query to pull out data for 3 such campaigns.
    for a smas.acct_key
    so data will
    smas.acct_key all the data for 1st campaign , all the data for 2nd campaign. all the data for 3rd campaign
    Could you please help in modifying this query.
    Thanks

    How about this ??
    select da.acct_nr,
           smas.mrkt_id,
           smas.fld_sls_cmpgn_perd_id,
           smas.acct_key,
           smas.upln_acct_key,
           smas.awrd_sls_amt,
           smas.fld_net_sls_amt,
           smas.stf_ind,
           da1.acct_nr,
           smas.fld_sls_cmpgn_perd_id,
           smas.acct_key,
           smas.upln_acct_key,
           smas.awrd_sls_amt,
           smas.fld_net_sls_amt,
           smas.stf_ind,
           da1.acct_nr,
           prev.*
      From cdw.sum_mrkt_acct_sls smas
      join codi.dim_acct da on (smas.mrkt_id = da.mrkt_id and
                               smas.acct_key = da.acct_key)
      join codi.dim_acct da1 on (smas.mrkt_id = da1.mrkt_id and
                                smas.UPLN_acct_key = da1.ACCT_KEY)
    where smas.acct_key in (select dwnln_acct_key
                               from codi.dim_ldrshp_genlgy
                              where mrkt_id = 66
                                and fld_sls_cmpgn_perd_id in( 20100304,20100305,20100306)
                                and root_upln_acct_key =
                                    (select acct_key
                                       from codi.dim_acct
                                      where acct_nr = '0032622'
                                        and mrkt_id = 66))
       and smas.mrkt_id = 66
       and smas.fld_sls_cmpgn_perd_id in( 20100304,20100305,20100306)
       and smas.sls_org_key - 100
    order by 1

  • Need help in insert statment query

    Hi,
    I have a table T1 with values like
    col1
    1
    2
    3
    4
    I need to write insert statment for t2 as
    insert into t2(col1,col2) values ('AA',select col1 from t1);
    The output of T2 should be
    col1 col2
    AA 1
    AA 2
    AA 3
    AA 4
    Any help in modifying the query.
    Ashish

    What is wrong with this?
    INSERT INTO id_own_dw.id_t_dw_org_dq_tgt
           (cost_center_cod_vc_old,
            cost_center_cod_vc_new
         SELECT '3016052',<<<_-- this you can replace with your varibale in pl/sql block
                cost_center_cod_vc
           FROM id_own_dw.id_t_dw_msl_org_cctr2mkdiv
          WHERE busi_unit_cod_vc = '3016496'

  • Need help in refining the query

    Hello Experts,
    Need your help in refining the query further more.
    table structure 
    Mskey  Col A Col B
    1   empno [20141127-20151128]1234
    1   empno [20151201-99991231]232544
    1   salutation [20141127-99991231]Mrs
    1   salutation [20151127-99991231]Mr
    2   empno [20141127-20151128]1234
    2   empno [20151201-99991231]232544
    2   salutation [20141127-99991231]Mrs
    2   salutation [20151127-99991231]Mr
    My requirement is to find the list of overlapping records based on the dates
    user details may be varying from time to time as new data would be pushed through HR systems to identity store via an interface.
    The job is getting failed whenever there is any overlapping with dates. So we proactively decided to schedule a job in this regards which runs weekly and would let us know for which and all the users there is overlapping dates are there, so that we can send the list to HR team for pushing new data.
    Overlapping Issue Example:
    The users employee id for an year it is 1234 and later he moved to another department and his employee id got changed and it became 2345 remaining all details are same. So the HR systems send the data for this user as empno - [20141127-20151128]1234 and empno - [20151201-99991231]232544
    it means from 20141127 to 20151128 his employee no is 1234 and from 20151201 to 99991231 his employee would be 2345.
    This is a correct case and the tool would accept this data.
    the below cases are invald
    Case 1: 1 salutation [20141127-99991231]Mrs 1 salutation [20151127-99991231]Mr
    Case 2: 2 salutation [20141127-99991231]Mrs 2 salutation [20141127-99991231]Mr
    So we wanted to find these overlapping records from tables.
    My Query:
    I am able to successfully write query for the case 2 type but unable to write for case1.
    select id,colA
    count(left(ColB,CHARINDEX(']',ColB))) as 'Count'
    from tblA with (nolock)
    where id in (Select distinct id from tblb with (nolock))
    group by id, cola,left(ColB,CHARINDEX(']',ColB))
    having count(left(ColB,CHARINDEX(']',ColB)))>1

    Finally got the required answer with the below query
    WITH Cte AS
    SELECT ID,ColA,ColB,ROW_NUMBER() OVER(ORDER BY (SELECT 1)) AS RN,
    CAST(SUBSTRING(ColB,2,CHARINDEX('-',ColB)-2) AS DATE) AS StartDT,
    CAST(SUBSTRING(ColB,CHARINDEX('-',ColB)+1,8) AS DATE)  AS EndDT  FROM TblA
    SELECT c1.ID, c1.ColA,c1.ColB
    FROM Cte c1 JOIN Cte c2
    ON c1.RN != c2.RN
    AND c1.ID=c2.ID
    AND c1.ColA=c2.ColA
    AND (c1.StartDT BETWEEN c2.StartDT AND c2.EndDT OR c2.StartDT BETWEEN c1.StartDT AND c1.EndDT )

  • Need help in tunnig the query

    Experts,
    Plz help in tuning this query
    SELECT
    DECODE(WSH_DELIVERY_DETAILS.LOT_NUMBER,'5I27164/1U',1, TO_NUMBER(SUBSTR(WSH_DELIVERY_DETAILS.LOT_NUMBER, (LENGTH(WSH_DELIVERY_DETAILS.LOT_NUMBER) - INSTR(REVERSE(WSH_DELIVERY_DETAILS.LOT_NUMBER),'/') + 2), (INSTR(REVERSE(WSH_DELIVERY_DETAILS.LOT_NUMBER),'/')-1))) ) AS NO_OF_PLATES,
         P_FORM.DESCRIPTION FORMOFPRODUCT,
    HZ_PARTIES.PARTY_NAME CUSTOMER, HZ_CUST_ACCOUNTS.ACCOUNT_NUMBER ACCT_NUM,
         NVL(OE_ORDER_HEADERS_ALL.CUST_PO_NUMBER,'XXXX') CUST_PO,
         INITCAP(HZ_PARTIES.ADDRESS1) BILL_ADD1,
    INITCAP(HZ_PARTIES.ADDRESS2) BILL_ADD2,
    INITCAP(HZ_PARTIES.ADDRESS3) BILL_ADD3,
    INITCAP(HZ_PARTIES.ADDRESS4) BILL_ADD4,
         INITCAP(HZ_PARTIES.CITY) BILL_CITY,
    INITCAP(HZ_PARTIES.STATE) BILL_STATE,
    HZ_PARTIES.POSTAL_CODE BILL_PC,
         INITCAP(HZ_LOCATIONS.ADDRESS1) SHIP_ADD1,
    INITCAP(HZ_LOCATIONS.ADDRESS2) SHIP_ADD2,
    INITCAP(HZ_LOCATIONS.ADDRESS3) SHIP_ADD3,
    INITCAP(HZ_LOCATIONS.ADDRESS4) SHIP_ADD4,
         INITCAP(HZ_LOCATIONS.CITY) SHIP_CITY,
    INITCAP(HZ_LOCATIONS.STATE) SHIP_STATE,
    HZ_LOCATIONS.POSTAL_CODE SHIP_PC,
         OE_TRANSACTION_TYPES_TL.NAME CATEGORY ,
         OE_ORDER_HEADERS_ALL.ORDER_NUMBER,
    OE_ORDER_HEADERS_ALL.SOURCE_DOCUMENT_ID,
    OE_ORDER_HEADERS_ALL.HEADER_ID,
    OE_ORDER_HEADERS_ALL.FREIGHT_TERMS_CODE,
    /* to_char(oe_order_headers_all.ORDERED_DATE,'DD-MON-RR HH24:MI:SS') ORDER_DATE,*/
    to_char(sysdate,'DD/MM/RRRR HH24:MI:SS') ORDERED_DATE,
         ROWNUM,
    OE_ORDER_LINES_ALL.INVENTORY_ITEM_ID,
         OE_ORDER_LINES_ALL.ORDERED_ITEM,
         OE_ORDER_LINES_ALL.ORDERED_QUANTITY,
         WSH_DELIVERY_DETAILS.SHIPPED_QUANTITY+DECODE(WSH_DELIVERY_DETAILS.DELIVERY_DETAIL_ID,129881,.22,0) AS SHIPPED_QUANTITY1, (WSH_DELIVERY_DETAILS.SHIPPED_QUANTITY+DECODE(WSH_DELIVERY_DETAILS.DELIVERY_DETAIL_ID,129881,.22,0))*OE_ORDER_LINES_ALL.UNIT_SELLING_PRICE AS LINE_PRICE,
         OE_ORDER_LINES_ALL.UNIT_SELLING_PRICE,
         JA_IN_RG_I.FOR_HOME_USE_PAY_ED_VAL,
    ----      SUBSTR(JA_IN_RG_I.EXCISE_INVOICE_NUMBER,1,2)|| SUBSTR(JA_IN_RG_I.EXCISE_INVOICE_NUMBER,4,8) INVOICE_NO,
         JA_IN_RG_I.EXCISE_INVOICE_NUMBER INVOICE_NO,
    to_char(JA_IN_RG_I.CREATION_DATE,'DD/MM/RRRR') INVOICE_DATE1,
         to_char(JA_IN_RG_I.EXCISE_INVOICE_DATE,'DD-MON-RR HH24:MI:SS') INV_DATE,
         to_char(JA_IN_RG_I.CREATION_DATE,'DD/MM/RRRR HH24:MI:SS') INVOICE_DATE,
         JA_IN_RG_I.EXCISE_DUTY_RATE,
         JA_IN_RG_I.EXCISE_DUTY_AMOUNT,
         WSH_DELIVERY_DETAILS.LOT_NUMBER,
    WSH_TRIPS.CARRIER_ID CARRIER_ID,
    WSH_TRIPS.VEHICLE_NUM_PREFIX,
    WSH_TRIPS.SEAL_CODE,
    WSH_TRIPS.ROUTING_INSTRUCTIONS,
    WSH_TRIPS.OPERATOR, WSH_NEW_DELIVERIES. DELIVERY_ID,
    WSH_NEW_DELIVERIES. ADDITIONAL_SHIPMENT_INFO TRUCK_NO,
         WSH_TRIPS.MODE_OF_TRANSPORT VEHICLE_TYPE,
    WSH_TRIPS.ATTRIBUTE6||' '|| WSH_TRIPS.ATTRIBUTE7||' '|| WSH_TRIPS.ATTRIBUTE8 REMARKS,
         WSH_TRIPS.ATTRIBUTE1 LR_NO, WSH_TRIPS.ATTRIBUTE7,
         WSH_TRIPS.ATTRIBUTE2 LR_DATE,
         DECODE(WSH_TRIPS.ATTRIBUTE3, '', '', 'ARE NO '||WSH_TRIPS.ATTRIBUTE3) AS ARE_NO,
         DECODE(WSH_TRIPS.ATTRIBUTE6, '', '', WSH_TRIPS.ATTRIBUTE6) AS REMARK1,
         DECODE(WSH_TRIPS.ATTRIBUTE4, '', '', 'R.P. NO. ' ||WSH_TRIPS.ATTRIBUTE4) AS PERMIT,
         DECODE(WSH_TRIPS.ATTRIBUTE5, '', '', 'Export Under '||WSH_TRIPS.ATTRIBUTE5) AS EXPORT_UNDER,
         DECODE(WSH_NEW_DELIVERIES.PORT_OF_DISCHARGE,'','', 'Seal No : '||WSH_NEW_DELIVERIES.PORT_OF_DISCHARGE) AS SEAL_NO,
         DECODE(WSH_NEW_DELIVERIES.DESCRIPTION, '', '', 'Container No : ' ||WSH_NEW_DELIVERIES.DESCRIPTION) AS CONTAINER_NO,
         MTL_SYSTEM_ITEMS_B.ATTRIBUTE4 AS EXCISE_TARIFF_NO,
         --MTL_SYSTEM_ITEMS_B.DESCRIPTION,
         HZ_CUST_ACCOUNTS.ACCOUNT_NUMBER,
         JSW_LOT_PACKSLIP.PACKSLIP_NO PACKSLIP,
         PR_TYPE.DESCRIPTION ||' ,'||
         P_FORM.DESCRIPTION || ' ,'||
         PR_GRADE.DESCRIPTION ||' ,'||
         PR_QTY.DESCRIPTION || ' ,'||
         DECODE(SUBSTR(PR_STL.DESCRIPTION,1,3),'Not','', PR_STL.DESCRIPTION) DESCRIPTION ,
    HZ_CUST_ACCT_SITES_ALL.ATTRIBUTE7 S_CST_NO,
    HZ_CUST_ACCT_SITES_ALL.ATTRIBUTE6 S_LST_NO,
    HZ_CUST_ACCT_SITES_ALL.ATTRIBUTE5 S_ECC_NO,
    A.ATTRIBUTE7 B_CST_NO,
    A.ATTRIBUTE6 B_LST_NO,
    INTERFACECRM.JSW_MES_COMMON_TNGL_T.JSW_ADDMINUTES(WSH_TRIPS.VEHICLE_NUM_PREFIX) POSTFIX_NUM
    FROM -- jsw_vat_slno,
         MTL_PARAMETERS,
         HZ_PARTIES,
         HZ_CUST_ACCOUNTS,
         HZ_LOCATIONS,
         HZ_PARTY_SITES,
         HZ_CUST_ACCT_SITES_ALL,
         HZ_CUST_SITE_USES_ALL,
    HZ_CUST_ACCT_SITES_ALL A,
    HZ_CUST_SITE_USES_ALL B,
         OE_TRANSACTION_TYPES_TL,
         JA_IN_RG_I,
         WSH_DELIVERY_DETAILS,
         OE_ORDER_LINES_ALL,
         OE_ORDER_HEADERS_ALL,
         WSH_DELIVERY_ASSIGNMENTS,
         WSH_NEW_DELIVERIES,
         WSH_DELIVERY_LEGS,
         WSH_TRIP_STOPS,
         WSH_TRIPS,
         MTL_SYSTEM_ITEMS_B,
         JSW_LOT_PACKSLIP,
         IC_TRAN_PND,
                   JSW_ITM_SEARCH_PRD_TYP          PR_TYPE,
              JSW_ITM_SEARCH_PRD_FRM          P_FORM     ,
                   JSW_ITM_SEARCH_PRD_GRD          PR_GRADE,      
              JSW_ITM_SEARCH_QUALITY_LEVEL PR_QTY,      
                   JSW_ITEM_SEARCH_SLITTING      PR_STL
    WHERE /* jsw_vat_slno.excise_invoice_number=ja_in_rg_i.excise_invoice_number and jsw_vat_slno.organization_id = ja_in_rg_i.organization_id */
    HZ_PARTIES.PARTY_ID = HZ_CUST_ACCOUNTS.PARTY_ID
         AND HZ_CUST_ACCOUNTS.CUST_ACCOUNT_ID = OE_ORDER_HEADERS_ALL.SOLD_TO_ORG_ID
         AND OE_ORDER_HEADERS_ALL.SHIP_TO_ORG_ID = HZ_CUST_SITE_USES_ALL.SITE_USE_ID
         AND HZ_CUST_SITE_USES_ALL.CUST_ACCT_SITE_ID = HZ_CUST_ACCT_SITES_ALL.CUST_ACCT_SITE_ID
    AND OE_ORDER_HEADERS_ALL.INVOICE_TO_ORG_ID = B.SITE_USE_ID
         AND B.CUST_ACCT_SITE_ID                          = A.CUST_ACCT_SITE_ID
         AND HZ_CUST_ACCT_SITES_ALL.PARTY_SITE_ID      = HZ_PARTY_SITES.PARTY_SITE_ID
         AND HZ_PARTY_SITES.LOCATION_ID = HZ_LOCATIONS.LOCATION_ID
         AND OE_ORDER_LINES_ALL.SHIP_FROM_ORG_ID = MTL_PARAMETERS.ORGANIZATION_ID
         AND OE_ORDER_HEADERS_ALL.ORDER_TYPE_ID = OE_TRANSACTION_TYPES_TL.TRANSACTION_TYPE_ID
    and upper(oe_transaction_types_tl.description) not like '%SUP%'
         AND OE_ORDER_HEADERS_ALL.HEADER_ID = WSH_DELIVERY_DETAILS.SOURCE_HEADER_ID
         AND OE_ORDER_HEADERS_ALL.HEADER_ID = OE_ORDER_LINES_ALL.HEADER_ID
         AND OE_ORDER_LINES_ALL.LINE_ID = WSH_DELIVERY_DETAILS.SOURCE_LINE_ID
         AND TO_CHAR(WSH_DELIVERY_DETAILS.DELIVERY_DETAIL_ID ) = JA_IN_RG_I.REF_DOC_ID
         AND WSH_DELIVERY_DETAILS.DELIVERY_DETAIL_ID = WSH_DELIVERY_ASSIGNMENTS.DELIVERY_DETAIL_ID
         AND WSH_DELIVERY_ASSIGNMENTS.DELIVERY_ID = WSH_NEW_DELIVERIES.DELIVERY_ID
         AND WSH_NEW_DELIVERIES.DELIVERY_ID = WSH_DELIVERY_LEGS.DELIVERY_ID
         AND WSH_DELIVERY_LEGS.PICK_UP_STOP_ID = WSH_TRIP_STOPS.STOP_ID
         AND WSH_TRIP_STOPS.TRIP_ID                = WSH_TRIPS.TRIP_ID
    /* Input parameter can be either Invoice number or Delivery Id */
         AND (WSH_NEW_DELIVERIES.DELIVERY_ID = :DEL_ID OR JA_IN_RG_I.EXCISE_INVOICE_NUMBER = :INVOICE_NUMBER)
         AND NVL(WSH_DELIVERY_DETAILS.SHIPPED_QUANTITY,0) <>0
         AND     MTL_SYSTEM_ITEMS_B.ORGANIZATION_ID     = WSH_DELIVERY_DETAILS.ORGANIZATION_ID
         AND     MTL_SYSTEM_ITEMS_B.INVENTORY_ITEM_ID = WSH_DELIVERY_DETAILS.INVENTORY_ITEM_ID
         AND IC_TRAN_PND.LINE_DETAIL_ID               = WSH_DELIVERY_DETAILS.DELIVERY_DETAIL_ID
         AND     JSW_LOT_PACKSLIP.LOT_ID                    = IC_TRAN_PND.LOT_ID(+)
         AND     JSW_LOT_PACKSLIP.DELIVERY_ID          = WSH_NEW_DELIVERIES.DELIVERY_ID(+)
         AND      IC_TRAN_PND.LINE_ID                    = OE_ORDER_LINES_ALL.LINE_ID                                                  
         AND     IC_TRAN_PND.DELETE_MARK                = 0
         AND IC_TRAN_PND.COMPLETED_IND      = 1
         AND      IC_TRAN_PND.STAGED_IND                    =     1
         AND     P_FORM.CODE(+)                                   = substr(OE_ORDER_LINES_ALL.ORDERED_ITEM,3,2)
         AND     PR_TYPE.CODE                                   = substr(OE_ORDER_LINES_ALL.ORDERED_ITEM,1,2)
         AND     PR_TYPE.CODE                                   = P_FORM.PRD_TYPE_CODE
         AND     P_FORM.CODE                                   = substr(OE_ORDER_LINES_ALL.ORDERED_ITEM,3,2)
    AND     P_FORM.CODE                                   = PR_GRADE.PRD_FRM_CODE
         AND     PR_GRADE.CODE                                   = substr(OE_ORDER_LINES_ALL.ORDERED_ITEM,5,2)     
         AND     PR_GRADE.CODE                                   = PR_QTY.PRD_GRD_CODE
         AND     PR_QTY.CODE                                   = substr(OE_ORDER_LINES_ALL.ORDERED_ITEM,7,2)
         AND     PR_STL.PROD_CODE                              = PR_TYPE.CODE
         AND     PR_STL.QUALITY_LEVEL_CODE                    = PR_QTY.CODE
         AND     PR_STL.CODE                                   = substr(OE_ORDER_LINES_ALL.ORDERED_ITEM,9,2)
         AND rownum=1
    thanks,
    baskar.l

    Here is the explain plan
    PLAN_TABLE_OUTPUT
    | Id | Operation | Name | Rows | Bytes | Cost |
    | 0 | SELECT STATEMENT | | 1 | 920 | 12628 |
    | 1 | COUNT STOPKEY | | | | |
    | 2 | NESTED LOOPS | | 1 | 920 | 12628 |
    | 3 | NESTED LOOPS | | 1 | 728 | 12627 |
    | 4 | NESTED LOOPS | | 1 | 718 | 12626 |
    | 5 | NESTED LOOPS | | 1 | 684 | 12625 |
    | 6 | NESTED LOOPS | | 1 | 674 | 12624 |
    | 7 | NESTED LOOPS | | 1 | 665 | 12623 |
    | 8 | NESTED LOOPS | | 1 | 655 | 12622 |
    | 9 | NESTED LOOPS | | 1 | 532 | 12621 |
    | 10 | NESTED LOOPS | | 1 | 522 | 12620 |
    | 11 | FILTER | | | | |
    | 12 | NESTED LOOPS OUTER | | | | |
    | 13 | NESTED LOOPS | | 1 | 494 | 12617 |
    | 14 | NESTED LOOPS | | 1 | 477 | 12614 |
    | 15 | NESTED LOOPS | | 1 | 467 | 12611 |
    | 16 | NESTED LOOPS | | 1 | 382 | 12610 |
    | 17 | NESTED LOOPS | | 1 | 366 | 12609 |
    | 18 | NESTED LOOPS | | 1 | 321 | 12606 |
    | 19 | FILTER | | | | |
    | 20 | HASH JOIN OUTER | | | | |
    | 21 | TABLE ACCESS BY INDEX ROWID| IC_TRAN_PND | 1 | 22 | 3 |
    | 22 | NESTED LOOPS | | 1 | 292 | 12601 |
    | 23 | HASH JOIN | | 1 | 270 | 12598 |
    | 24 | NESTED LOOPS | | 4011 | 908K| 6272 |
    | 25 | HASH JOIN | | 4012 | 893K| 6272 |
    | 26 | TABLE ACCESS FULL | JSW_ITM_SEARCH_PRD_GRD | 33 | 759 | 2 |
    | 27 | HASH JOIN | | 20547 | 4113K| 6269 |
    | 28 | TABLE ACCESS FULL | JSW_ITM_SEARCH_QUALITY_LEVEL | 54 | 1296 | 2 |
    | 29 | HASH JOIN | | 24352 | 4304K| 6266 |
    | 30 | TABLE ACCESS FULL | JSW_ITM_SEARCH_PRD_TYP | 7 | 154 | 2 |
    | 31 | HASH JOIN | | 170K| 25M| 6260 |
    | 32 | TABLE ACCESS FULL | JSW_ITEM_SEARCH_SLITTING | 73 | 1971 | 2 |
    | 33 | HASH JOIN | | 44367 | 5719K| 6257 |
    | 34 | HASH JOIN | | 4469 | 410K| 367 |
    | 35 | TABLE ACCESS FULL| OE_TRANSACTION_TYPES_TL | 18 | 846 | 4 |
    | 36 | TABLE ACCESS FULL| OE_ORDER_HEADERS_ALL | 38240 | 1755K| 362 |
    | 37 | TABLE ACCESS FULL | OE_ORDER_LINES_ALL | 379K| 13M| 5761 |
    | 38 | INDEX UNIQUE SCAN | MTL_PARAMETERS_U1 | 1 | 4 | |
    | 39 | TABLE ACCESS FULL | WSH_DELIVERY_DETAILS | 579K| 21M| 5926 |
    | 40 | INDEX RANGE SCAN | IC_TRAN_PND_N1 | 2 | | 2 |
    | 41 | TABLE ACCESS FULL | JSW_ITM_SEARCH_PRD_FRM | 16 | 272 | 2 |
    | 42 | TABLE ACCESS BY INDEX ROWID | MTL_SYSTEM_ITEMS_B | 1 | 12 | 2 |
    | 43 | INDEX UNIQUE SCAN | MTL_SYSTEM_ITEMS_B_U1 | 1 | | 1 |
    | 44 | TABLE ACCESS BY INDEX ROWID | JA_IN_RG_I | 1 | 45 | 3 |
    | 45 | INDEX RANGE SCAN | JA_IN_RG_I_N3 | 1 | | 2 |
    | 46 | TABLE ACCESS BY INDEX ROWID | HZ_CUST_ACCOUNTS | 1 | 16 | 1 |
    | 47 | INDEX UNIQUE SCAN | HZ_CUST_ACCOUNTS_U1 | 1 | | |
    | 48 | TABLE ACCESS BY INDEX ROWID | HZ_PARTIES | 1 | 85 | 1 |
    | 49 | INDEX UNIQUE SCAN | HZ_PARTIES_U1 | 1 | | |
    | 50 | TABLE ACCESS BY INDEX ROWID | WSH_DELIVERY_ASSIGNMENTS | 1 | 10 | 3 |
    | 51 | INDEX RANGE SCAN | WSH_DELIVERY_ASSIGNMENTS_N3 | 1 | | 2 |
    | 52 | TABLE ACCESS BY INDEX ROWID | JSW_LOT_PACKSLIP | 1 | 17 | 3 |
    | 53 | INDEX RANGE SCAN | JSW_LOT_PACKSLIP_N1 | 1 | | 2 |
    | 54 | TABLE ACCESS BY INDEX ROWID | WSH_NEW_DELIVERIES | 1 | 18 | 1 |
    | 55 | INDEX UNIQUE SCAN | WSH_NEW_DELIVERIES_U1 | 1 | | |
    | 56 | TABLE ACCESS BY INDEX ROWID | WSH_DELIVERY_LEGS | 1 | 10 | 2 |
    | 57 | INDEX RANGE SCAN | WSH_DELIVERY_LEGS_N1 | 1 | | 1 |
    | 58 | TABLE ACCESS BY INDEX ROWID | WSH_TRIP_STOPS | 1 | 10 | 1 |
    | 59 | INDEX UNIQUE SCAN | WSH_TRIP_STOPS_U1 | 1 | | |
    | 60 | TABLE ACCESS BY INDEX ROWID | WSH_TRIPS | 1 | 123 | 1 |
    | 61 | INDEX UNIQUE SCAN | WSH_TRIPS_U1 | 1 | | |
    | 62 | TABLE ACCESS BY INDEX ROWID | HZ_CUST_SITE_USES_ALL | 1 | 10 | 1 |
    | 63 | INDEX UNIQUE SCAN | HZ_CUST_SITE_USES_U1 | 1 | | |
    | 64 | TABLE ACCESS BY INDEX ROWID | HZ_CUST_ACCT_SITES_ALL | 1 | 9 | 1 |
    | 65 | INDEX UNIQUE SCAN | HZ_CUST_ACCT_SITES_U1 | 1 | | |
    | 66 | TABLE ACCESS BY INDEX ROWID | HZ_CUST_SITE_USES_ALL | 1 | 10 | 1 |
    | 67 | INDEX UNIQUE SCAN | HZ_CUST_SITE_USES_U1 | 1 | | |
    | 68 | TABLE ACCESS BY INDEX ROWID | HZ_CUST_ACCT_SITES_ALL | 1 | 34 | 1 |
    | 69 | INDEX UNIQUE SCAN | HZ_CUST_ACCT_SITES_U1 | 1 | | |
    | 70 | TABLE ACCESS BY INDEX ROWID | HZ_PARTY_SITES | 1 | 10 | 1 |
    | 71 | INDEX UNIQUE SCAN | HZ_PARTY_SITES_U1 | 1 | | |
    | 72 | TABLE ACCESS BY INDEX ROWID | HZ_LOCATIONS | 1 | 192 | 1 |
    | 73 | INDEX UNIQUE SCAN | HZ_LOCATIONS_U1 | 1 | | |
    we need to tune this query to reduce it time of execution as our users are very much affected in running requests.
    thanks,
    baskar.l

  • Help in writing a query!

    Dear All,
    My database is 11gR2 on Linux.
    I am struck in writing a difficult query, need help from your guys.
    I have a table, an application is controlling the columns of this table. Columns are added by the application.
    This is the structure of the table:
    create table imran_test(
    2 username varchar2(100),
    3 layer1 number(1),
    4 layer2 number(2),
    5 layer3 number(2));
    -- where layer1, layer2, layer3.... are increased up to layer 22 and could be increased more...
    Now each username will have 1 in any one of the layer, and all other layers will have 0
    Like if I consider the above table this could be the sample data:
    imran 1 0 0
    hafeez 0 1 0
    james 0 0 1
    Now the result my query should return is:
    select username, <column name where value is 1> and value of it, as per sample data, this should be return by the query:
    imran layer1 1
    hafeez layer2 1
    james layer 3 1
    Note: Please remember the columns are not fixed, they are added/altered by application.
    Regards,
    Imran

    The table design is incorrect as it is not in 3NF and has a repeating group
    You should correct the 'design' first or turn the layer... columns in a VARRAY.
    Also my feeling is you should change your subject line 'Help in writing a query!' in 'Write my query' as that is what you actually ask, and leave out the exclamation mark.
    Basically you dump everything in this forum, and being a bit less demanding might suit you better.
    Also you still act rude by not marking your questions as answered.
    Change that
    Sybrand Bakker
    Senior Oracle DBA
    Edited by: sybrand_b on 1-apr-2012 12:09

  • Help in group by query

    Hi,
    Please help me in the following query.
    SELECT
    a.bcast_id ,
    a.bcast_file_nm ,
    a.clickthru_url_cd ,
    a.list_id ,
    a.prs_id ,
    a.resp_cd ,
    a.run_seq ,
    COUNT(a.resp_cd) ,
    c.src_grp_cd ,
    b.site_cd
    FROM
    gca_cbx_response_test a ,
    gca_program_run_hist b ,
    campaign_master c
    WHERE
    a.list_id=b.list_id
    AND
    a.run_seq=b.campaign_run_seq
    AND
    b.campaign_cd=c.campaign_cd
    GROUP BY
    a.bcast_id ,
    a.bcast_file_nm ,
    a.clickthru_url_cd ,
    a.list_id ,
    a.prs_id ,
    a.resp_cd ,
    a.run_seq ,
    c.src_grp_cd ,
    b.site_cd
    HAVING
    a.resp_ts
    BETWEEN
    to_date('04-AUG-2005 00:01:32','DD-MON-YYYY HH24:MI:SS')
    AND
    to_date('04-AUG-2005 00:01:32','DD-MON-YYYY HH24:MI:SS');
    But the following group by query is throwing error that a.resp_ts is not a group by function.
    But i don't want to add a.resp_ts in group by clause.
    Please help me in this query.
    Thanks,
    Dilip

    How about:
    SELECT a.bcast_id ,
           a.bcast_file_nm ,
           a.clickthru_url_cd ,
           a.list_id ,
           a.prs_id ,
           a.resp_cd ,
           a.run_seq ,
           COUNT(a.resp_cd) ,
           c.src_grp_cd ,
           b.site_cd
    FROM   gca_cbx_response_test a ,
           gca_program_run_hist b ,
           campaign_master c
    WHERE  a.list_id=b.list_id
    AND    a.run_seq=b.campaign_run_seq
    AND    b.campaign_cd=c.campaign_cd
    AND    a.resp_ts BETWEEN to_date('04-AUG-2005 00:01:32','DD-MON-YYYY HH24:MI:SS')
                     AND to_date('04-AUG-2005 00:01:32','DD-MON-YYYY HH24:MI:SS');
    GROUP BY a.bcast_id ,
             a.bcast_file_nm ,
             a.clickthru_url_cd ,
             a.list_id ,
             a.prs_id ,
             a.resp_cd ,
             a.run_seq ,
             c.src_grp_cd ,
             b.site_cd;

  • Help in designing a Query

    Hello Everyone,
       I want you help in designing a query that involve OACT,OJDT,JDT1,OBGT and BGT1
    Actually I want a report that shows accounts Budget quarterly and its expenditure quarterly as well..
    Like shown below
    Account Name
    Budget from Jan to March
    Budget from April to June
    Budget from July to Sept
    Budget from Oct to Dec
    Expenditure from Jan to Mar
    Expenditure from Apr to June
    Expenditure from July to Sept
    Expenditure from Oct to Dec
                     Parameters would be Fiscal Year and Date range and Account name
    Can anyone please help me out in that one
    Thanks in advance

    Looking at the test data I have in these tables, it would appear impossible to join them all in one query without using UDFs...
    They don't share any columns.

  • Help tune this monster query please

    Hi, I have a query that runs for a long time. Its a system generated query but I need to improve its performance. I have enough indexes on the tables. Here is the query, its explain plan and the tkprof:
    The QUERY
    SELECT FD.FORM_ID, FD.FIELD_NAME, FD.FIELD_TEXT, FD.OPTION_TEXT, FD.OPTION_TYPE, FD.FIELD_ORDER, FD.LIST_ORDER,
    FD.MULTIPLE_ANSWER, FD.FIELD_INSTANCE, NVL(AD.APPS,0) APPS
    FROM (
        SELECT FIELD_NAME, FIELD_VALUE, INSTANCE, COUNT(0) APPS
        FROM APPLICATION_DATA AD
        WHERE APPLICATION_ID IN (
            SELECT A.APPLICATION_ID
            FROM APPLICATIONS A
            WHERE A.DELETED = 0 AND DECODE(A.TRAY,'Incomplete','Incomplete','Complete') = 'Complete' AND A.JOB_ID IN (
                SELECT JOB_ID
                FROM JOBS J
                WHERE J.ACCOUNT_ID IN (
                    SELECT ACCOUNT_ID
                        FROM AGENCY_ACCOUNTS CONNECT BY ACCOUNT_ID = PRIOR AGENCY_ID START WITH ACCOUNT_ID = J.ACCOUNT_ID ) )                  AND A.ACCOUNT_ID IN (
                        SELECT ACCOUNT_ID
                        FROM AGENCY_ACCOUNTS CONNECT BY ACCOUNT_ID = PRIOR AGENCY_ID START WITH ACCOUNT_ID = 113346 ) AND                       FIELD_NAME IN (
                            SELECT FIELD_NAME FROM APPLICATION_FIELDS
                                WHERE FIELD_TYPE IN ('Checkbox','Radio','Select','SelectM', 'RadioHoriz','RadioPara') ) )
        GROUP BY FIELD_NAME, FIELD_VALUE, INSTANCE ) AD, (
            SELECT AF.FORM_ID, AF.FIELD_NAME, AF.FIELD_TEXT, AFO.OPTION_TEXT, AFO.OPTION_TYPE, AF.FIELD_ORDER, AFO.LIST_ORDER,
            DECODE(AF.FIELD_TYPE,'Checkbox',1,'SelectM',1,0) MULTIPLE_ANSWER, AF.FIELD_INSTANCE
            FROM APPLICATION_FIELDS AF, APPLICATION_FIELD_OPTIONS AFO, JOB_FORMS JF
            WHERE AFO.FIELD_NAME = AF.FIELD_NAME AND AFO.FORM_ID = AF.FORM_ID AND JF.FORM_ID = AF.FORM_ID AND AF.FIELD_TYPE IN
            ('Checkbox','Radio','Select','SelectM', 'RadioHoriz','RadioPara') AND JF.JOB_ID IN (
                SELECT JOB_ID
                FROM JOBS J
                WHERE J.ACCOUNT_ID IN (
                    SELECT ACCOUNT_ID
                    FROM AGENCY_ACCOUNTS CONNECT BY ACCOUNT_ID = PRIOR AGENCY_ID START WITH ACCOUNT_ID = 113346 ) ) ) FD
    WHERE AD.FIELD_NAME = FD.FIELD_NAME AND AD.FIELD_VALUE = FD.OPTION_TEXT AND AD.INSTANCE = FD.FIELD_INSTANCE
    GROUP BY FD.FORM_ID, FD.FIELD_NAME, FD.FIELD_TEXT, FD.OPTION_TEXT, FD.OPTION_TYPE, FD.FIELD_ORDER, FD.LIST_ORDER,
    FD.MULTIPLE_ANSWER, FD.FIELD_INSTANCE, NVL(AD.APPS,0)
    ORDER BY FD.FORM_ID ASC, FD.FIELD_ORDER ASCEXPLAIN PLAN OUTPUT
    PLAN_TABLE_OUTPUT                                                                                                                                                                                      
    Plan hash value: 3519364953                                                                                                                                                                            
    | Id  | Operation                                   | Name                       | Rows  | Bytes | Cost (%CPU)| Time     |                                                                             
    |   0 | SELECT STATEMENT                            |                            |     1 |   120 |   763   (2)| 00:00:10 |                                                                             
    |   1 |  SORT GROUP BY                              |                            |     1 |   120 |   763   (2)| 00:00:10 |                                                                             
    |   2 |   VIEW                                      |                            |     1 |   120 |   762   (1)| 00:00:10 |                                                                             
    |   3 |    HASH GROUP BY                            |                            |     1 |   220 |   762   (1)| 00:00:10 |                                                                             
    |*  4 |     FILTER                                  |                            |       |       |            |          |                                                                             
    |*  5 |      TABLE ACCESS BY INDEX ROWID            | APPLICATION_DATA           |     1 |    45 |     6   (0)| 00:00:01 |                                                                             
    PLAN_TABLE_OUTPUT                                                                                                                                                                                      
    |   6 |       NESTED LOOPS                          |                            |    40 |  8800 |   486   (2)| 00:00:06 |                                                                             
    |*  7 |        HASH JOIN                            |                            |    60 | 10500 |   125   (4)| 00:00:02 |                                                                             
    |*  8 |         HASH JOIN                           |                            |    60 |  7320 |    53   (6)| 00:00:01 |                                                                             
    |   9 |          NESTED LOOPS                       |                            |    11 |   297 |     9  (12)| 00:00:01 |                                                                             
    |  10 |           VIEW                              | VW_NSO_3                   |     3 |    15 |     5   (0)| 00:00:01 |                                                                             
    |  11 |            HASH UNIQUE                      |                            |     3 |    69 |            |          |                                                                             
    |  12 |             TABLE ACCESS BY INDEX ROWID     | JOBS                       |     3 |    30 |     3   (0)| 00:00:01 |                                                                             
    |  13 |              NESTED LOOPS                   |                            |     3 |    69 |     5   (0)| 00:00:01 |                                                                             
    |  14 |               VIEW                          | VW_NSO_1                   |     1 |    13 |     2   (0)| 00:00:01 |                                                                             
    |* 15 |                CONNECT BY WITH FILTERING    |                            |       |       |            |          |                                                                             
    |  16 |                 TABLE ACCESS BY INDEX ROWID | AGENCY_ACCOUNTS            |       |       |            |          |                                                                             
    PLAN_TABLE_OUTPUT                                                                                                                                                                                      
    |* 17 |                  INDEX UNIQUE SCAN          | AGENCY_ACCOUNTS_ACCOUNT_ID |     1 |     5 |     1   (0)| 00:00:01 |                                                                             
    |  18 |                 NESTED LOOPS                |                            |       |       |            |          |                                                                             
    |  19 |                  BUFFER SORT                |                            |       |       |            |          |                                                                             
    |  20 |                   CONNECT BY PUMP           |                            |       |       |            |          |                                                                             
    |  21 |                  TABLE ACCESS BY INDEX ROWID| AGENCY_ACCOUNTS            |     1 |    10 |     2   (0)| 00:00:01 |                                                                             
    |* 22 |                   INDEX UNIQUE SCAN         | AGENCY_ACCOUNTS_ACCOUNT_ID |     1 |       |     1   (0)| 00:00:01 |                                                                             
    |* 23 |                 TABLE ACCESS FULL           | AGENCY_ACCOUNTS            |     1 |    10 |     2   (0)| 00:00:01 |                                                                             
    |* 24 |               INDEX RANGE SCAN              | JOBS_ACCOUNT_ID            |     3 |       |     1   (0)| 00:00:01 |                                                                             
    |* 25 |           INDEX RANGE SCAN                  | JOBS_FORMS_JID_FID         |     4 |    88 |     1   (0)| 00:00:01 |                                                                             
    |* 26 |          TABLE ACCESS FULL                  | APPLICATION_FIELDS         |  3579 |   332K|    43   (3)| 00:00:01 |                                                                             
    |  27 |         TABLE ACCESS FULL                   | APPLICATION_FIELD_OPTIONS  | 32897 |  1702K|    72   (3)| 00:00:01 |                                                                             
    PLAN_TABLE_OUTPUT                                                                                                                                                                                      
    |* 28 |        INDEX RANGE SCAN                     | IDX$$_B8D40001             |     4 |       |     3   (0)| 00:00:01 |                                                                             
    |* 29 |      FILTER                                 |                            |       |       |            |          |                                                                             
    |  30 |       MERGE JOIN CARTESIAN                  |                            |     3 |   225 |    11   (0)| 00:00:01 |                                                                             
    |  31 |        NESTED LOOPS                         |                            |     1 |    53 |     6   (0)| 00:00:01 |                                                                             
    |  32 |         NESTED LOOPS                        |                            |     1 |    40 |     4   (0)| 00:00:01 |                                                                             
    |* 33 |          TABLE ACCESS BY INDEX ROWID        | APPLICATIONS               |     1 |    30 |     3   (0)| 00:00:01 |                                                                             
    |* 34 |           INDEX UNIQUE SCAN                 | APPS_APP_ID                |     1 |       |     2   (0)| 00:00:01 |                                                                             
    |  35 |          TABLE ACCESS BY INDEX ROWID        | JOBS                       | 18780 |   183K|     1   (0)| 00:00:01 |                                                                             
    |* 36 |           INDEX UNIQUE SCAN                 | JOBS_JOB_ID                |     1 |       |     0   (0)| 00:00:01 |                                                                             
    |* 37 |         VIEW                                | VW_NSO_2                   |     1 |    13 |     2   (0)| 00:00:01 |                                                                             
    |* 38 |          CONNECT BY WITH FILTERING          |                            |       |       |            |          |                                                                             
    PLAN_TABLE_OUTPUT                                                                                                                                                                                      
    |  39 |           TABLE ACCESS BY INDEX ROWID       | AGENCY_ACCOUNTS            |       |       |            |          |                                                                             
    |* 40 |            INDEX UNIQUE SCAN                | AGENCY_ACCOUNTS_ACCOUNT_ID |     1 |     5 |     1   (0)| 00:00:01 |                                                                             
    |  41 |           NESTED LOOPS                      |                            |       |       |            |          |                                                                             
    |  42 |            BUFFER SORT                      |                            |       |       |            |          |                                                                             
    |  43 |             CONNECT BY PUMP                 |                            |       |       |            |          |                                                                             
    |  44 |            TABLE ACCESS BY INDEX ROWID      | AGENCY_ACCOUNTS            |     1 |    10 |     2   (0)| 00:00:01 |                                                                             
    |* 45 |             INDEX UNIQUE SCAN               | AGENCY_ACCOUNTS_ACCOUNT_ID |     1 |       |     1   (0)| 00:00:01 |                                                                             
    |* 46 |           TABLE ACCESS FULL                 | AGENCY_ACCOUNTS            |     1 |    10 |     2   (0)| 00:00:01 |                                                                             
    |  47 |        BUFFER SORT                          |                            |     3 |    66 |     9   (0)| 00:00:01 |                                                                             
    |  48 |         INLIST ITERATOR                     |                            |       |       |            |          |                                                                             
    |* 49 |          INDEX RANGE SCAN                   | IDX$$_91CA0001             |     3 |    66 |     5   (0)| 00:00:01 |                                                                             
    PLAN_TABLE_OUTPUT                                                                                                                                                                                      
    |* 50 |       FILTER                                |                            |       |       |            |          |                                                                             
    |* 51 |        CONNECT BY WITH FILTERING            |                            |       |       |            |          |                                                                             
    |  52 |         TABLE ACCESS BY INDEX ROWID         | AGENCY_ACCOUNTS            |       |       |            |          |                                                                             
    |* 53 |          INDEX UNIQUE SCAN                  | AGENCY_ACCOUNTS_ACCOUNT_ID |     1 |     5 |     1   (0)| 00:00:01 |                                                                             
    |  54 |         NESTED LOOPS                        |                            |       |       |            |          |                                                                             
    |  55 |          BUFFER SORT                        |                            |       |       |            |          |                                                                             
    |  56 |           CONNECT BY PUMP                   |                            |       |       |            |          |                                                                             
    |  57 |          TABLE ACCESS BY INDEX ROWID        | AGENCY_ACCOUNTS            |     1 |    10 |     2   (0)| 00:00:01 |                                                                             
    |* 58 |           INDEX UNIQUE SCAN                 | AGENCY_ACCOUNTS_ACCOUNT_ID |     1 |       |     1   (0)| 00:00:01 |                                                                             
    |* 59 |         TABLE ACCESS FULL                   | AGENCY_ACCOUNTS            |     1 |    10 |     2   (0)| 00:00:01 |                                                                             
    PLAN_TABLE_OUTPUT                                                                                                                                                                                      
    Predicate Information (identified by operation id):                                                                                                                                                    
       4 - filter( EXISTS (SELECT 0 FROM "APPLICATIONS" "A","JOBS" "SYS_ALIAS_1", (SELECT "ACCOUNT_ID" "$nso_col_1"                                                                                        
                  FROM "AGENCY_ACCOUNTS" "AGENCY_ACCOUNTS" WHERE "ACCOUNT_ID"=NULL) "VW_NSO_2","APPLICATION_FIELDS"                                                                                        
                  "APPLICATION_FIELDS" WHERE  EXISTS (SELECT 0 FROM "AGENCY_ACCOUNTS" "AGENCY_ACCOUNTS" WHERE "ACCOUNT_ID"=NULL AND                                                                        
                  ("ACCOUNT_ID"=:B1)) AND ("FIELD_TYPE"='Checkbox' OR "FIELD_TYPE"='Radio' OR "FIELD_TYPE"='RadioHoriz' OR                                                                                 
                  "FIELD_TYPE"='RadioPara' OR "FIELD_TYPE"='Select' OR "FIELD_TYPE"='SelectM') AND "FIELD_NAME"=:B2 AND                                                                                    
                  "A"."ACCOUNT_ID"="$nso_col_1" AND "A"."JOB_ID"="JOB_ID" AND "A"."APPLICATION_ID"=:B3 AND                                                                                                 
                  DECODE("A"."TRAY",'Incomplete','Incomplete','Complete')='Complete' AND "A"."DELETED"=0))                                                                                                 
    PLAN_TABLE_OUTPUT                                                                                                                                                                                      
       5 - filter("FIELD_NAME"="AF"."FIELD_NAME" AND "INSTANCE"="AF"."FIELD_INSTANCE")                                                                                                                     
       7 - access("AFO"."FIELD_NAME"="AF"."FIELD_NAME" AND "AFO"."FORM_ID"="AF"."FORM_ID")                                                                                                                 
       8 - access("JF"."FORM_ID"="AF"."FORM_ID")                                                                                                                                                           
      15 - filter("ACCOUNT_ID"=113346)                                                                                                                                                                     
      17 - access("ACCOUNT_ID"=113346)                                                                                                                                                                     
      22 - access("ACCOUNT_ID"=NULL)                                                                                                                                                                       
      23 - access("ACCOUNT_ID"=NULL)                                                                                                                                                                       
      24 - access("J"."ACCOUNT_ID"="$nso_col_1")                                                                                                                                                           
      25 - access("JF"."JOB_ID"="$nso_col_1")                                                                                                                                                              
      26 - filter("AF"."FIELD_TYPE"='Checkbox' OR "AF"."FIELD_TYPE"='Radio' OR "AF"."FIELD_TYPE"='RadioHoriz' OR                                                                                           
                  "AF"."FIELD_TYPE"='RadioPara' OR "AF"."FIELD_TYPE"='Select' OR "AF"."FIELD_TYPE"='SelectM')                                                                                              
    PLAN_TABLE_OUTPUT  FOR THE QUERY                                                                                                                                                                                    
      28 - access("FIELD_VALUE"="AFO"."OPTION_TEXT")                                                                                                                                                       
      29 - filter( EXISTS (SELECT 0 FROM "AGENCY_ACCOUNTS" "AGENCY_ACCOUNTS" WHERE "ACCOUNT_ID"=NULL AND                                                                                                   
                  ("ACCOUNT_ID"=:B1)))                                                                                                                                                                     
      33 - filter(DECODE("A"."TRAY",'Incomplete','Incomplete','Complete')='Complete' AND "A"."DELETED"=0)                                                                                                  
      34 - access("A"."APPLICATION_ID"=:B1)                                                                                                                                                                
      36 - access("A"."JOB_ID"="JOB_ID")                                                                                                                                                                   
      37 - filter("A"."ACCOUNT_ID"="$nso_col_1")                                                                                                                                                           
      38 - filter("ACCOUNT_ID"=113346)                                                                                                                                                                     
      40 - access("ACCOUNT_ID"=113346)                                                                                                                                                                     
      45 - access("ACCOUNT_ID"=NULL)                                                                                                                                                                       
      46 - access("ACCOUNT_ID"=NULL)                                                                                                                                                                       
    PLAN_TABLE_OUTPUT                                                                                                                                                                                      
      49 - access("FIELD_NAME"=:B1 AND ("FIELD_TYPE"='Checkbox' OR "FIELD_TYPE"='Radio' OR "FIELD_TYPE"='RadioHoriz'                                                                                       
                  OR "FIELD_TYPE"='RadioPara' OR "FIELD_TYPE"='Select' OR "FIELD_TYPE"='SelectM'))                                                                                                         
      50 - filter("ACCOUNT_ID"=:B1)                                                                                                                                                                        
      51 - filter("ACCOUNT_ID"=:B1)                                                                                                                                                                        
      53 - access("ACCOUNT_ID"=:B1)                                                                                                                                                                        
      58 - access("ACCOUNT_ID"=NULL)                                                                                                                                                                       
      59 - access("ACCOUNT_ID"=NULL)                                                                                                                                                                       
    106 rows selected.TKPROF FOR THE QUERY
    call     count       cpu    elapsed       disk      query    current        rows
    Parse        1      0.05       0.08          0          0          0           0
    Execute      1      0.00       0.00          0          0          0           0
    Fetch       17   1040.45    1018.36          0   48926569          0         229
    total       19   1040.50    1018.44          0   48926569          0         229
    Misses in library cache during parse: 1
    Optimizer mode: ALL_ROWS
    Parsing user id: 68
    Rows     Row Source Operation
        229  SORT GROUP BY (cr=48926569 pr=0 pw=0 time=1018362160 us)
        454   VIEW  (cr=48926569 pr=0 pw=0 time=1018361382 us)
        454    HASH GROUP BY (cr=48926569 pr=0 pw=0 time=1018360468 us)
    185302     FILTER  (cr=48926569 pr=0 pw=0 time=877249113 us)
    1440799      TABLE ACCESS BY INDEX ROWID APPLICATION_DATA (cr=11309333 pr=0 pw=0 time=213262026 us)
    82907114       NESTED LOOPS  (cr=174051 pr=0 pw=0 time=166906535 us)
       1790        HASH JOIN  (cr=625 pr=0 pw=0 time=41617 us)
        121         HASH JOIN  (cr=310 pr=0 pw=0 time=11263 us)
         39          NESTED LOOPS  (cr=121 pr=0 pw=0 time=6894 us)
         44           VIEW  VW_NSO_3 (cr=75 pr=0 pw=0 time=6424 us)
         44            HASH UNIQUE (cr=75 pr=0 pw=0 time=6333 us)
         44             TABLE ACCESS BY INDEX ROWID JOBS (cr=75 pr=0 pw=0 time=3090 us)
         52              NESTED LOOPS  (cr=32 pr=0 pw=0 time=58177 us)
          7               VIEW  VW_NSO_1 (cr=23 pr=0 pw=0 time=1242 us)
          7                CONNECT BY WITH FILTERING (cr=23 pr=0 pw=0 time=1212 us)
          1                 TABLE ACCESS BY INDEX ROWID AGENCY_ACCOUNTS (cr=3 pr=0 pw=0 time=185 us)
          1                  INDEX UNIQUE SCAN AGENCY_ACCOUNTS_ACCOUNT_ID (cr=2 pr=0 pw=0 time=100 us)(object id 60086)
          6                 NESTED LOOPS  (cr=20 pr=0 pw=0 time=590 us)
          7                  BUFFER SORT (cr=0 pr=0 pw=0 time=248 us)
          7                   CONNECT BY PUMP  (cr=0 pr=0 pw=0 time=95 us)
          6                  TABLE ACCESS BY INDEX ROWID AGENCY_ACCOUNTS (cr=20 pr=0 pw=0 time=314 us)
          6                   INDEX UNIQUE SCAN AGENCY_ACCOUNTS_ACCOUNT_ID (cr=14 pr=0 pw=0 time=188 us)(object id 60086)
          0                 TABLE ACCESS FULL AGENCY_ACCOUNTS (cr=0 pr=0 pw=0 time=0 us)
         44               INDEX RANGE SCAN JOBS_ACCOUNT_ID (cr=9 pr=0 pw=0 time=280 us)(object id 60253)
         39           INDEX RANGE SCAN JOBS_FORMS_JID_FID (cr=46 pr=0 pw=0 time=968 us)(object id 60299)
       3579          TABLE ACCESS FULL APPLICATION_FIELDS (cr=189 pr=0 pw=0 time=14469 us)
      32524         TABLE ACCESS FULL APPLICATION_FIELD_OPTIONS (cr=315 pr=0 pw=0 time=32629 us)
    82905323        INDEX RANGE SCAN IDX$$_B8D40001 (cr=173426 pr=0 pw=0 time=83870105 us)(object id 60121)
    185297      FILTER  (cr=37617236 pr=0 pw=0 time=749052070 us)
    185297       MERGE JOIN CARTESIAN (cr=37617196 pr=0 pw=0 time=742583363 us)
    185297        NESTED LOOPS  (cr=35393366 pr=0 pw=0 time=706772617 us)
    1139675         NESTED LOOPS  (cr=9180841 pr=0 pw=0 time=84949686 us)
    1140164          TABLE ACCESS BY INDEX ROWID APPLICATIONS (cr=5760834 pr=0 pw=0 time=46774108 us)
    1440060           INDEX UNIQUE SCAN APPS_APP_ID (cr=4320774 pr=0 pw=0 time=24380604 us)(object id 60107)
    1139675          TABLE ACCESS BY INDEX ROWID JOBS (cr=3420007 pr=0 pw=0 time=28126171 us)
    1139675           INDEX UNIQUE SCAN JOBS_JOB_ID (cr=2280328 pr=0 pw=0 time=14563524 us)(object id 60252)
    185297         VIEW  VW_NSO_2 (cr=26212525 pr=0 pw=0 time=613602445 us)
    6866175          CONNECT BY WITH FILTERING (cr=26212525 pr=0 pw=0 time=614183170 us)
    1139675           TABLE ACCESS BY INDEX ROWID AGENCY_ACCOUNTS (cr=3419025 pr=0 pw=0 time=24323520 us)
    1139675            INDEX UNIQUE SCAN AGENCY_ACCOUNTS_ACCOUNT_ID (cr=2279350 pr=0 pw=0 time=13905212 us)(object id 60086)
    6838050           NESTED LOOPS  (cr=22793500 pr=0 pw=0 time=388173853 us)
    7977725            BUFFER SORT (cr=0 pr=0 pw=0 time=178812411 us)
    7977725             CONNECT BY PUMP  (cr=0 pr=0 pw=0 time=57522094 us)
    6838050            TABLE ACCESS BY INDEX ROWID AGENCY_ACCOUNTS (cr=22793500 pr=0 pw=0 time=179425469 us)
    6838050             INDEX UNIQUE SCAN AGENCY_ACCOUNTS_ACCOUNT_ID (cr=15955450 pr=0 pw=0 time=113988543 us)(object id 60086)
          0           TABLE ACCESS FULL AGENCY_ACCOUNTS (cr=0 pr=0 pw=0 time=0 us)
    185297        BUFFER SORT (cr=2223830 pr=0 pw=0 time=24721936 us)
    1208625         INLIST ITERATOR  (cr=2223830 pr=0 pw=0 time=26470412 us)
    1208625          INDEX RANGE SCAN IDX$$_91CA0001 (cr=2223830 pr=0 pw=0 time=18174073 us)(object id 60129)
          2       FILTER  (cr=40 pr=0 pw=0 time=1292 us)
          2        CONNECT BY WITH FILTERING (cr=40 pr=0 pw=0 time=1276 us)
          2         TABLE ACCESS BY INDEX ROWID AGENCY_ACCOUNTS (cr=6 pr=0 pw=0 time=86 us)
          2          INDEX UNIQUE SCAN AGENCY_ACCOUNTS_ACCOUNT_ID (cr=4 pr=0 pw=0 time=46 us)(object id 60086)
         10         NESTED LOOPS  (cr=34 pr=0 pw=0 time=791 us)
         12          BUFFER SORT (cr=0 pr=0 pw=0 time=369 us)
         12           CONNECT BY PUMP  (cr=0 pr=0 pw=0 time=127 us)
         10          TABLE ACCESS BY INDEX ROWID AGENCY_ACCOUNTS (cr=34 pr=0 pw=0 time=376 us)
         10           INDEX UNIQUE SCAN AGENCY_ACCOUNTS_ACCOUNT_ID (cr=24 pr=0 pw=0 time=242 us)(object id 60086)
          0         TABLE ACCESS FULL AGENCY_ACCOUNTS (cr=0 pr=0 pw=0 time=0 us)Any help in tuning this query to run faster will be highly appreciated. I also noticed merge join certesian on the tkprof and explain plan. Any idea in takling that will also be appreciated.
    Cheers,
    Ade

    It's quite hard to tune a query without seeing the table structure and available indices as well as relationship between each other but I'll try.
    In your sql which you've given the table alias AD, you have this filter criteria
                  AND    FIELD_NAME IN (                        
                         SELECT FIELD_NAME
                         FROM   APPLICATION_FIELDS                            
                         WHERE  FIELD_TYPE IN ('Checkbox','Radio','Select','SelectM', 'RadioHoriz','RadioPara')
                         ) </br>
    My question is, is FIELD_NAME a column in the APPLICATION_DATA table? If it is, try moving this clause outside of the current sub-query it is in, i.e. one more level up. It looks like you put them inside the wrong level.
    I would also try converting this <COL> IN <SUB-QUERY> to a join, if that wouldn't affect the no. of rows returned. I can't tell for sure since I dont know the relationship between your tables.
    It looks to me like you're doing this
    SELECT <col list>
    FROM   ( SELECT                                         -- LEVEL 1
             FROM   APPLICATION_DATA AD
             WHERE  AD.APPLICAITON_ID IN (                  -- LEVEL 2
                    SELECT
                    FROM   APPLICATIONS A
                    WHERE  A.JOB_ID IN (
                           <SUBQUERY>
                    AND    A.ACCOUNT_ID IN (
                           <SUBQUERY>
                    AND FIELDNAME IN ( -- SHOULD BE INSIDE LEVEL1
    <SUBQUERY>
           ) AD,
           ( SELECT ....
           ) FD
    WHERE  ....

Maybe you are looking for