Subquery_factoring_clause

Dear all,
I've been thinking of subquery_factoring_clause, and some ways to optimize them, and end up with the following question:
- Does Discoverer generates queries with subquery_factoring_clause?
I try to go deep in the discoverer documentation but couldn't find any reference to the types of queries that Discoverer generates.
Thanks
Marco

Hi Vikas,
You download patches for ODP.NET from MetaLink just as you would with other Oracle patches. As mentioned by Yvonne, the patch number for version 9.2.0.4.10 is 3867855. However, the latest 9i ODP.NET patch is version 9.2.0.4.15 and that patch number is 4147997.
Hope that helps,
Mark

Similar Messages

  • With clause vs sub-queries

    db and dev 10g rel2 , hi all,
    i am trying to learn the "WITH CLAUSE" , and i do not see any difference between using it and using sub-queries .
    when i searched for this , i found that the with clause is used when You need to reference the subquery block multiple places in the query by specifying the query name , but i can not imagine an example for doing so .
    if you could provide me with an example please ? and telling me about another situations in which i could need using the "with clause" if any ?
    thanks

    >
    db and dev 10g rel2 , hi all,
    i am trying to learn the "WITH CLAUSE" , and i do not see any difference between using it and using sub-queries .
    when i searched for this , i found that the with clause is used when You need to reference the subquery block multiple places in the query by specifying the query name , but i can not imagine an example for doing so .
    if you could provide me with an example please ? and telling me about another situations in which i could need using the "with clause" if any ?
    >
    It isn't just about referencing a subquery multiple times. There are other advantages to using 'common table expressions'/'subquery factoring' also.
    Take a look at the example below. It first defines 'dept_costs' as a query block, then defines 'avg_cost' as a new query block and it references the first query block.
    Then the actual query references both query blocks just as if they are tables. And, in fact, in some circumstances Oracle will actually materialize them AS temporary tables.
    Look at how easy it is to understand the entire statement. You can focus first on the 'dept_costs' query block WITHOUT having to look at anything else. That is because the query block is self-contained; you are defining a result set. There is no 'join' or connection to any other part of the statement.
    It is easy for a developer, and for Oracle, to understand what is needed for that one piece.
    Next you can focus entirely on the 'avg_cost' query block. Since it uses the first query block just as if it were a table you can treat it as a table. That means you do NOT even need to look at the first query block to understand what the second query block is doing.
    Same with the actual query. You can analyze it by treating the two query blocks just as if they were other tables.
    Even better you can test the first query block by itself in sql*plus or other tool to confirm that it works and you can create an execution plan for it to make sure it will use an appropriate index. You can also then test the first and second query blocks together to make sure THEY have a proper execution plan.
    Then when you test then entire statement you already know that the query blocks work correctly.
    Try to do THAT with a query that uses nested sub-queries.
    Sure - you could write a set of nested sub-queries to accomplish the same thing (Oracle will sometimes rewrite your query that way itself) but it becomes one big query and the individual pieces are not nearly as easy to see, analyze or understand.
    It can be difficult if not impossible to extract a nested query in order to test it even to just try to get the syntax working. And when you do extract it you will often be testing something that isn't quite exactly the same as when i t was nested.
    So: easier to understand, easier to write and test (especially for new developers) as well as easier to use multiple times without having to duplicate it.
    >
    subquery_factoring_clause
    The WITH query_name clause lets you assign a name to a subquery block. You can then reference the subquery block multiple places in the query by specifying the query name. Oracle Database optimizes the query by treating the query name as either an inline view or as a temporary table.
    >
    The SQL Language doc has an example.
    http://docs.oracle.com/cd/B28359_01/server.111/b28286/statements_10002.htm#i2129904
    >
    Subquery Factoring: Example The following statement creates the query names dept_costs and avg_cost for the initial query block containing a join, and then uses the query names in the body of the main query.
    WITH
    dept_costs AS (
    SELECT department_name, SUM(salary) dept_total
    FROM employees e, departments d
    WHERE e.department_id = d.department_id
    GROUP BY department_name),
    avg_cost AS (
    SELECT SUM(dept_total)/COUNT(*) avg
    FROM dept_costs)
    SELECT * FROM dept_costs
    WHERE dept_total >
    (SELECT avg FROM avg_cost)
    ORDER BY department_name;
    DEPARTMENT_NAME DEPT_TOTAL
    Sales 313800
    Shipping 156400

  • PL/SQL WITH Clause

    Hi Gurus,
    I have been trying to find more information regarding the WITH CLAUSE, but most of the info I find is related to Forums. Do you know where i could find Oracle specific documentation on this? I really like using that clause, but i have noticed some drastic performance differences with it compared to the same query run outside of the WITH CLAUSE.
    Just wanted to find some documentation on it and can't seem to find anything.
    As always greatly appreciate the info/feedback.
      Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - 64bi
      PL/SQL Release 10.2.0.3.0 - Production
      CORE     10.2.0.3.0     Production
      TNS for Linux: Version 10.2.0.3.0 - Production
      NLSRTL Version 10.2.0.3.0 - ProductionThanks,
    S

    Here it goes:
    Using With Clause:
    11g
    http://download.oracle.com/docs/cd/E11882_01/server.112/e10578/tdpdw_sql.htm#TDPDW0073
    10g
    http://download.oracle.com/docs/cd/B19306_01/server.102/b14223/aggreg.htm#sthref1670
    subquery_factoring_clause:
    11g
    http://download.oracle.com/docs/cd/E11882_01/server.112/e10592/statements_10002.htm#SQLRF01702
    10g
    http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/statements_10002.htm#SQLRF01702
    Edited by: fsitja on Apr 15, 2010 5:14 PM

  • WITH CLAUSE: ORA-32034

    Hello,
    I have a query that requires WITH clauses since it reiterates a similar select, so I'm down with 2 WITH clause which I cannot unionize. How can I do this?
    Thanks Pierre
    with DURCDR as (select CALLINGPARTYNUMBER,CALLINGPARTYIMSI,CALLINGPARTYIMEI,CALLEDPARTYNUMBER,CALLEDPAR
    TYIMSI,CALLEDPARTYIMEI,
    CHARGINGSTARTTIME,REDIRECTINGPARTYIMEI,REDIRECTINGPARTYIMSI,REDIRECTINGPARTYNUMB
    ER from bgw_durcdr where CHARGINGSTARTTIME BETWEEN '01-05-2007:00:00:00' and '01-05-2007:23:59:59'
    and (CALLEDPARTYIMEI IN '2980' or CALLINGPARTYIMEI IN '2980' OR REDIRECTINGPARTYIMEI IN '2980'))
    SELECT
    CALLEDPARTYNUMBER,
    CALLEDPARTYIMSI,
    CALLEDPARTYIMEI,
    CHARGINGSTARTTIME
    FROM DURCDR
    UNION
    SELECT
    CALLINGPARTYNUMBER,
    CALLINGPARTYIMSI,
    CALLINGPARTYIMEI,
    CHARGINGSTARTTIME
    FROM DURCDR
    UNION
    SELECT
    REDIRECTINGPARTYIMEI,
    REDIRECTINGPARTYIMSI,
    REDIRECTINGPARTYNUMBER,
    CHARGINGSTARTTIME
    FROM
    DURCDR ==> GET ERROR ORA-32034
    UNION
    WITH EVTCDR AS(select DESTINATION,DESTINATIONIMSI,DESTINATIONIMEI,CHARGINGSTARTTIME,ORIGINIMEI,ORIGINI
    MSI,ORIGIN,REDIRECTINGPA
    RTYIMEI,REDIRECTINGPARTYIMSI,REDIRECTINGPARTYNUMBER FROM BGW_EVTCDR WHERE CHARGINGSTARTTIME BETWEEN '01-05-2007:00:00:00' and '01-05-2007:23:59:59'
    AND( DESTINATIONIMEI IN '2980' OR ORIGINIMEI IN '2980' OR REDIRECTINGPARTYIMEI IN '2980'))
    SELECT
    DESTINATION,
    DESTINATIONIMSI,
    DESTINATIONIMEI,
    CHARGINGSTARTTIME
    FROM EVTCDR
    UNION
    SELECT
    ORIGINIMEI,
    ORIGINIMSI,
    ORIGIN,
    CHARGINGSTARTTIME
    FROM EVTCDR
    UNION
    SELECT
    REDIRECTINGPARTYIMEI,
    REDIRECTINGPARTYIMSI,
    REDIRECTINGPARTYNUMBER,
    CHARGINGSTARTTIME
    FROM EVTCDR;

    (although the example you gave still fails in v10) Really ?
    SQL> select banner from v$version where rownum = 1;
    BANNER
    Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod
    SQL> with d as (select deptno from emp), 
      2  s as (select deptno from dept) 
      3  select * from d 
      4  union 
      5  select * from s;
        DEPTNO
            10
            20
            30
            40Your example works with subquery which is the separate select statement.
    SQL> with t as (select 1 a from dual)
      2  select * from t where a in (
      3  with x as (select 2 b from dual)
      4  select b from x
      5  )
      6  /
    no rows selected
    SQL> with t as (select 1 a from dual),
      2  x as (select 2 b from dual)
      3  select * from t where a in (
      4  select b from x
      5  )
      6  /
    no rows selectedhttp://download-uk.oracle.com/docs/cd/B19306_01/server.102/b14200/statements_10002.htm#i2065646
    Restrictions on Subquery Factoring This clause is subject to the following restrictions:
    You can specify only one subquery_factoring_clause in a single SQL statement. You cannot specify a query_name in its own subquery. However, any query_name defined in the subquery_factoring_clause can be used in any subsequent named query block in the subquery_factoring_clause.
    In a compound query with set operators, you cannot use the query_name for any of the component queries, but you can use the query_name in the FROM clause of any of the component queries.
    Rgds.

  • Need help with cursor def

    Hi I have seen some where in the code
    .what I could not understand is
    1.what does the e with clause in this code will do? where it is used frequently and what is the advantage?
    2.the temp_holding is a table with only one column.So is it like we can use with caluse in the cursor with only one columned tables?
    3.why is the AS keyword used in this?
    4.If I want to write a select staement for a cursor like this and see what is the data contained in the cursor ,how to write it ?
    thanks in adavnce for your help..
    OPEN lv_refcur FOR
    WITH TEMP_HOLDINGS AS
    SELECT A1.VENDOR_INSTRUMENT_ID,A1.DATA_SOURCE_CD FROM FI_IDX_BENCHMARK_HOLDINGS A1, FI_IDX_BENCHMARK B1, FI_IDX_SOURCE C1
    WHERE
    A1.PRICING_DT = pv_in_dt AND A1.DATA_SOURCE_CD = pv_in_data_src_cd AND A1.INDEX_CD = B1.INDEX_CD
    AND B1.INDEX_CD = C1.INDEX_CD AND C1.DATA_SOURCE_CD = A1.DATA_SOURCE_CD AND B1.IS_PA_REQUIRED = 'Y'
    UNION
    SELECT A2.VENDOR_INSTRUMENT_ID,A2.DATA_SOURCE_CD FROM FI_IDX_FORWARD_HOLDINGS A2, FI_IDX_BENCHMARK B2, FI_IDX_SOURCE C2
    WHERE
    A2.PRICING_DT = pv_in_dt AND A2.DATA_SOURCE_CD = pv_in_data_src_cd AND A2.INDEX_CD = B2.INDEX_CD
    AND B2.INDEX_CD = C2.INDEX_CD AND C2.DATA_SOURCE_CD = A2.DATA_SOURCE_CD AND B2.IS_PA_REQUIRED = 'Y'
    -- MDR START MC IGAR Disclosure change
    UNION
    SELECT
    A1.VENDOR_INSTRUMENT_ID,
    A1.DATA_SOURCE_CD
    FROM
    FI_IDX_BENCHMARK_HOLDINGS A1,
    FI_IDX_BENCHMARK B1,
    FI_IDX_SOURCE C1,
    fi_group_member GM
    WHERE
    A1.PRICING_DT = pv_in_dt
    AND GM.group_cd = 'BCGLBIDXPA'
    AND GM.purpose_cd = 'GLOBALIDX'
    AND A1.DATA_SOURCE_CD = GM.character_val
    AND A1.INDEX_CD = B1.INDEX_CD
    AND B1.INDEX_CD = C1.INDEX_CD
    AND C1.DATA_SOURCE_CD = pv_in_data_src_cd
    AND B1.IS_PA_REQUIRED = 'N'
    UNION
    SELECT
    A2.VENDOR_INSTRUMENT_ID,
    A2.DATA_SOURCE_CD
    FROM
    FI_IDX_FORWARD_HOLDINGS A2,
    FI_IDX_BENCHMARK B2,
    FI_IDX_SOURCE C2,
    fi_group_member GM
    WHERE
    A2.PRICING_DT = pv_in_dt
    AND GM.group_cd = 'BCGLBIDXPA'
    AND GM.purpose_cd = 'GLOBALIDX'
    AND A2.DATA_SOURCE_CD = GM.character_val
    AND A2.INDEX_CD = B2.INDEX_CD
    AND B2.INDEX_CD = C2.INDEX_CD
    AND C2.DATA_SOURCE_CD = pv_in_data_src_cd
    AND B2.IS_PA_REQUIRED = 'N'
    -- MDR END
    SELECT
    INSTRUMENT_ID,
    FUND_OR_INDEX_CD,
    PRICING_DT,
    FI_INSTRUMENT_ID,
    ISSUE_DESCRIPTION,
    TICKER,
    ISSUE_DT,
    STATED_MATURITY_DT,
    COUPON,
    STATE_CD,
    COUNTRY_CD,
    CURRENCY_CD,
    CALLABLE_FLAG,
    PUTABLE_FLAG,
    INSURED_FLAG,
    AMT_CD,
    REVENUE_SOURCE_CD,
    ISSUER_ID,
    NON_2A7_DIVER_ISSUER_ID,
    BLOOMBERG_MBS_TYPE,
    MBS_AGENCY_CD,
    ORIGINAL_TERM,
    DS_CLASS1_CD,
    DS_CLASS2_CD,
    DS_CLASS3_CD,
    MAX(LB_CLASS1_CD) LB_CLASS1_CD,
    MAX(LB_CLASS2_CD) LB_CLASS2_CD,
    MAX(LB_CLASS3_CD) LB_CLASS3_CD,
    MAX(LB_CLASS4_CD) LB_CLASS4_CD,
    GENERIC_INSTRUMENT_ID,
    MAX(SC_CLASS1_CD) SC_CLASS1_CD,
    MAX(SC_CLASS2_CD) SC_CLASS2_CD,
    MAX(SC_CLASS3_CD) SC_CLASS3_CD,
    MAX(SC_CLASS4_CD) SC_CLASS4_CD
    FROM (
    SELECT
    DISTINCT
    PV_FND_IDX_CD AS FUND_OR_INDEX_CD,
    IAI.FI_INSTRUMENT_ID AS FI_INSTRUMENT_ID,
    -- MC IGAR Disclosure
    decode( pv_in_data_src_cd, 'LBG', decode (I.INSTRUMENT_DOMAIN_CD, 'MBS', I.cusip, IAI.ALTERNATE_ID), IAI.ALTERNATE_ID) AS INSTRUMENT_ID,
    -- MC IGAR Disclosure
    pv_in_dt AS PRICING_DT,
    I.ISSUE_DESC AS ISSUE_DESCRIPTION,
    I.BLOOMBERG_TICKER AS TICKER,
    DECODE(pv_in_data_src_cd,'LBG',I.ISSUE_DT,NULL) AS ISSUE_DT,
    I.STATED_MATURITY_DT AS STATED_MATURITY_DT,
    I.COUPON AS COUPON,
    I.STATE_CD AS STATE_CD,
    I.COUNTRY_CD AS COUNTRY_CD,
    I.CURRENCY_CD AS CURRENCY_CD,
    I.CALLABLE_IND AS CALLABLE_FLAG,
    I.PUTABLE_IND AS PUTABLE_FLAG,
    DECODE(pv_in_data_src_cd,'LBG',I.INSURED_IND,NULL) AS INSURED_FLAG,
    I.AMT_CD AS AMT_CD,
    I.REVENUE_SOURCE_CD AS REVENUE_SOURCE_CD,
    I.MASTER_ISSUER_ID AS ISSUER_ID,
    I.NON_2A7_DIVER_ISSUER_ID AS NON_2A7_DIVER_ISSUER_ID,
    MBS.BLOOMBERG_MBS_TYPE AS BLOOMBERG_MBS_TYPE,
    MBS.MBS_AGENCY_CD AS MBS_AGENCY_CD,
    MBS.ORIGINAL_TERM AS ORIGINAL_TERM,
    NULL AS DS_CLASS1_CD,
    NULL AS DS_CLASS2_CD,
    NULL AS DS_CLASS3_CD,
    DECODE (S.CLASSIFICATION_SCHEME_CD, 'LBCC', S.CLASSIFICATION_LEVEL1_CD)
    AS LB_CLASS1_CD,
    DECODE (S.CLASSIFICATION_SCHEME_CD, 'LBCC', S.CLASSIFICATION_LEVEL2_CD)
    AS LB_CLASS2_CD,
    DECODE (S.CLASSIFICATION_SCHEME_CD, 'LBCC', S.CLASSIFICATION_LEVEL3_CD)
    AS LB_CLASS3_CD,
    DECODE (S.CLASSIFICATION_SCHEME_CD, 'LBCC', S.CLASSIFICATION_LEVEL4_CD)
    AS LB_CLASS4_CD,
    NULL AS GENERIC_INSTRUMENT_ID,
    DECODE (S.CLASSIFICATION_SCHEME_CD, 'SCIS', S.CLASSIFICATION_LEVEL1_CD)
    AS SC_CLASS1_CD,
    DECODE (S.CLASSIFICATION_SCHEME_CD, 'SCIS', S.CLASSIFICATION_LEVEL2_CD)
    AS SC_CLASS2_CD,
    DECODE (S.CLASSIFICATION_SCHEME_CD, 'SCIS', S.CLASSIFICATION_LEVEL3_CD)
    AS SC_CLASS3_CD,
    DECODE (S.CLASSIFICATION_SCHEME_CD, 'SCIS', S.CLASSIFICATION_LEVEL4_CD)
    AS SC_CLASS4_CD
    FROM
    INSTRUMENT I,
    INSTRUMENT_SECTOR S,
    TEMP_HOLDINGS H,
    INSTRUMENT_ALTERNATE_ID IAI,
    INSTRUMENT_MBS MBS,
    FI_IDX_INSTRUMENT FII
    WHERE
    H.DATA_SOURCE_CD = FII.DATA_SOURCE_CD
    AND H.VENDOR_INSTRUMENT_ID = FII.VENDOR_INSTRUMENT_ID
    AND FII.FMR_CUSIP = IAI.ALTERNATE_ID
    AND IAI.FI_INSTRUMENT_ID = I.FI_INSTRUMENT_ID
    AND IAI.FI_INSTRUMENT_ID = S.FI_INSTRUMENT_ID(+)
    AND IAI.FI_INSTRUMENT_ID = MBS.FI_INSTRUMENT_ID(+)
    AND IAI.ALTERNATE_ID_TYPE_CODE = 'FMR_CUSIP'
    GROUP BY INSTRUMENT_ID, FUND_OR_INDEX_CD, PRICING_DT, FI_INSTRUMENT_ID,
    ISSUE_DESCRIPTION, TICKER, ISSUE_DT, STATED_MATURITY_DT, COUPON, STATE_CD,
    COUNTRY_CD, CURRENCY_CD, CALLABLE_FLAG, PUTABLE_FLAG, INSURED_FLAG, AMT_CD,
    REVENUE_SOURCE_CD, ISSUER_ID, NON_2A7_DIVER_ISSUER_ID, BLOOMBERG_MBS_TYPE,
    MBS_AGENCY_CD, ORIGINAL_TERM, DS_CLASS1_CD, DS_CLASS2_CD, DS_CLASS3_CD,
    GENERIC_INSTRUMENT_ID;
    Edited by: 953115 on Dec 5, 2012 2:04 AM

    953115 wrote:
    1.what does the e with clause in this code will do? where it is used frequently and what is the advantage?The WITH clause is called subquery factoring, not easy to find in the manual if you don't know that
    http://docs.oracle.com/cd/E11882_01/server.112/e26088/statements_10002.htm#i2161315
    http://docs.oracle.com/cd/E11882_01/server.112/e26088/statements_10002.htm#i2077142
    >
    subquery_factoring_clause
    The WITH query_name clause lets you assign a name to a subquery block. You can then reference the subquery block multiple places in the query by specifying query_name. Oracle Database optimizes the query by treating the query name as either an inline view or as a temporary table.
    >
    Simply, it is like creating a view local to the query that can be used one or more times in the rest of the query. It has added benefits such as the potential to materialize the results, explicitly push predicates into the subquery, and perform hierarchical queries using the recursive feature.
    >
    2.the temp_holding is a table with only one column.So is it like we can use with caluse in the cursor with only one columned tables?No you can put any query in the WITH clause
    >
    3.why is the AS keyword used in this?Developer preference? It is optional when specifying a column alias, it seems many of the aliases in your query are unnecessary since they just duplicate the existing column name.
    4.If I want to write a select staement for a cursor like this and see what is the data contained in the cursor ,how to write it ?You can view the results from the cursor the same way as any select query, e.g.
    SQL> var c refcursor
    SQL> begin
      2    open :c for
      3      with test_data as
      4      (
      5      select 1 n, 'a' s from dual union all
      6      select 2 n, 'b' s from dual union all
      7      select 5 n, 'x' s from dual
      8      )
      9      select n, s, case when n > 2 then 'High' else 'Low' end y
    10      from test_data;
    11  end;
    12  /
    PL/SQL procedure successfully completed.
    SQL> print c
             N S Y
             1 a Low
             2 b Low
             5 x High

Maybe you are looking for