WITH Clause in Select

Hi,
Somebody to give an overview of WITH Clause in a SELECT stmt?.
Thanks,
Robert

Here:
http://download.oracle.com/docs/cd/B19306_01/server.102/b14223/aggreg.htm#sthref1670
and here...just by googling
http://www.psoug.org/reference/with.html

Similar Messages

  • With clause of select (subquery factoring)

    I've got a script that looks like this:
    with X as (select * from atable)
    select x1.*, x2.*
    from X x1,
    X x2
    This works perfectly well in SQL*Plus, in PL/SQL, etc. Yet when I try to add it as "New Folder from Database" to an existing Business area in Discoverer, I get the infamous "ORA-03001 Unimplemented Feature".
    What am I doing wrong?
    Oracle 9i, Disco 9.0.4

    Hi,
    You will have to define a database view containing your query and load the view into your database. I don't think you can use subquery factoring in a custom folder.
    Rod West

  • WITH-clause: performance- or readability enhancement?

    Hi,
    I was preparing for a course on with I also discuss the with-clause. Assuming it would perform better than just writing down two extra statements, I tried out the explain plan and found out that the with-clause actually performes worse. The example:
    Statment including WITH-clause:
    SQL> with country as
    2 (select c.country_id
    3 ,     c.country_name
    4 ,     c.region_id
    5 ,     r.region_name
    6 from countries c
    7 ,     regions r
    8 where c.region_id = r.region_id)
    9 select l.street_address
    10 ,     l.city
    11 ,     c.region_name
    12 from locations l
    13 ,     country c
    14 where l.country_id = c.country_id
    15 and c.country_id = 'UK'
    16 union
    17 select l.street_address
    18 ,     l.city
    19 ,     c.region_name
    20 from locations l
    21 ,     country c
    22 where l.country_id = c.country_id
    23 and c.country_id = 'NL'
    24 ;
    The cost of this executing is 11 as the explain plan tells me (dbms_xplan.display_cursor)
    Now, without WITH-clause:
    SQL> select l.street_address
    2 ,     l.city
    3 ,     c.region_name
    4 from locations l
    5 ,     (select c.country_id
    6      ,     c.country_name
    7      ,     c.region_id
    8      ,     r.region_name
    9      from countries c
    10      ,     regions r
    11      where c.region_id = r.region_id) c
    12 where l.country_id = c.country_id
    13 and c.country_id = 'UK'
    14 union
    15 select l.street_address
    16 ,     l.city
    17 ,     c.region_name
    18 from locations l
    19 ,     (select c.country_id
    20      ,     c.country_name
    21      ,     c.region_id
    22      ,     r.region_name
    23      from countries c
    24      ,     regions r
    25      where c.region_id = r.region_id) c
    26 where l.country_id = c.country_id
    27 and c.country_id = 'NL'
    28 ;
    Now explain plan tells me the cost is just 6! Why does this not perform faster. Are there examples at which it does? Or is the WITH-clause only to make the statements syntax more readable??

    Ok, here's the plan for the first query (including with) PLAN_TABLE_OUTPUT                                                                                                                                                                  
    Plan hash value: 744749920                                                                                                                                                         
    | Id  | Operation                       | Name                     | Rows  | Bytes | Cost (%CPU)| Time     |                                                                       
    |   0 | SELECT STATEMENT                |                          |       |       |    11 (100)|          |                                                                       
    |   1 |  TEMP TABLE TRANSFORMATION      |                          |       |       |            |          |                                                                       
    |   2 |   LOAD AS SELECT                |                          |       |       |            |          |                                                                       
    |   3 |    MERGE JOIN                   |                          |    25 |   700 |     4  (25)| 00:00:01 |                                                                       
    |   4 |     TABLE ACCESS BY INDEX ROWID | REGIONS                  |     4 |    56 |     2   (0)| 00:00:01 |                                                                       
    |   5 |      INDEX FULL SCAN            | REG_ID_PK                |     4 |       |     1   (0)| 00:00:01 |                                                                       
    PLAN_TABLE_OUTPUT                                                                                                                                                                  
    |*  6 |     SORT JOIN                   |                          |    25 |   350 |     2  (50)| 00:00:01 |                                                                       
    |   7 |      INDEX FULL SCAN            | COUNTRY_C_ID_PK          |    25 |   350 |     1   (0)| 00:00:01 |                                                                       
    |   8 |   SORT UNIQUE                   |                          |   100 |  5100 |    11  (64)| 00:00:01 |                                                                       
    |   9 |    UNION-ALL                    |                          |       |       |            |          |                                                                       
    |* 10 |     HASH JOIN                   |                          |    75 |  3825 |     5  (20)| 00:00:01 |                                                                       
    |  11 |      TABLE ACCESS BY INDEX ROWID| LOCATIONS                |     3 |    99 |     2   (0)| 00:00:01 |                                                                       
    |* 12 |       INDEX RANGE SCAN          | LOC_COUNTRY_IX           |     3 |       |     1   (0)| 00:00:01 |                                                                       
    |* 13 |      VIEW                       |                          |    25 |   450 |     2   (0)| 00:00:01 |                                                                       
    |  14 |       TABLE ACCESS FULL         | SYS_TEMP_0FD9D6602_FACCC |    25 |   650 |     2   (0)| 00:00:01 |                                                                       
    |* 15 |     HASH JOIN                   |                          |    25 |  1275 |     5  (20)| 00:00:01 |                                                                       
    |  16 |      TABLE ACCESS BY INDEX ROWID| LOCATIONS                |     1 |    33 |     2   (0)| 00:00:01 |                                                                       
    PLAN_TABLE_OUTPUT                                                                                                                                                                  
    |* 17 |       INDEX RANGE SCAN          | LOC_COUNTRY_IX           |     1 |       |     1   (0)| 00:00:01 |                                                                       
    |* 18 |      VIEW                       |                          |    25 |   450 |     2   (0)| 00:00:01 |                                                                       
    |  19 |       TABLE ACCESS FULL         | SYS_TEMP_0FD9D6602_FACCC |    25 |   650 |     2   (0)| 00:00:01 |                                                                       
    Predicate Information (identified by operation id):                                                                                                                                
       6 - access("C"."REGION_ID"="R"."REGION_ID")                                                                                                                                     
           filter("C"."REGION_ID"="R"."REGION_ID")                                                                                                                                     
      10 - access("L"."COUNTRY_ID"="C"."COUNTRY_ID")                                                                                                                                   
    PLAN_TABLE_OUTPUT                                                                                                                                                                  
      12 - access("L"."COUNTRY_ID"='UK')                                                                                                                                               
      13 - filter("C"."COUNTRY_ID"='UK')                                                                                                                                               
      15 - access("L"."COUNTRY_ID"="C"."COUNTRY_ID")                                                                                                                                   
      17 - access("L"."COUNTRY_ID"='NL')                                                                                                                                               
      18 - filter("C"."COUNTRY_ID"='NL')                                                                                                                                               
                                                 And this is the plan for the query WITHOUT the with-clause:
    Plan hash value: 3987129437                                                                                                                                                        
    | Id  | Operation                       | Name            | Rows  | Bytes | Cost (%CPU)| Time     |                                                                                
    |   0 | SELECT STATEMENT                |                 |       |       |     6 (100)|          |                                                                                
    |   1 |  SORT UNIQUE                    |                 |     4 |   212 |     6  (67)| 00:00:01 |                                                                                
    |   2 |   UNION-ALL                     |                 |       |       |            |          |                                                                                
    PLAN_TABLE_OUTPUT                                                                                                                                                                  
    |   3 |    NESTED LOOPS                 |                 |     3 |   159 |     2   (0)| 00:00:01 |                                                                                
    |   4 |     NESTED LOOPS                |                 |     1 |    20 |     1   (0)| 00:00:01 |                                                                                
    |*  5 |      INDEX UNIQUE SCAN          | COUNTRY_C_ID_PK |     1 |     6 |     0   (0)|          |                                                                                
    |   6 |      TABLE ACCESS BY INDEX ROWID| REGIONS         |     4 |    56 |     1   (0)| 00:00:01 |                                                                                
    |*  7 |       INDEX UNIQUE SCAN         | REG_ID_PK       |     1 |       |     0   (0)|          |                                                                                
    |   8 |     TABLE ACCESS BY INDEX ROWID | LOCATIONS       |     3 |    99 |     1   (0)| 00:00:01 |                                                                                
    |*  9 |      INDEX RANGE SCAN           | LOC_COUNTRY_IX  |     3 |       |     0   (0)|          |                                                                                
    |  10 |    NESTED LOOPS                 |                 |     1 |    53 |     2   (0)| 00:00:01 |                                                                                
    |  11 |     NESTED LOOPS                |                 |     1 |    20 |     1   (0)| 00:00:01 |                                                                                
    |* 12 |      INDEX UNIQUE SCAN          | COUNTRY_C_ID_PK |     1 |     6 |     0   (0)|          |                                                                                
    |  13 |      TABLE ACCESS BY INDEX ROWID| REGIONS         |     4 |    56 |     1   (0)| 00:00:01 |                                                                                
    PLAN_TABLE_OUTPUT                                                                                                                                                                  
    |* 14 |       INDEX UNIQUE SCAN         | REG_ID_PK       |     1 |       |     0   (0)|          |                                                                                
    |  15 |     TABLE ACCESS BY INDEX ROWID | LOCATIONS       |     1 |    33 |     1   (0)| 00:00:01 |                                                                                
    |* 16 |      INDEX RANGE SCAN           | LOC_COUNTRY_IX  |     1 |       |     0   (0)|          |                                                                                
    Predicate Information (identified by operation id):                                                                                                                                
       5 - access("C"."COUNTRY_ID"='UK')                                                                                                                                               
       7 - access("C"."REGION_ID"="R"."REGION_ID")                                                                                                                                     
       9 - access("L"."COUNTRY_ID"='UK')                                                                                                                                               
    PLAN_TABLE_OUTPUT                                                                                                                                                                  
      12 - access("C"."COUNTRY_ID"='NL')                                                                                                                                               
      14 - access("C"."REGION_ID"="R"."REGION_ID")                                                                                                                                     
      16 - access("L"."COUNTRY_ID"='NL')                                                                                                                                               
                                                                                                                                                                                        As you can see: WITH does not appear to get me any performance enhancement...

  • Retrieving OpenXml data without 'with' clause

    I have a stored procedure which takes input as XML parameter which converts that into a table.My problem is that I don't to use 'with' clause in select statement when i retrieve data from openXml.
    Here My code.
    DECLARE @DocHandle int
    DECLARE @XmlDocument nvarchar(1000)
    SET @XmlDocument = N'<ROOT>
    <Customer CustomerID="VINET" ContactName="Paul Henriot">
    <Order OrderID="10248" CustomerID="VINET" EmployeeID="5"
    OrderDate="1996-07-04T00:00:00">
    <OrderDetail ProductID="11" Quantity="12"/>
    <OrderDetail ProductID="42" Quantity="10"/>
    </Order>
    </Customer>
    <Customer CustomerID="LILAS" ContactName="Carlos Gonzlez">
    <Order OrderID="10283" CustomerID="LILAS" EmployeeID="3"
    OrderDate="1996-08-16T00:00:00">
    <OrderDetail ProductID="72" Quantity="3"/>
    </Order>
    </Customer>
    </ROOT>'
    -- Create an internal representation of the XML document.
    EXEC sp_xml_preparedocument @DocHandle OUTPUT, @XmlDocument
    -- Execute a SELECT statement using OPENXML rowset provider.
    SELECT *
    FROM OPENXML (@DocHandle,'/ROOT/Customer',1) WITH (CustomerID varchar(10),---I dont want to use this
    EXEC sp_xml_removedocument @DocHandle
     

    Well, the WITH clause is optional. :-) If you remove it, you get a so-called
    edge table. Exactly what one would use it for, I have not been able to
    figure out.
    But you don't need to use OPENXML at all. Here's an example using XQuery
    instead:
    DECLARE @x xml
    SELECT @x =
      N'<Orders>
          <Order OrderID="13000" CustomerID="ALFKI"
                 OrderDate="2006-09-20Z" EmployeeID="2">
             <OrderDetails ProductID="76" Price="123" Qty = "10"/>
             <OrderDetails ProductID="16" Price="3.23" Qty = "20"/>
          </Order>
          <Order OrderID="13001" CustomerID="VINET"
                 OrderDate="2006-09-20Z" EmployeeID="1">
             <OrderDetails ProductID="12" Price="12.23" Qty = "1"/>
          </Order>
        </Orders>'
    SELECT OrderID    = T.Item.value('@OrderID[1]',    'int'),
           CustomerID = T.Item.value('@CustomerID[1]', 'nchar(5)'),
           OrderDate  = T.Item.value('@OrderDate[1]',  'datetime'),
           EmployeeId = T.Item.value('@EmployeeID[1]', 'smallint')
    FROM   @x.nodes('/Orders/Order') AS T(Item)
    SELECT OrderID    = A.Item.value('@OrderID[1]',    'int'),
           ProductID  = B.Item.value('@ProductID[1]',  'smallint'),
           Price      = B.Item.value('@Price[1]',      'decimal(10,2)'),
           Qty        = B.Item.value('@Qty[1]',        'int')
    FROM   @x.nodes('/Orders/Order') AS A(Item)
    CROSS  APPLY A.Item.nodes('OrderDetails') AS B (Item)
    Erland Sommarskog, SQL Server MVP, [email protected]

  • OPEN CURSOR using a WITH clause in the select query

    Hi,
    I am using Oracle 9i. I have a requirement where I have a REFCURSOR as an OUT parameter for my procedure. I have declared the TYPE and created the procedure.
    In the procedure, I am using OPEN <cursor_name> FOR <query>;
    Ideally this works in most of the cases that I have tried earlier. However, in the current case I am using a WITH clause in my query to get the results.
    I need help in understanding if the above mentioned syntax would not allow me to use the WITH clause in the query.

    What error do you get , seems to work ok for me on 10g
    SQL> begin
      2  open :cv for 'with x as (select * from emp)  select * from x';
      3  end;
      4  /
    PL/SQL procedure successfully completed.
    SQL> print :cv
         EMPNO
    ENAME
    JOB              MGR HIREDATE         SAL       COMM     DEPTNO
          7521
    WARD
    SALESMAN        7698 22-FEB-81       1250        500         30
          7566
    JONES
    MANAGER         7839 02-APR-81       2975                    20
         EMPNO

  • 'Missing select' error for update statement using WITH clause

    Hi,
    I am getting the below error for update statement using WITH clause
    SQL Error: ORA-00928: missing SELECT keyword
      UPDATE A
      set A.col1 = 'val1'
         where
      A.col2 IN (
      WITH D AS
      SELECT col2 FROM
      (SELECT col2, MIN(datecol) col3 FROM DS
      WHERE <conditions>
        GROUP BY PATIENT) D2
      WHERE
      <conditions on A.col4 and D2.col3>

    Hi,
    The format of a query using WITH is:
    WITH  d  AS
        SELECT  ...  -- sub_query
    SELECT  ...   -- main query
    You don't have a main query.  The keyword FROM has to come immediately after the right ')' that ends the last WITH clause sub-query.
    That explains the problem based on what you posted.  I can't tell if the real problem is in the conditions that you didn't post.
    I hope this answers your question.
    If not, post a complete test script that people can run to re-create the problem and test their ideas.  Include a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all the tables involved, and the results you want from that data.
    In the case of a DML operation (such as UPDATE) the sample data should show what the tables are like before the DML, and the results will be the contents of the changed table(s) after the DML.
    Explain, using specific examples, how you get those results from that data.
    Always say what version of Oracle you're using (e.g. 11.2.0.2.0).
    See the forum FAQ: https://forums.oracle.com/message/9362002

  • Issue in use of "WITH CLAUSE"

    My DB version is 10.2.0
    One of my query takes long time for execution and here is the Old Query,
    SELECT HD.DATUM DATUM, HA2.GELDEINGAENGE_INSG-NVL(HA3.VERWERTUNGSERLOESE,0)
      GELDEINGANG, HA3.VERWERTUNGSERLOESE
    FROM
    ( SELECT DISTINCT(TO_CHAR(ERFASSDATUM,'YYYY/MM')) DATUM
      FROM TRANS_HIST H1 ,
        (SELECT BUCHUNGSGRUPPE, LFDNR, SICHERHEITBEZUG
        FROM BUCHUNGSSCHL
        WHERE STORNOMM=0) B1
      WHERE H1.ERFASSDATUM >=TO_DATE('01.10'||TO_CHAR(ADD_MONTHS(SYSDATE,-9),'YYYY')||' 00.00.00','DD.MM.YYYY HH24:MI:SS')
      AND H1.BUCHUNGSGRUPPE = B1.BUCHUNGSGRUPPE AND H1.LFDNR = B1.LFDNR
      AND (H1.BUCHUNGSGRUPPE = '8888' OR ( H1.BUCHUNGSGRUPPE > 5999 AND H1.BUCHUNGSGRUPPE < 7100 AND B1.SICHERHEITBEZUG = 1)) ) HD,
      (SELECT TO_CHAR(ERFASSDATUM,'YYYY/MM') DATUM, SUM(BETRAG) GELDEINGAENGE_INSG
      FROM TRANS_HIST H2,
      (SELECT F.GLAEUBIGERNR,F.FORDNR,F.FORDERGNR,A.MAHN_NUM
      FROM FRD_ACCT F,
      (SELECT * FROM ANS_PRCH WHERE RANGMM=1) A
      WHERE F.GLAEUBIGERNR=A.GLAEUBIGERNR (+) AND F.FORDNR=A.FORDNR(+)
      AND F.FORDERGNR=A.FORDERGNR(+) AND NVL(F.INDIVIDUALFLAG,0)=0 ) F1
      WHERE ( (F1.GLAEUBIGERNR= H2.GLAEUBIGERNR AND F1.FORDNR=H2.FORDNR AND F1.FORDERGNR=H2.FORDERGNR) OR
      F1.MAHN_NUM = H2.MAHN_NUM) AND H2.BUCHUNGSGRUPPE = '8888' AND
      H2.ERFASSDATUM >= TO_DATE('01.10'||TO_CHAR(ADD_MONTHS(SYSDATE,-9),'YYYY')||' 00.00.00','DD.MM.YYYY HH24:MI:SS')
      GROUP BY TO_CHAR(ERFASSDATUM,'YYYY/MM') ) HA2,
      (SELECT TO_CHAR(ERFASSDATUM,'YYYY/MM') DATUM, SUM(BETRAG) VERWERTUNGSERLOESE
       FROM TRANS_HIST H3, (SELECT BUCHUNGSGRUPPE, LFDNR,
      SICHERHEITBEZUG, NACHMIETVERTRAGE
      FROM BUCHUNGSSCHL
      WHERE STORNOMM=0) B3,
      (SELECT F.GLAEUBIGERNR,F.FORDNR,F.FORDERGNR,A.MAHN_NUM
      FROM FRD_ACCT F,
      (SELECT * FROM ANS_PRCH WHERE RANGMM=1) A
      WHERE F.GLAEUBIGERNR= A.GLAEUBIGERNR (+) AND F.FORDNR=A.FORDNR(+)
      AND F.FORDERGNR=A.FORDERGNR(+) AND NVL(F.INDIVIDUALFLAG,0)=0 ) F2
      WHERE ( (F2.GLAEUBIGERNR=H3.GLAEUBIGERNR AND F2.FORDNR=H3.FORDNR AND F2.FORDERGNR=H3.FORDERGNR)
      OR F2.MAHN_NUM = H3.MAHN_NUM)
      AND H3.ERFASSDATUM >=TO_DATE('01.10'||TO_CHAR(ADD_MONTHS(SYSDATE,-9),'YYYY')||' 00.00.00','DD.MM.YYYY HH24:MI:SS')
      AND H3.BUCHUNGSGRUPPE = B3.BUCHUNGSGRUPPE AND
      H3.LFDNR = B3.LFDNR AND H3.BUCHUNGSGRUPPE > 5999
      AND H3.BUCHUNGSGRUPPE < 7100 AND (B3.SICHERHEITBEZUG = 1 OR B3.NACHMIETVERTRAGE =1)
      GROUP BY TO_CHAR(ERFASSDATUM,'YYYY/MM') ) HA3
      WHERE HD.DATUM=HA2.DATUM (+)
      AND HD.DATUM=HA3.DATUM (+)
      ORDER BY DATUM ASC
    call     count       cpu    elapsed       disk      query    current        rows
    Parse        1      0.22       0.22          0          0          0           0
    Execute      1      0.00       0.00          0          0          0           0
    Fetch        1    469.61    1448.30    2874498    3017355          1           9
    total        3    469.83    1448.53    2874498    3017355          1           9
    Misses in library cache during parse: 1
    Optimizer mode: ALL_ROWS
    Parsing user id: 62     (recursive depth: 1)
    Rows     Row Source Operation
          9  SORT ORDER BY (cr=3017355 pr=2874498 pw=0 time=1448309418 us)
          9   HASH JOIN RIGHT OUTER (cr=3017355 pr=2874498 pw=0 time=1448309191 us)
          9    VIEW  (cr=1228085 pr=1175604 pw=0 time=906871801 us)
          9     HASH GROUP BY (cr=1228085 pr=1175604 pw=0 time=906871785 us)
       1559      CONCATENATION  (cr=1228085 pr=1175604 pw=0 time=564453620 us)
        233       HASH JOIN  (cr=614043 pr=589377 pw=0 time=562088377 us)
         94        TABLE ACCESS FULL BUCHUNGSSCHL (cr=29 pr=0 pw=0 time=505 us)
    254136        HASH JOIN  (cr=614014 pr=589377 pw=0 time=509476999 us)
    497464         TABLE ACCESS FULL TRANS_HIST (cr=586339 pr=562603 pw=0 time=65783878 us)
    737515         HASH JOIN RIGHT OUTER (cr=27675 pr=26774 pw=0 time=18577731 us)
    372656          TABLE ACCESS FULL ANS_PRCH (cr=6346 pr=5910 pw=0 time=408778 us)
    737515          TABLE ACCESS FULL FRD_ACCT (cr=21329 pr=20864 pw=0 time=2254657 us)
       1326       HASH JOIN  (cr=614042 pr=586227 pw=0 time=520726941 us)
         94        TABLE ACCESS FULL BUCHUNGSSCHL (cr=29 pr=0 pw=0 time=641 us)
    221360        FILTER  (cr=614013 pr=586227 pw=0 time=372791872 us)
    221360         HASH JOIN OUTER (cr=614013 pr=586227 pw=0 time=372570499 us)
    221360          HASH JOIN  (cr=607668 pr=580286 pw=0 time=368077766 us)
    243053           TABLE ACCESS FULL TRANS_HIST (cr=586339 pr=563978 pw=0 time=1425434011 us)
    737515           TABLE ACCESS FULL FRD_ACCT (cr=21329 pr=16308 pw=0 time=8859226 us)
    372656          TABLE ACCESS FULL ANS_PRCH (cr=6345 pr=5941 pw=0 time=1872464 us)
          9    HASH JOIN OUTER (cr=1789270 pr=1698894 pw=0 time=541436637 us)
          9     VIEW  (cr=586667 pr=562439 pw=0 time=213337882 us)
          9      HASH UNIQUE (cr=586667 pr=562439 pw=0 time=213337873 us)
    748717       HASH JOIN  (cr=586667 pr=562439 pw=0 time=104432018 us)
        830        TABLE ACCESS FULL BUCHUNGSSCHL (cr=29 pr=0 pw=0 time=1042 us)
    1241936        TABLE ACCESS FULL TRANS_HIST (cr=586638 pr=562439 pw=0 time=339666777 us)
          9     VIEW  (cr=1202603 pr=1136455 pw=0 time=328097826 us)
          9      HASH GROUP BY (cr=1202603 pr=1136455 pw=0 time=328097809 us)
    695373       CONCATENATION  (cr=1202603 pr=1136455 pw=0 time=324453471 us)
          0        HASH JOIN  (cr=587003 pr=557994 pw=0 time=167168982 us)
    744472         TABLE ACCESS FULL TRANS_HIST (cr=587003 pr=557994 pw=0 time=30622271 us)
          0         HASH JOIN RIGHT OUTER (cr=0 pr=0 pw=0 time=0 us)
          0          TABLE ACCESS FULL ANS_PRCH (cr=0 pr=0 pw=0 time=0 us)
          0          TABLE ACCESS FULL FRD_ACCT (cr=0 pr=0 pw=0 time=0 us)
    695373        FILTER  (cr=615600 pr=578461 pw=0 time=157284464 us)
    695373         HASH JOIN OUTER (cr=615600 pr=578461 pw=0 time=156589072 us)
    695373          HASH JOIN  (cr=609255 pr=572518 pw=0 time=150571393 us)
    744472           TABLE ACCESS FULL TRANS_HIST (cr=587926 pr=556115 pw=0 time=31820718 us)
    737515           TABLE ACCESS FULL FRD_ACCT (cr=21329 pr=16403 pw=0 time=3696334 us)
    372656          TABLE ACCESS FULL ANS_PRCH (cr=6345 pr=5943 pw=0 time=391928 us)I tried fine tuning the query using "With Clause" but landed up with oracle error, which prevents me from using with clause inside a paranthesis.
    SELECT HD.DATUM DATUM, HA2.GELDEINGAENGE_INSG-NVL(HA3.VERWERTUNGSERLOESE,0)
      GELDEINGANG, HA3.VERWERTUNGSERLOESE
    FROM
    ( WITH HIST_VIEW AS (SELECT GLAEUBIGERNR,FORDNR,FORDERGNR,MAHN_NUM,BUCHUNGSGRUPPE,LFDNR,STORNOMM,ERFASSDATUM,BETRAG
                         FROM TRANS_HIST H
                         WHERE ERFASSDATUM >=TO_DATE('01.10'||TO_CHAR(ADD_MONTHS(SYSDATE,-9),'YYYY')||' 00.00.00','DD.MM.YYYY HH24:MI:SS')
                         AND  (H.BUCHUNGSGRUPPE = '8888' OR ( H.BUCHUNGSGRUPPE > 5999 AND H.BUCHUNGSGRUPPE < 7100))),
           FORD_VIEW AS (SELECT F.GLAEUBIGERNR,F.FORDNR,F.FORDERGNR,A.MAHN_NUM
                         FROM FRD_ACCT F,ANS_PRCH A
                         WHERE F.GLAEUBIGERNR=A.GLAEUBIGERNR(+) AND F.FORDNR=A.FORDNR(+)
                         AND F.FORDERGNR=A.FORDERGNR(+) AND NVL(F.INDIVIDUALFLAG,0)=0 AND  A.RANGMM=1)
    (SELECT DISTINCT(TO_CHAR(ERFASSDATUM,'YYYY/MM')) DATUM
      FROM HIST_VIEW H1 ,BUCHUNGSSCHL B1
      WHERE B1.STORNOMM=0  AND H1.BUCHUNGSGRUPPE = B1.BUCHUNGSGRUPPE AND H1.LFDNR = B1.LFDNR
      AND (H1.BUCHUNGSGRUPPE = '8888' OR (H1.BUCHUNGSGRUPPE > 5999 AND H1.BUCHUNGSGRUPPE < 7100 AND B1.SICHERHEITBEZUG = 1))) HD,
    (SELECT TO_CHAR(ERFASSDATUM,'YYYY/MM') DATUM, SUM(BETRAG) GELDEINGAENGE_INSG
      FROM HIST_VIEW H2,FORD_VIEW F1
      WHERE ((F1.GLAEUBIGERNR= H2.GLAEUBIGERNR AND F1.FORDNR=H2.FORDNR AND F1.FORDERGNR=H2.FORDERGNR) OR
      F1.MAHN_NUM = H2.MAHN_NUM) AND H2.BUCHUNGSGRUPPE = '8888'
      GROUP BY TO_CHAR(ERFASSDATUM,'YYYY/MM') ) HA2,
    (SELECT TO_CHAR(ERFASSDATUM,'YYYY/MM') DATUM, SUM(BETRAG) VERWERTUNGSERLOESE
      FROM HIST_VIEW H3, BUCHUNGSSCHL B3,FORD_VIEW F2
      WHERE ((F2.GLAEUBIGERNR=H3.GLAEUBIGERNR AND F2.FORDNR=H3.FORDNR
      AND F2.FORDERGNR=H3.FORDERGNR) OR F2.MAHN_NUM = H3.MAHN_NUM) AND B3.STORNOMM=0
      AND H3.BUCHUNGSGRUPPE = B3.BUCHUNGSGRUPPE AND
      H3.LFDNR = B3.LFDNR AND H3.BUCHUNGSGRUPPE > 5999
      AND H3.BUCHUNGSGRUPPE < 7100 AND (B3.SICHERHEITBEZUG = 1 OR B3.NACHMIETVERTRAGE =1)
      GROUP BY TO_CHAR(ERFASSDATUM,'YYYY/MM')) HA3
      WHERE HD.DATUM=HA2.DATUM (+)
      AND HD.DATUM=HA3.DATUM (+)
      ORDER BY DATUM ASC )
    ORA-00907: missing right parenthesis

    If you format your code it makes it far easier to spot mistakes...
    WITH HIST_VIEW AS (SELECT GLAEUBIGERNR,FORDNR,FORDERGNR,MAHN_NUM,BUCHUNGSGRUPPE,LFDNR,STORNOMM,ERFASSDATUM,BETRAG
                       FROM   TRANS_HIST H
                       WHERE  ERFASSDATUM >=TO_DATE('01.10'||TO_CHAR(ADD_MONTHS(SYSDATE,-9),'YYYY')||' 00.00.00','DD.MM.YYYY HH24:MI:SS')
                       AND   (  H.BUCHUNGSGRUPPE = '8888'
                            OR (H.BUCHUNGSGRUPPE > 5999 AND H.BUCHUNGSGRUPPE < 7100)
         FORD_VIEW AS (SELECT F.GLAEUBIGERNR,F.FORDNR,F.FORDERGNR,A.MAHN_NUM
                       FROM   FRD_ACCT F,ANS_PRCH A
                       WHERE  F.GLAEUBIGERNR=A.GLAEUBIGERNR(+)
                       AND    F.FORDNR=A.FORDNR(+)
                       AND    F.FORDERGNR=A.FORDERGNR(+)
                       AND    NVL(F.INDIVIDUALFLAG,0)=0
                       AND    A.RANGMM=1
    SELECT HD.DATUM DATUM
          ,HA2.GELDEINGAENGE_INSG-NVL(HA3.VERWERTUNGSERLOESE,0) GELDEINGANG
          ,HA3.VERWERTUNGSERLOESE
    FROM  (SELECT DISTINCT(TO_CHAR(ERFASSDATUM,'YYYY/MM')) DATUM
           FROM   HIST_VIEW H1
                 ,BUCHUNGSSCHL B1
           WHERE  B1.STORNOMM=0
           AND    H1.BUCHUNGSGRUPPE = B1.BUCHUNGSGRUPPE
           AND    H1.LFDNR = B1.LFDNR
           AND   (  H1.BUCHUNGSGRUPPE = '8888'
                OR (H1.BUCHUNGSGRUPPE > 5999 AND H1.BUCHUNGSGRUPPE < 7100 AND B1.SICHERHEITBEZUG = 1)
          ) HD,
          (SELECT TO_CHAR(ERFASSDATUM,'YYYY/MM') DATUM
                 ,SUM(BETRAG) GELDEINGAENGE_INSG
           FROM   HIST_VIEW H2
                 ,FORD_VIEW F1
           WHERE (
                   (    F1.GLAEUBIGERNR= H2.GLAEUBIGERNR
                    AND F1.FORDNR=H2.FORDNR
                    AND F1.FORDERGNR=H2.FORDERGNR
                 OR
                    F1.MAHN_NUM = H2.MAHN_NUM
           AND   H2.BUCHUNGSGRUPPE = '8888'
           GROUP BY TO_CHAR(ERFASSDATUM,'YYYY/MM')
          ) HA2,
          (SELECT TO_CHAR(ERFASSDATUM,'YYYY/MM') DATUM
                 ,SUM(BETRAG) VERWERTUNGSERLOESE
           FROM   HIST_VIEW H3
                 ,BUCHUNGSSCHL B3
                 ,FORD_VIEW F2
           WHERE (
                   (    F2.GLAEUBIGERNR=H3.GLAEUBIGERNR
                    AND F2.FORDNR=H3.FORDNR
                    AND F2.FORDERGNR=H3.FORDERGNR
                 OR
                   F2.MAHN_NUM = H3.MAHN_NUM
           AND B3.STORNOMM=0
           AND H3.BUCHUNGSGRUPPE = B3.BUCHUNGSGRUPPE
           AND H3.LFDNR = B3.LFDNR
           AND H3.BUCHUNGSGRUPPE > 5999
           AND H3.BUCHUNGSGRUPPE < 7100
           AND (B3.SICHERHEITBEZUG = 1 OR B3.NACHMIETVERTRAGE =1)
           GROUP BY TO_CHAR(ERFASSDATUM,'YYYY/MM')
          ) HA3
    WHERE HD.DATUM=HA2.DATUM (+)
    AND   HD.DATUM=HA3.DATUM (+)
    ORDER BY DATUM ASC
    ) <--- What's this doing on the end?Remove that last bracket and you should be ok.
    I took the liberty of putting the subquery factoring at the beginning, so see if that works.

  • Need help with INSERT and WITH clause

    I wrote sql statement which correctly work, but how i use this statment with INSERT query? NEED HELP. when i wrote insert i see error "ORA 32034: unsupported use of with clause"
    with t1 as(
    select a.budat,a.monat as period,b.vtweg,
    c.gjahr,c.buzei,c.shkzg,c.hkont, c.prctr,
    c.wrbtr,
    c.matnr,
    c.menge,
    a.monat,
    c.zuonr
    from ldw_v1.BKPF a,ldw_v1.vbrk b, ldw_v1.bseg c
    where a.AWTYP='VBRK' and a.BLART='RV' and a.BUKRS='8431' and a.awkey=b.vbeln
    and a.bukrs=c.bukrs and a.belnr=c.belnr and a.gjahr=c.gjahr and c.koart='D'
    and c.ktosl is null and c.gsber='4466' and a.gjahr>='2011' and b.vtweg='01'
    ,t2 as(
    select a.BUKRS,a.BELNR, a.GJAHR,t1.vtweg,t1.budat,t1.monat from t1, ldw_v1.bkpf a
    where t1.zuonr=a.xblnr and a.blart='WL' and bukrs='8431'
    ,tcogs as (
    select t2.budat,t2.monat,t2.vtweg, bseg.gjahr,bseg.hkont,bseg.prctr,
    sum(bseg.wrbtr) as COGS,bseg.matnr,bseg.kunnr,sum(bseg.menge) as QUANTITY
    from t2, ldw_v1.bseg
    where t2.bukrs=bseg.bukrs and t2.belnr=bseg.BELNR and t2.gjahr=bseg.gjahr and BSEG.KOART='S'
    group by t2.budat,t2.monat,t2.vtweg, bseg.gjahr,bseg.hkont,bseg.prctr,
    bseg.matnr,bseg.kunnr
    ,t3 as
    select a.budat,a.monat,b.vtweg,
    c.gjahr,c.buzei,c.shkzg,c.hkont, c.prctr,
    case when c.shkzg='S' then c.wrbtr*(-1)
    else c.wrbtr end as NTS,
    c.matnr,c.kunnr,
    c.menge*(-1) as Quantity
    from ldw_v1.BKPF a,ldw_v1.vbrk b, ldw_v1.bseg c
    where a.AWTYP='VBRK' and a.BLART='RV' and a.BUKRS='8431' and a.awkey=b.vbeln
    and a.bukrs=c.bukrs and a.belnr=c.belnr and a.gjahr=c.gjahr and c.koart='S'
    and c.ktosl is null and c.gsber='4466' and a.gjahr>='2011' and b.vtweg='01'
    ,trevenue as (
    select t3.budat,t3.monat,t3.vtweg, t3.gjahr,t3.hkont,t3.prctr,
    sum(t3.NTS) as NTS,t3.matnr,t3.kunnr,sum(t3.QUANTITY) as QUANTITY
    from t3
    group by t3.budat,t3.monat,t3.vtweg, t3.gjahr,t3.hkont,t3.prctr,t3.matnr,t3.kunnr
    select NVL(tr.budat,tc.budat) as budat,
    NVL(tr.monat,tc.monat) as monat,
    NVL(tr.vtweg,tc.vtweg) as vtweg,
    NVL(tr.gjahr, tc.gjahr) as gjahr,
    tr.hkont as NTS_hkont,
    tc.hkont as COGS_hkont,
    NVL(tr.prctr,tc.prctr) as prctr,
    NVL(tr.MATNR, tc.MATNR) as matnr,
    NVL(tr.kunnr, tc.kunnr) as kunnr,
    NVL(tr.Quantity, tc.Quantity) as Quantity,
    tr.NTS as NTS,
    tc.COGS as COGS
    from trevenue TR full outer join tcogs TC
    on TR.BUDAT=TC.BUDAT and TR.MONAT=TC.MONAT and TR.GJAHR=TC.GJAHR
    and TR.MATNR=TC.MATNR and TR.KUNNR=TC.KUNNR and TR.QUANTITY=TC.QUANTITY
    and TR.VTWEG=TC.VTWEG and TR.PRCTR=TC.PRCTR
    Edited by: user13566113 on 25.03.2011 5:26

    Without seeing what you tried it is hard to say what you did wrong, but this is how it would work
    SQL> create table t ( n number );
    Table created.
    SQL> insert into t
      2  with test_data as
      3    (select 1 x from dual union all
      4     select 2 x from dual union all
      5     select 3 x from dual union all
      6     select 4 x from dual)
      7  select x from test_data;
    4 rows created.
    SQL>

  • Report with multiplying columns  and WITH clause

    Hello
    I saw sth strange in my report. I assume that it could happens very often.
    I have report with few columns which two of them ar most complicated (many joins subqueries aggreagations on joined values etc.) These two columns (i.e C3,C4) should be multiplied (C3*C4).
    When i do pure report without multiplying only columns C1,C2,C3,C4 everything is ok - duration about 15 sec. but... when I put next column on report which multiply these columns (in Answers C5=C3*C4)
    I wait 3-4 minutes and my database hungs :(. After investigation I saw that in first case to databese goes pure "SELECT" statement it means:
    "Select ... as C1, ... as C2, max(...) as C3, sum(xxx)... C4 from yyy,sss,ttt WHERE aaa"
    but in second case BI uses WITH clause it means:
    WITH SAWITH0 AS
    ( Select ... as C1, ... as C2, max(...) as C3, sum(xxx)... C4 from yyy,sss,ttt WHERE aaa )
    SELECT SAWITH0.C1 as C1,
    SAWITH0.C2 as C2,
         SAWITH0.C3 as C3,
         SAWITH0.C4 as C4,
         SAWITH0.C3*SAWITH0.C4 as C5 FROM SSS
    and this statement is long runninq query and kills my database :(.
    I checked that SQL like this:
    Select ... as C1, ... as C2, max(...) as C3, sum(xxx)... C4, max(...)*sum(xxx)... As C5 from yyy,sss,ttt WHERE aaa" -
    runs few times faster than that above
    I know that I can do this multiply in business model layer but sometimes users can multiply(or other operations) on columns in reports without my knowledge and it kills my db :(. Where is bug? Why SQLs with WITH clause takes so much db time?
    Thank you for each kind of help

    WITH clause or Subquery Factoring allows the set of data to be reused multiple times within the SQL. Oracle will usually materialize the data into a temporary table (you will see it if you take an explain plan of the SQL).
    I would be surprised if it was the actual WITH clause that was causing the performance issue, however you can test this by turning the WITH clause feature off. Go to the Physical model, right mouse click on your Database > Properties > Features Tab, scroll down to WITH_CLAUSE_SUPPORTED and switch it off.
    I'd be interested to know if you do see actual improvement.
    Good Luck.

  • Error in writing WITH CLAUSE

    i am writing these SQL Lines in my stored procedure i am using with clause i am getting error when i perform
    inner join
    With  WardItemsDtl as ( SELECT WardItemDtl_ID,
    WardTransID,
    wdDtl.ItemID,
    SUM(ItemQty) ItemQty,
    ReceivedWardID as WardID
    FROM LL_WardItems_Details wdDtl
    WHERE isposted = 0
    GROUP BY wdDtl.ItemID,WardItemDtl_ID,ReceivedWardID,WardTransID
    ORDER BY WardItemDtl_ID )
    , PartyItemsDtl AS ( SELECT WardItemDtl_ID,
    ItemID,
    SUM(IssueItemQty) IssueItemQty
    FROM LL_PartyItems_Details
    GROUP BY WardItemDtl_ID,ItemID
    ORDER BY WardItemDtl_ID )
    SELECT wdDt.WardTransID,
    wdDt.ItemID,
    itmt.ItemCode,
    itmt.ItemDescription,
    wdDt.ItemQty as TotalItemQty,
    NVL(pDtl.IssueItemQty, 0) as IssueQtyToParty,
    wdDt.WardID,
    wdmt.Ward_Name
    from WardItemsDtl wdDt,PartyItemsDtl pDtl
    inner join LLItemMst itmt
    on itmt.ItemID=wdDt.ItemID -----error in this line
    inner join ward_mst wdmt -- error in this line
    on wdmt.Ward_ID=wdDt.WardID
    error
    Error report:
    SQL Error: ORA-00904: "WDDT"."ITEMID": invalid identifier
    00904. 00000 - "%s: invalid identifier"
    *Cause:   
    *Action:                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

    WITH WARDITEMSDTL AS
      (SELECT WARDITEMDTL_ID,
        WARDTRANSID,
        WDDTL.ITEMID,
        SUM(ITEMQTY) ITEMQTY,
        RECEIVEDWARDID AS WARDID
      FROM LL_WARDITEMS_DETAILS WDDTL
      WHERE ISPOSTED = 0
      GROUP BY WDDTL.ITEMID,
        WARDITEMDTL_ID,
        RECEIVEDWARDID,
        WARDTRANSID
      ORDER BY WARDITEMDTL_ID
      PARTYITEMSDTL AS
      (SELECT WARDITEMDTL_ID,
        ITEMID,
        SUM(ISSUEITEMQTY) ISSUEITEMQTY
      FROM LL_PARTYITEMS_DETAILS
      GROUP BY WARDITEMDTL_ID,
        ITEMID
      ORDER BY WARDITEMDTL_ID
    SELECT WDDT.WARDTRANSID,
      WDDT.ITEMID,
      ITMT.ITEMCODE,
      ITMT.ITEMDESCRIPTION,
      WDDT.ITEMQTY              AS TOTALITEMQTY,
      NVL(PDTL.ISSUEITEMQTY, 0) AS ISSUEQTYTOPARTY,
      WDDT.WARDID,
      WDMT.WARD_NAME
    FROM WARDITEMSDTL WDDT,
      PARTYITEMSDTL PDTL,
    LLITEMMST ITMT,
    WARD_MST WDMT     
    WHERE ITMT.ITEMID=WDDT.ITEMID
    AND  WDMT.WARD_ID=WDDT.WARDID ;AND WHERE YOUR JOIN PARTYITEMSDTL PDTL

  • Unable to use the with clause in oracle 9.0.2

    Hi,
    I need to use oracle SQL with clause in oracle 9.0.2 database. Its a 9i feature but i am unable to use it.
    It is giving internal error, when i try to execute it.
    Even for simple query:
    WITH acct_summary as ( select TOT_COLL_AMT from tdc_acct)
    select TOT_COLL_AMT from acct_summary WHERE TOT_COLL_AMT>100;
    Error message while using 8.0.5 sql plus client:
    SP2-0642: SQL*Plus internal error state 2091, context 0:0:0
    Unsafe to proceed
    Please help to find out why i am not able to use the sql with clause in oracle 9.0.2 database.
    Thanks and regards,
    Raajkathir

    Hi Jens Petersen,
    Yes, You are correct. Thank you very much.
    Regards,
    Raja

  • ORA-32034:unsupported use of WITH clause, anywork arround ? please help

    Hi,
    Can some one please tell me how to write an union statement with two "WITH STATEMENT" . I am trying to create a materialized view ( in testing ) , and I get the error
    CREATE MATERIALIZED VIEW MV_TEST AS
    WITH
    PER AS (SELECT P.PERSONNEL_ID, P.FIRST_NAME EMP_FIRST_NAME, P.SURNAME_ISI EMP_LAST_NAME, P.EXEMPT, P.FTE_ASSIGNED,
            NVL(P.EMPLOYEE_ID,P.VISITOR_NUMBER) EMPLOYEE_VISITOR_ID, ST.CODE_VALUE STAFF_CATEGORY, DIV.DIVISION_NUMBER EMP_DIVISION
            FROM PERSONNEL P , CODES ST , DIVISIONS DIV 
            WHERE P.STAFF_CATEGORY_CODE_ID  = ST.CODE_ID (+) AND P.DIVISION_ID = DIV.DIVISION_ID (+)),
    ACT AS (SELECT AC.ACCOUNT_ID, AC.ACCOUNT_NUMBER, AC.ACCOUNT_NAME, AC.ACCOUNT_START_DATE, AC.ACCOUNT_END_DATE, ACS.ACCOUNT_STATUS_DESIG ACCOUNT_STATUS,
            MGR.FIRST_NAME MGR_FIRST_NAME, MGR.SURNAME_ISI MGR_LAST_NAME,  MGR_DIV.DIVISION_NUMBER MGR_DIVISION
            FROM ACCOUNTS AC, ACCOUNT_STATUS ACS, PERSONNEL MGR, DIVISIONS MGR_DIV
            WHERE AC.ACCOUNT_STATUS_ID = ACS.ACCOUNT_STATUS_ID AND AC.ACCOUNT_MANAGER = MGR.PERSONNEL_ID AND MGR.DIVISION_ID = MGR_DIV.DIVISION_ID (+)),
    LCS AS (SELECT LC.PERSONNEL_ID, LC.ACCOUNT_ID , SUM(NVL(LC.LCS_EFFORT,0)) LCS_EFFORT , AP.PERIOD_YEAR
            FROM
            (SELECT PERSONNEL_ID, ACCOUNT_ID , FISCAL_YEAR_ID , FISCAL_PERIOD_ID , LCS_EFFORT
            FROM LABOR_COMMITED WHERE COMMITED_DATE IS NOT NULL ) LC
            PARTITION BY ( PERSONNEL_ID, ACCOUNT_ID)
            RIGHT OUTER JOIN
            (SELECT FIRST_DAY PERIOD_YEAR, FISCAL_YEAR_ID, FISCAL_SYSTEM_PERIOD_ID FROM ALL_PERIODS WHERE CALENDRICAL_EQUIVALENT IS NOT NULL) AP
            ON (LC.FISCAL_YEAR_ID = AP.FISCAL_YEAR_ID  AND LC.FISCAL_PERIOD_ID = AP.FISCAL_SYSTEM_PERIOD_ID)
            GROUP BY LC.PERSONNEL_ID, LC.ACCOUNT_ID , AP.PERIOD_YEAR)
    SELECT  PER.PERSONNEL_ID , PER.EMP_FIRST_NAME, PER.EMP_LAST_NAME, PER.EXEMPT, PER.FTE_ASSIGNED, PER.EMPLOYEE_VISITOR_ID, PER.STAFF_CATEGORY , PER.EMP_DIVISION,
            ACT.ACCOUNT_NUMBER, ACT.ACCOUNT_NAME, ACT.ACCOUNT_START_DATE, ACT.ACCOUNT_END_DATE, ACT.ACCOUNT_STATUS, ACT.MGR_FIRST_NAME, ACT.MGR_LAST_NAME,
            ACT.MGR_DIVISION, TO_NUMBER(TO_CHAR(LCS.PERIOD_YEAR,'YYYY')) YEARS, LCS.PERIOD_YEAR, LCS.LCS_EFFORT
    FROM    PER, ACT, LCS
    WHERE   PER.PERSONNEL_ID = LCS.PERSONNEL_ID
    AND     ACT.ACCOUNT_ID = LCS.ACCOUNT_ID
    UNION ALL
    WITH
    PER AS (SELECT P.PERSONNEL_ID, P.FIRST_NAME EMP_FIRST_NAME, P.SURNAME_ISI EMP_LAST_NAME, P.EXEMPT, P.FTE_ASSIGNED,
            NVL(P.EMPLOYEE_ID,P.VISITOR_NUMBER) EMPLOYEE_VISITOR_ID, ST.CODE_VALUE STAFF_CATEGORY, DIV.DIVISION_NUMBER EMP_DIVISION
            FROM PERSONNEL P , CODES ST , DIVISIONS DIV 
            WHERE P.STAFF_CATEGORY_CODE_ID  = ST.CODE_ID (+) AND P.DIVISION_ID = DIV.DIVISION_ID (+)),
    ACT AS (SELECT AC.ACCOUNT_ID, AC.ACCOUNT_NUMBER, AC.ACCOUNT_NAME, AC.ACCOUNT_START_DATE, AC.ACCOUNT_END_DATE, ACS.ACCOUNT_STATUS_DESIG ACCOUNT_STATUS,
            MGR.FIRST_NAME MGR_FIRST_NAME, MGR.SURNAME_ISI MGR_LAST_NAME,  MGR_DIV.DIVISION_NUMBER MGR_DIVISION
            FROM ACCOUNTS AC, ACCOUNT_STATUS ACS, PERSONNEL MGR, DIVISIONS MGR_DIV
            WHERE AC.ACCOUNT_STATUS_ID = ACS.ACCOUNT_STATUS_ID AND AC.ACCOUNT_MANAGER = MGR.PERSONNEL_ID AND MGR.DIVISION_ID = MGR_DIV.DIVISION_ID (+)),
    LCE AS (SELECT LE.PERSONNEL_ID, LE.ACCOUNT_ID , SUM(NVL(LE.LCS_EFFORT,0)) LCS_EFFORT , AP.PERIOD_YEAR
            FROM
            (SELECT PERSONNEL_ID, ACCOUNT_ID , FISCAL_YEAR_ID , FISCAL_PERIOD_ID , LCS_EFFORT
            FROM LABOR_EFFORT WHERE LABOR_EFFORT_ID NOT IN (SELECT LABOR_EFFORT_ID FROM LABOR_EFFORT_RETRO_ELEMENTS)) LE
            PARTITION BY ( PERSONNEL_ID, ACCOUNT_ID)
            RIGHT OUTER JOIN
            (SELECT FIRST_DAY PERIOD_YEAR, FISCAL_YEAR_ID, FISCAL_SYSTEM_PERIOD_ID FROM ALL_PERIODS WHERE CALENDRICAL_EQUIVALENT IS NOT NULL) AP
            ON (LE.FISCAL_YEAR_ID = AP.FISCAL_YEAR_ID  AND LE.FISCAL_PERIOD_ID = AP.FISCAL_SYSTEM_PERIOD_ID)
            GROUP BY LE.PERSONNEL_ID, LE.ACCOUNT_ID , AP.PERIOD_YEAR)
    SELECT  PER.EMP_FIRST_NAME, PER.EMP_LAST_NAME, PER.EXEMPT, PER.FTE_ASSIGNED, PER.EMPLOYEE_VISITOR_ID, PER.STAFF_CATEGORY , PER.EMP_DIVISION,
            ACT.ACCOUNT_NUMBER, ACT.ACCOUNT_NAME, ACT.ACCOUNT_START_DATE, ACT.ACCOUNT_END_DATE, ACT.ACCOUNT_STATUS, ACT.MGR_FIRST_NAME, ACT.MGR_LAST_NAME,
            ACT.MGR_DIVISION, TO_NUMBER(TO_CHAR(LCE.PERIOD_YEAR,'YYYY')) YEARS, LCE.PERIOD_YEAR, LCE.LCS_EFFORT
    FROM    PER, ACT, LCE
    WHERE   PER.PERSONNEL_ID = LCE.PERSONNEL_ID
    AND     ACT.ACCOUNT_ID = LCE.ACCOUNT_ID

    Have you tried with SELECT outside the WITH clause ?
    e.g.
    SELECT * FROM (WITH stmt1 goes here..)
    UNION ALL
    SELECT * FROM (WITH stmt2 goes here...).

  • ORA-32034: unsupported use of WITH clause-issue

    hello all,
    i am facing some issue when i use with clause and union all operator.
    i have created a dummy code to solve this problem..
    my code is ----------
    with dept_1 as
    (select deptno d1 from detp9 where deptno = 20)
    select empno from emp9 e,dept_d1 where e.empno = dept_1.d1
    UNION ALL
    with dept_1 as
    (select deptno d2 from detp9 where deptno = 30)
    select empno from emp9 e,dept_d2 where e.empno = dept_2.d2.
    when i ran this i gort a message-
    ORA-32034: unsupported use of WITH clause.
    please help me to solve this iisue..
    when i ran it separatly without using union/union all it ran sucessfully..
    thanks in advance..

    923315 wrote:
    hello all,
    i am facing some issue when i use with clause and union all operator.
    i have created a dummy code to solve this problem..
    my code is ----------
    with dept_1 as
    (select deptno d1 from detp9 where deptno = 20)
    select empno from emp9 e,dept_d1 where e.empno = dept_1.d1
    UNION ALL
    with dept_1 as
    (select deptno d2 from detp9 where deptno = 30)
    select empno from emp9 e,dept_d2 where e.empno = dept_2.d2.
    when i ran this i gort a message-
    ORA-32034: unsupported use of WITH clause.
    please help me to solve this iisue..
    when i ran it separatly without using union/union all it ran sucessfully..
    thanks in advance..Well, i don't see anything about these queries that makes sense.
    You are essentially joining emp and dept on EMPNO to DEPTNO ... that doesn't usually make any sense.
    How about you step back from the query which is almost certainly incorrect, and explain your tables, their data and what you need as output?
    Cheers,

  • Use of WITH clause

    Hi,
    I am using Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 .
    I am working in a procedure where I want to manipulate the data produced by a WITH AS clause.  But it is giving "PL/SQL: ORA-00942: table or view does not exist" error. A small example to illustrate my problem is given below. Here in the example am just trying to get a replaced string by using the out put of a WITH AS clause.
    SET SERVEROUTPUT ON;
    DECLARE
    CNT INTEGER;
    LETTER CHAR(1);
    REPLACED_STRING VARCHAR2(10);
    BEGIN
    REPLACED_STRING := 'ABC';
    WITH T AS
      (SELECT 'ABC' VAL FROM DUAL),
      TMP (LEN, SUBVAL) AS
      (SELECT LENGTH(VAL) LEN,
        SUBSTR(VAL, 1, INSTR(VAL, 'B', 1, 1)) SUBVAL
       FROM T
       UNION ALL
       SELECT LENGTH(VAL)-1 LEN,
       SUBSTR(VAL, 2, INSTR(VAL, 'C', 1, 1)) SUBVAL
       FROM T
      SELECT COUNT(*) INTO CNT FROM TMP,T ;
      FOR I IN 1..CNT LOOP
      SELECT SUBSTR(SUBVAL,1,1) INTO LETTER FROM  TMP,T;
      SELECT REPLACE(REPLACED_STRING,LETTER,'X')INTO REPLACED_STRING FROM DUAL;
      DBMS_OUTPUT.PUT_LINE(REPLACED_STRING);
    END LOOP;
    END;
    I thought of declaring a cursor which will hold the data produced by WITH clause but it did not work. Can you please let me know what are the possible options to do this.
    Thanks

    I think, I can not use the WITH inside loop as, I want to manipulate on data resulted by WITH reading row by row from it. I have a complex procedure and I wont be able to explain that scenario here and thus i came up with a similar problem through a very simple example. I will try to explain my problem thru my example here.
    The WITH in my example give an out put as :
    SUBVAL
    AB
    BC
      WITH T AS
      (SELECT 'ABC' VAL FROM DUAL),
      TMP (LEN, SUBVAL) AS
      (SELECT LENGTH(VAL) LEN,
        SUBSTR(VAL, 1, INSTR(VAL, 'B', 1, 1)) SUBVAL
       FROM T
       UNION ALL
       SELECT LENGTH(VAL)-1 LEN,
       SUBSTR(VAL, 2, INSTR(VAL, 'C', 1, 1)) SUBVAL
       FROM T
      SELECT subval FROM TMP,T;
    and then by using this in the PLSQL block mentioned above
      FOR I IN 1..CNT LOOP
      SELECT SUBSTR(SUBVAL,1,1) INTO LETTER FROM  TMP,T;
      SELECT REPLACE(REPLACED_STRING,LETTER,'X')INTO REPLACED_STRING FROM DUAL;
      DBMS_OUTPUT.PUT_LINE(REPLACED_STRING);
      END LOOP;
    I want to have an output like:
    XBC
    XXC
    Please note that nature of my original problem is that I want to manipulate the data given by WITH clause in the same procedure by passing it to other functions/procs to get the desired result. So please advice me following this approach only which would be helpful.
    Thanks

  • Need help in 'WITH CLAUSE' Query

    Hello Gurus,
    I am trying to calculate the count of distinct members for each provid.
    I am using the with clause to get information regarding the provid.
       WITH T AS
      (SELECT a.UD_ID MRR_ID,
                            A.UD_LASTNAME LAST_NAME,
                            A.UD_FIRSTNAME FIRST_NAME,
                            COUNT(DISTINCT DP.PA_PROVIDERID) PROVIDERS_ASSIGNED
                       FROM (SELECT UD.UD_ID,
                                    UD_LASTNAME,
                                    UD_FIRSTNAME
                               FROM USER_DETAILS       UD,
                                    MAP_USERS_TO_ROLES MR
                              WHERE MR.MUR_UR_ID_REF = 1000
                                AND MR.MUR_UD_ID_REF = UD.UD_ID) A,
                            D4C_PROVIDER_ASSIGNMENT DP
                      WHERE A.UD_ID = DP.PA_ASSIGNEDTO
                      AND dp.pa_status ='A'
                      GROUP BY A.UD_ID,
                               A.UD_LASTNAME,
                               A.UD_FIRSTNAME
                      ORDER BY 3 DESC)    OUTPUT of just above query without WITH clause.
    MRR_ID     LAST_NAME     FIRST_NAME     PROVIDERS_ASSIGNED
    1229    mrrTest         mrrTest         4
    1228    mrr2Last        mrr2First       5
    1230    mrr1Last        mrr1First       7
    1226    Panwar          SIngh           1
    1181    MRRLast         MRRTest         4
    1221    One             MRR             1
    1322    Thakuria        Bibhuthi        2I am creating this and get all the information to show on front end. Now I want to calculate the no of members as per the providers assigned for each MRR_ID
    Below query show the no of members for all the providers assigned to a provider.
    ex:
    SELECT * FROM (   
    SELECT COUNT(DISTINCT dmpc_hicn) countmember
    FROM D4C_HICN_PROVIDER_claims  a WHERE trim(DPMC_PROVIDER_NO) IN 
    (SELECT trim(pa_providerid)
       FROM d4c_provider_assignment
       WHERE pa_assignedto = 1181  (mrr_id)    --here i have use the mrrid from with clause and get the member count with all the columns coming from WITH CLAUSE.
       AND pa_roleid = 1000
       AND PA_STATUS ='A'
    GROUP BY a.dmpc_ss_id_ref)Right now I am using materialized view i dont wanna use the same..
    I am sending the materialized view code as well what i am doing ..
    ( SELECT SUM(member_count)member_count_bynpi ,mrr_id  FROM (
    (SELECT count(DISTINCT hp.dmpc_hicn) member_count,hp.DMPC_SS_ID_REF ,a1.ud_id mrr_id FROM  D4C_HICN_PROVIDER_claims hp ,
    (SELECT   a.UD_ID ,DP.PA_PROVIDERID
         FROM (SELECT UD.UD_ID, UD_LASTNAME, UD_FIRSTNAME
                 FROM USER_DETAILS UD, MAP_USERS_TO_ROLES MR
                WHERE MR.MUR_UR_ID_REF = 1000
                  AND MR.MUR_UD_ID_REF = UD.UD_ID) A,
              D4C_PROVIDER_ASSIGNMENT DP
        WHERE A.UD_ID = DP.PA_ASSIGNEDTO
        AND dp.pa_status ='A'
        /*AND dp.PA_ASSIGNEDTO = 1221*/) a1
        WHERE trim(a1.PA_PROVIDERID) = trim(hp.dpmc_provider_no)
        GROUP BY a1.ud_id,hp.DMPC_SS_ID_REF))
        GROUP BY mrr_id)Please help me to write the code with the materialized view. Thanks in Advance.
    Kind regards,
    UP
    Edited by: BluShadow on 22-Aug-2011 07:58
    fixed {noformat}{noformat} tags.  Please use lowercase "code" rather than uppercase "CODE" in the tags.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

    Probably, this may help
    WITH T1 as(
               SELECT UD.UD_ID,
                      UD_LASTNAME,
                      UD_FIRSTNAME
                FROM USER_DETAILS UD, MAP_USERS_TO_ROLES MR
                WHERE MR.MUR_UR_ID_REF = 1000
                AND MR.MUR_UD_ID_REF = UD.UD_ID)
          T2 as (SELECT a.UD_ID ,DP.PA_PROVIDERID
                 FROM T1 A, D4C_PROVIDER_ASSIGNMENT DP
                 WHERE A.UD_ID = DP.PA_ASSIGNEDTO
                 AND dp.pa_status ='A')
           T3 as(SELECT count(DISTINCT hp.dmpc_hicn) member_count,
                              hp.DMPC_SS_ID_REF ,
                               a1.ud_id mrr_id
                  FROM T2 A1 ,D4C_HICN_PROVIDER_claims hp
                  WHERE trim(a1.PA_PROVIDERID) = trim(hp.dpmc_provider_no)
                  GROUP BY a1.ud_id,hp.DMPC_SS_ID_REF)
    SELECT SUM(member_count)member_count_bynpi ,mrr_id from T3
    GROUP BY mrr_id

Maybe you are looking for