Rewrite this query

How to rewrite this query. It took more than 30 minutes

 SELECT  t.svc_ord_nbr
     FROM (SELECT a.svc_ord_nbr
             FROM m04_nt_uv_ord_admin a, m04_cct_nbr_range b
            WHERE a.adabas_isn = b.adabas_isn) t where t.svc_ord_nbr not in (select c.SVC_ORD_NBR
                                                                   from  m04_nt_uv_ord_admin c
                                                                   having count(*)>1
                                                                   group by c.svc_ord_nbr)

May use are:

/* Formatted on 2012/06/18 17:36 (Formatter Plus v4.8.8) */
SELECT t.svc_ord_nbr
  FROM (SELECT a.svc_ord_nbr
          FROM m04_nt_uv_ord_admin a, m04_cct_nbr_range b
         WHERE a.adabas_isn = b.adabas_isn) t
 WHERE NOT EXISTS (SELECT   1
                       FROM m04_nt_uv_ord_admin c
                      WHERE t.svc_ord_nbr = c.svc_ord_nbr
                     HAVING COUNT (*) > 1
                   GROUP BY c.svc_ord_nbr)

Tags: Database

Similar Questions

  • How to rewrite this query to get the correct results?

    Friends,

    DB: 9iR2

    I need to get the name of the employee and the employee number that are not in the table of presence.
    but this query is not the right answer.
    select e.eno,e.ename from empl e
    where e.eno not in (select a.eno from attendance a)
    Thank you

    Depending on your data

    SQL> create table attendance(
      2  ENO   VARCHAR2(5),
      3   TDATE VARCHAR2(10),
      4   IN_TIME  VARCHAR2(6),
      5   OUT_TIME VARCHAR2(6),
      6   SHIFT_NO  NUMBER(1));
    
    Table created.
    
    SQL>  create table empl(
      2   ENO VARCHAR2(5),
      3   ENAME  VARCHAR2(75));
    
    Table created.
    
    SQL> insert into empl values('11','AA');
    
    1 row created.
    
    SQL>   insert into empl values('12','AB');
    
    1 row created.
    
    SQL>    insert into empl values('13','AC');
    
    1 row created.
    
    SQL>     insert into empl values('14','AD');
    
    1 row created.
    
    SQL>   insert into empl values('15','AF');
    
    1 row created.
    
    SQL>  insert into attendance values('11','23-3-2009','9.00','6.00',1);
    
    1 row created.
    
    SQL>  insert into attendance values('14','24-3-2009','9.00','6.00',1);
    
    1 row created.
    
    SQL>  insert into attendance values('11','25-3-2009','9.00','6.00',1);
    
    1 row created.
    
    SQL>  insert into attendance values('13','23-3-2009','9.00','6.00',1);
    
    1 row created.
    
    SQL>  insert into attendance values('15','23-3-2009','9.00','6.00',1);
    
    1 row created.
    
    SQL> commit;
    
    Commit complete.
    
    select e.eno,e.ename
    from empl e
    where not exists(select 1 from attendance a where a.eno=e.eno);
    
    ENO     ENAME
    
    12     AB
    

    Twinkle

  • Rewrite the query in 11 g

    Hello

    Can anyone suggest an idea to rewrite this query? This query takes longer than expected, probably
    they way the application is made is not fair? Thanks in advance! I use 11g
     SELECT   brand_id,
                          region_id,
                          sku_base_id,
                          desc_key,
                          (SELECT   MAX (cps.product_code)
                             FROM   cat_prod_sku_vw cps
                            WHERE       cps.brand_id = srl.brand_id
                                    AND cps.region_id = srl.region_id
                                    AND cps.brand_id = 4
                                    AND cps.region_id = 0
                                    AND (   cps.parent_cat_id
                                         || 'PROD'
                                         || cps.prod_base_id) = srl.desc_text)
                             AS desc_text
                   FROM   sku_rgn_lang_vw srl
                  WHERE       brand_id = 4
                          AND region_id = 0
                          AND desc_key LIKE 'www_pcode_%'
                          AND LENGTH (desc_text) > 6
                          AND desc_text LIKE 'CAT%' ;

    You should look at this thread {message identifier: = 9360003} to get an idea of what other information is required.

    However, I'd be inclined to re - write as an (external) join instead of a scalar select statement. Something more like:

    SELECT srl.brand_id, srl.region_id, srl.sku_base_id, srl.desc_key,
           cps.desc_text
    FROM sku_rgn_lang_vw srl
       left join (SELECT brand_id, region_id,
                         parent_cat_id||'PROD'||prod_base_id desc_text
                         MAX (product_code) product_code
                  FROM cat_prod_sku_vw cps
                  WHERE brand_id = 4
                    AND region_id = 0
                  GROUP BY brand_id, region_id,
                           parent_cat_id||'PROD'||prod_base_id) cps
          on cps.brand_id = srl.brand_id AND
             cps.region_id = srl.region_id AND
             cps.parent_cat_id||'PROD'||cps.prod_base_id = srl.desc_text
    WHERE srl.brand_id = 4
      AND srl.region_id = 0
      AND srl.desc_key LIKE 'www_pcode_%'
      AND LENGTH (cps.desc_text) > 6
      AND cps.desc_text LIKE 'CAT%' ;
    

    Although I'm not entirely sure, I had all the correct alias.

    John

  • Rewriting a query...

    Hi all

    DB version is 10.2.0.4 a sql runs for hours. When I saw the explain plan command it shows using his "count stopkey". Find out where I heard using rownum can cause unexpected sub-optimal execution plans... How to rewrite this query?
    SQL> explain plan for SELECT JPDH.RESOURCES   FROM APPS.PROD_DATA_HEADERS_ALL JPDH
      2                WHERE JPDH.TRANS_ID =  (SELECT TRANS_ID   FROM
      3               PROD_DATA_OUTPUTS_ALL JPDO  WHERE JPDH.TRANS_ID =
      4               JPDO.TRANS_ID  AND ROWNUM = 1  AND JPDO.BATCH_ID = :b1 )
      5  ;
    
    Explained.
    
    SQL> select * from table(dbms_xplan.display);
    
    PLAN_TABLE_OUTPUT
    ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    Plan hash value: 2503736831
    
    --------------------------------------------------------------------------------------------------------------
    | Id  | Operation                     | Name                         | Rows  | Bytes | Cost (%CPU)| Time     |
    --------------------------------------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT              |                              |     1 |    16 |  3735   (1)| 00:00:47 |
    |*  1 |  FILTER                       |                              |       |       |            |          |
    |   2 |   TABLE ACCESS FULL           |     PROD_DATA_HEADERS_ALL    |  1775K|    27M|  3731   (1)| 00:00:47 |
    |*  3 |   *COUNT STOPKEY*               |                              |       |       |            |          |
    |*  4 |    TABLE ACCESS BY INDEX ROWID|     PROD_DATA_OUTPUTS_ALL    |     1 |    12 |     4   (0)| 00:00:01 |
    |*  5 |     INDEX RANGE SCAN          |     PROD_DATA_OUTPUTS_ALL_N1 |     3 |       |     3   (0)| 00:00:01 |
    --------------------------------------------------------------------------------------------------------------
    Baskar.l

    This is a rewritten query that has more chance that the optimizer can it turn into something more effective.

    select jpdh.resources
      from apps.prod_data_headers_all jpdh
     where exists
           ( select 'dummy'
               from prod_data_outputs_all jpdo
              where jpdo.trans_id = jpdh.trans_id
                and jpdo.batch_id = :b1
           )
    

    If it does not work, then please follow the advice of this thread: {: identifier of the thread = 501834}

    Kind regards
    Rob.

  • Rewrite the query

    Hello
    I have a standard report oracle question which i've drawn from the data model
    is it possible to rewrite this query without the bind variable, also the data provided as output is appropriate even when rewritten
    WHT does & C_vendor_dynamic_SQL stand
    SELECT /*+ ORDERED_PREDICATES */  DISTINCT (v.vendor_id)           C_vendor_id,
                       v.vendor_name                               C_vendor_name,
         DECODE(:SORT_BY_ALTERNATE, 'Y', UPPER(v.vendor_name_alt),
                       UPPER(v.vendor_name))      C_sort_vendor_name,
         v.segment1          C_vendor_num
    FROM    ap_invoices i, po_vendors v
    WHERE  0 < (SELECT SUM(nvl(prepay_amount_remaining,amount)) 
                     FROM  ap_invoice_distributions aid 
                     WHERE aid.invoice_id = i.invoice_id 
                     AND   aid.line_type_lookup_code IN ('ITEM','TAX') 
                     AND   nvl(aid.reversal_flag,'N') <> 'Y')
    AND v.vendor_id = i.vendor_id   
    AND i.invoice_type_lookup_code =  'PREPAYMENT'
    AND       i.cancelled_date IS NULL
    &C_vendor_dynamic_SQL
    ORDER BY DECODE(:SORT_BY_ALTERNATE, 'Y', UPPER(v.vendor_name_alt), UPPER(v.vendor_name))
    thanking in advance

    Published by: makdutakdu on June 21, 2010 10:54

    I guess then this fine shud work, so was that, in the cud of user earlier request, give then here with the two bind and lexical values ignored the user cannot enter, but even of output?

    Yes, the output will be the same.
    The user can always have enter the values if you have not deleted the user settings, but even if the user enters the values the report output will be same each time.

  • Rewrite the query without views

    I remember seeing a function in oracle PL/SQL package, which would be to rewrite a query as it contained no views more (but the only real tables)... but I don't remember the name of the function.

    What function/package for this?

    Hello

    looking for the new feature of 12 c DBMS_UTILITY. EXPAND_SQL_TEXT?

    Concerning

    Marcus

  • How can rewrite the query statement effectively

    Hi gurus,

    BANNER

    ----------------------------------------------------------------

    Oracle Database 10g Release 10.2.0.4.0 - Production 64-bit

    PL/SQL Release 10.2.0.4.0 - Production

    CORE 10.2.0.4.0 Production

    AMT for Linux: release 10.2.0.4.0 - Production

    NLSRTL Version 10.2.0.4.0 - Production

    Table scripts


    CREATE TABLE WAREHOUSE

    (

    IDZONE VARCHAR2 (6 BYTE) NOT NULL,

    ZONE_CREATEDATE DATE NOT NULL,

    DATE OF DT_WAREHOUSE,

    DATE OF DT_POSTING

    )

    AREA ID, ZONE_CREATEDATE is PK

    CREATE TABLE ZONE_VIEW

    (

    IDZONE VARCHAR2 (6 BYTE) NOT NULL,

    ZONE_CREATEDATE DATE NOT NULL,

    ZONE_NUM NUMBER (2) NOT NULL,

    DATE OF TRANX_DATE

    )

    ID of the AREA, ZONE_CREATEDATE, ZONE_NUM is PK

    Query

    SELECT ID area, zone_createdate

    Of

    (

    SELECT ID area, zone_createdate,

    MAX (CASE WHEN zone_num = 18 AND tranx_date 'n' IS NOT NULL, THEN 1 TIME zone_num = 18 AND tranx_date IS NULL THEN 0 ELSE-1 END) region_18,.

    MAX (CASE WHEN zone_num = 110 AND not IS NOT NULL, THEN 1 TIME = 110 zone_num tranx_date AND tranx_date IS NULL THEN 0 ELSE-1 END) region_110,.

    MAX (CASE WHEN zone_num = 135 AND tranx_date no IS NOT NULL, THEN 1 TIMES zone_num = 135 AND tranx_date IS NULL THEN 0 ELSE-1 END) region_135,.

    MAX (CASE WHEN zone_num = 140 AND tranx_date 'n' IS NOT NULL, THEN 1 TIME zone_num = 140 AND tranx_date IS NULL THEN 0 ELSE-1 END) region_140

    OF zone_view

    GROUP BY IDZone, zone_createdate

    ) zrn

    WHERE zrn.region_18 <>0

    AND (((zrn.region_110 = 1) OR (zrn.region_110 = - 1)) AND (there IS NOT (SELECT null from warehouse w WHERE w.zoneid = zrn.zoneid AND w.zone_createdate = zrn.zone_createdate AND w.dt_warehouse IS NULL)))

    AND (((zrn.region_135 = 1) AND (there IS NOT (SELECT null from warehouse w WHERE w.zoneid = zrn.zoneid AND w.zone_createdate = zrn.zone_createdate AND w.dt_posting IS NULL))) OR (zrn.region_140 = 1))

    OR ((zrn.region_18 >-1) AND (zrn.region_135 = - 1) AND (zrn.region_140 < 1))

    OR ((zrn.region_110 = 1) AND (zrn.region_135 = - 1) AND (zrn.region_140, <>, 0))

    );

    Top query runs too slowly on the real data set. Is there an effective way to rewrite the query which can perform the dough?

    Any help or suggestion would be appreciated

    Thanks in advance

    At the time where the application may not throw anything until after he has retrieved and grouped all the ranks of zone_view. His estimate of 7.5 minutes to scan millions 639 lines really fast enough - it's 85 million lines per minute.

    This seems to be where the time went. The only way that the query can be accelerated is to recover some of the conditions that must be applied before the grouping. The only obvious possibilities for this are

    (a) filter values of zone_num

    (b) move the audit only

    NOT EXISTS (SELECT null from warehouse w WHERE w.zoneid = zrn.zoneid AND w.zone_createdate = zrn.zone_createdate AND w.dt_warehouse IS NULL)

    within the group, because this condition is applied regardless of the values in calculated fields. The AREA ID, ZONE_CREATEDATE are not null, so I suggest to try:

    SELECT ID area, zone_createdate

    Of

    (

    SELECT ID area, zone_createdate,

    MAX (CASE WHEN zone_num = 18 AND tranx_date 'n' IS NOT NULL, THEN 1 TIME zone_num = 18 AND tranx_date IS NULL THEN 0 ELSE-1 END) region_18,.

    MAX (CASE WHEN zone_num = 110 AND not IS NOT NULL, THEN 1 TIME = 110 zone_num tranx_date AND tranx_date IS NULL THEN 0 ELSE-1 END) region_110,.

    MAX (CASE WHEN zone_num = 135 AND tranx_date no IS NOT NULL, THEN 1 TIMES zone_num = 135 AND tranx_date IS NULL THEN 0 ELSE-1 END) region_135,.

    MAX (CASE WHEN zone_num = 140 AND tranx_date 'n' IS NOT NULL, THEN 1 TIME zone_num = 140 AND tranx_date IS NULL THEN 0 ELSE-1 END) region_140

    OF zone_view

    where (IDZone, zone_createdate) NOT IN (select the zone ID, w warehouse zone_createdate WHERE w.dt_warehouse IS NULL)

    and zone_num (18, 110, 135, 140)

    GROUP BY IDZone, zone_createdate

    ) zrn

    WHERE zrn.region_18 <> 0

    AND ((zrn.region_110 = 1) OR (zrn.region_110 = - 1))

    AND (((zrn.region_135 = 1) AND (there IS NOT (SELECT null from warehouse w WHERE w.zoneid = zrn.zoneid AND w.zone_createdate = zrn.zone_createdate AND w.dt_posting IS NULL))) OR (zrn.region_140 = 1))

    OR ((zrn.region_18 >-1) AND (zrn.region_135 = - 1) AND (zrn.region_140)<>

    OR ((zrn.region_110 = 1) AND (zrn.region_135 = - 1) AND (zrn.region_140 <> 0))

    )

    Unfortunately, if the plan of the query time estimates are correct, most of the duration of the query is spent doing a reading zone_view in a full analysis, and which will not be changed by this query.

    If there are many rows for each (IDZone, zone_createdate) zone_view pair, then it is possible that an index on (area ID, zone_num, zone_createdate) would contribute to this request, especially if most (IDZone, zone_createdate) pairs are filtered with the NOT IN (select the zone ID, zone_createdate w warehouse WHERE w.dt_warehouse IS NULL). More radical but probably effective measure would be a functional on index

    zone_view (zone_num, IDZone, zone_createdate, case when tranx_date is null then 0 otherwise 1 end)

    and using the expression in the query box:

    SELECT ID area, zone_createdate

    Of

    (

    SELECT ID area, zone_createdate,

    NVL (MAX (CASE WHEN zone_num = 18 then case when tranx_date is null, then 0 or 1 end end),-1) region_18,.

    NVL (MAX (CASE WHEN zone_num = 110 then case when tranx_date is null, then 0 or 1 end end),-1) region_110,.

    NVL (MAX (CASE WHEN zone_num = 135 then case when tranx_date is null, then 0 or 1 end end),-1) region_135,.

    NVL (MAX (CASE WHEN zone_num = 140 then case when tranx_date is null, then 0 or 1 end end),-1) region_140

    OF zone_view

    where (IDZone, zone_createdate) NOT IN (select the zone ID, w warehouse zone_createdate WHERE w.dt_warehouse IS NULL)

    and zone_num (18, 110, 135, 140)

    GROUP BY IDZone, zone_createdate

    ) zrn

    WHERE zrn.region_18 <> 0

    AND ((zrn.region_110 = 1) OR (zrn.region_110 = - 1))

    AND (((zrn.region_135 = 1) AND (there IS NOT (SELECT null from warehouse w WHERE w.zoneid = zrn.zoneid AND w.zone_createdate = zrn.zone_createdate AND w.dt_posting IS NULL))) OR (zrn.region_140 = 1))

    OR ((zrn.region_18 >-1) AND (zrn.region_135 = - 1) AND (zrn.region_140)<>

    OR ((zrn.region_110 = 1) AND (zrn.region_135 = - 1) AND (zrn.region_140 <> 0))

    )

    This gives a very different plan on my database, by performing an iteration on relevant areas of performance status:

    ---------------------------------------------------------

    | ID | Operation | Name |

    ---------------------------------------------------------

    |   0 | SELECT STATEMENT |              |

    |*  1 |  FILTER                          |              |

    |   2.   VIEW                           |              |

    |*  3 |    FILTER                        |              |

    |   4.     HASH GROUP BY.              |

    |   5.      ANTI NESTED LOOPS.              |

    |   6.       INLIST ITERATOR.              |

    |*  7 |        INDEX RANGE SCAN | ZV_F1 |

    |*  8 |       TABLE ACCESS BY INDEX ROWID | WAREHOUSE |

    |*  9 |        INDEX UNIQUE SCAN | WAREHOUSE_PK |

    | * 10 |   TABLE ACCESS BY INDEX ROWID | WAREHOUSE |

    | * 11 |    INDEX UNIQUE SCAN | WAREHOUSE_PK |

    ---------------------------------------------------------

    Your plan will be different because you have data volumes (I didn't create millions of lines of test data)

    It would be useful to know how many rows there are in the WAREHOUSE, how many distinct (IDZone, zone_createdate) pairs there are in zone_view and the number of rows actually returns the query, and how much is NOT IN (select the zone ID, w zone_createdate warehouse WHERE w.dt_warehouse IS NULL) because these relative numbers determine how these approaches are.

    If there is a table containing all of the (area ID, zone_createdate) pairs which could take place in zone_view, which would have may another possible approach:

    First filter possible pairs against NOT IN (select the zone ID, w warehouse zone_createdate WHERE w.dt_warehouse IS NULL)

    Then attach them to the zone_view to retrieve only the rows in the filtered (IDZone, zone_createdate) pairs.

    Good luck.

  • Rewrite the query with joins, and group by

    Hello

    It's an interview question.

    Table names: bookshelf_checkout
    virtual library

    And the join condition between these two tables is title

    We need to rewrite under request without using the join condition and group by clause?

    SELECT b.title,max(bc.returned_date - bc.checkout_date) "Most Days Out"
               FROM bookshelf_checkout bc,bookshelf b
               WHERE bc.title(+)=b.title
               GROUP BY b.title;
    When I was in College, I read most of SELECT statements can be replaced by operations base SQL (DEFINE the OPERATORS). Now, I am rewriting the query with SET operators, but not able to get the exact result.

    Kindly help me on this.

    Thank you
    Suri

    Something like that?

      1  WITH books AS (
      2  SELECT 'title 1' title FROM dual UNION ALL
      3  SELECT 'title 2' FROM dual UNION ALL
      4  SELECT 'title 3' FROM dual ),
      5  bookshelf AS (
      6  SELECT 'title 1' title, DATE '2012-05-01' checkout_date, DATE '2012-05-15' returned_date FROM dual UNION ALL
      7  SELECT 'title 1' title, DATE '2012-05-16' checkout_date, DATE '2012-05-20' returned_date FROM dual UNION ALL
      8  SELECT 'title 2' title, DATE '2012-04-01' checkout_date, DATE '2012-05-15' returned_date FROM dual )
      9  SELECT bs.title, MAX(bs.returned_date - bs.checkout_date) OVER (PARTITION BY title) FROM bookshelf bs
     10  UNION
     11  (SELECT b.title, NULL FROM books b
     12  MINUS
     13* SELECT bs.title, NULL FROM bookshelf bs)
    SQL> /
    
    TITLE   MAX(BS.RETURNED_DATE-BS.CHECKOUT_DATE)OVER(PARTITIONBYTITLE)
    ------- ------------------------------------------------------------
    title 1                                                           14
    title 2                                                           44
    title 3
    

    Lukasz

  • How to write this query in the hierarchy

    Hi gurus,

    Really need your help on this query.  Thank you very much in advance.

    SELECT
      t1.key as root_key ,
    (SELECT
          t2.unit_id AS unit_id 
          level-1 AS level ,
          t2.name,
          t2.creator
        FROM
          tab t2
          START WITH t2.unit_id       =   t1.unit_id            -----check each node as root
          CONNECT BY prior t2.unit_id = t2.parent_unit_id
    
      )
       t1.name as parent_unit_name
    FROM
      tab t1
    

    I'll write a query of the hierarchy as above, and that EACH line (node, totally more than 10200) is checked as root node to see how many sheets are accessible for her... It must be implemented in a single query.

    I know inline query should NOT return multiple rows or multiple columns, but the inline elements are necessary and can certainly be made in a correct solution.

    (env):

    Database Oracle 12 c Enterprise Edition Release 12.1.0.2.0 - 64 bit Production

    PL/SQL Release 12.1.0.2.0

    )

    Test data:

    select 1 as unit_id, null as parent_organization_unit_id, 'U1' as name from dual
    union all
    select 2, 1, 'U2' FROM DUAL
    UNION ALL
    SELECT 3, NULL, 'U3' FROM DUAL
    UNION ALL
    SELECT 4, 3, 'U4' FROM DUAL
    UNION ALL
    SELECT 5, 2, 'U5' FROM DUAL
    UNION ALL
    SELECT 6, 5, 'U6' FROM DUAL
    UNION ALL
    SELECT 7, 6, 'U7' FROM DUAL
    UNION ALL
    SELECT 8, 5, 'U8' FROM DUAL
    UNION ALL
    SELECT 9, 5, 'U9' FROM DUAL;
    

    Final result should be like this

    key unit_id,    level,   name, parent_name
    1    1    0    u1      u1
    1    2    1    u2       u1
    1    5    2     u5      u1
    1    6    3     u6      u1
    1    7    4    u7       u1
    1    8    3    u8       u1
    1    9    3     u9      u1
    2    2    0     u2       u2
    2    5    1      u5       u2
    2    6    2     u6       u2
    2    7    3      u7      u2
    2    8    2      u8       u2
    2    9    2      u9       u2
    
    

    Don't know how get you your output, it does not match your data...

    with tab as)

    Select 1 as unit_id, null as parent_organization_unit_id 'U1' as the name of double

    Union of all the

    Select 2, 1, 'U2' FROM DUAL

    UNION ALL

    SELECT 3, NULL, 'U3' FROM DUAL

    UNION ALL

    SELECT 4, 3, 'U4' FROM DUAL

    UNION ALL

    SELECT 5, 2, 'U5' OF THE DOUBLE

    UNION ALL

    SELECT 6, 5, 'U6' OF THE DOUBLE

    UNION ALL

    SELECT 7, 6, "U7" OF THE DOUBLE

    UNION ALL

    SELECT 8, 5, 'U8' FROM DUAL

    UNION ALL

    9. SELECT, 5, 'U9' FROM DUAL

    )

    Select dense_rank() key (order by connect_by_root unit_id), unit_id, level - 1 as 'LEVEL', connect_by_root name root_parent_name

    t tab

    Start with parent_organization_unit_id is null

    Connect prior unit_id = parent_organization_unit_id

    KEY UNIT_ID LEVEL ROOT_PARENT_NAME
    1 1 0 "U1".
    1 2 1 "U1".
    1 5 2 "U1".
    1 6 3 "U1".
    1 7 4 "U1".
    1 8 3 "U1".
    1 9 3 "U1".
    2 3 0 "U3".
    2 4 1 "U3".
  • How to optimize the performance of this query SQL

    Hello

    I need to find the age for each day, but I need for all previous dates in a single query. So I used the following query:

    Select trunc (sysdate) - level + 1 DATE

    trunc (sysdate) - level + 1 - created_date AGE

    elements

    connect by trunc (sysdate) - level + 1 - created_date > 0

    I get output (FOR the DATE and AGE) that is fine and correct:

    DATE AGE           

    --------- ----------

    6 JULY 15 22

    5 JULY 15 21

    4 JULY 15 20

    3 JULY 15 19

    2 JULY 15 18

    JULY 1, 15 17

    JUNE 30, 15 16

    JUNE 29, 15 15

    JUNE 28, 15 14

    JUNE 27, 15 13

    JUNE 26, 15 12

    25 JUNE 15 11

    24 JUNE 15 10

    Now I need to calculate the average age for each day and I added the average in the following query:

    Select trunc (sysdate) - level + 1 DATE .

    AVG (trunc (sysdate) - level + 1 - created_date) AVERAGE_AGE

    elements

    connect by trunc (sysdate) - level + 1 - created_date > 0

    Group of trunc (sysdate) - level + 1


    This query is correct? When I add the aggregate (avg) function to this query, it takes 1 hour to retrieve the data. When I remove the average request function that gives the result in 2 seconds? What is the solution to calculate the average without affecting performance? Help, please

    Maybe you are looking for something like this...

    SQL > ed
    A written file afiedt.buf

    1 with t (point, created_date) :)
    2 Select 1, date '' 2015-06-24 from all the double union
    3 select 2, date ' 2015-06-29 the Union double all the
    4 Select 3, date ' 2015-06-17' of the double
    5        )
    6  --
    7. end of test data
    8  --
    9. Select item
    10, trunc (sysdate) - level + 1 as dt
    11, trunc (sysdate) - level + 1-created_date age
    12, round (avg (trunc (sysdate) - level + 1 - created_date) on (trunc (sysdate) partition - level + 1), 2) as avg_in_day
    13 t
    14 connect by level<=>
    15-point point = prior
    sys_guid() 16 and prior is not null
    17 * order by 1.2
    SQL > /.
    POINT DT AGE AVG_IN_DAY
    ---------- ----------- ---------- ----------
    1 JUNE 24, 2015 0 3.5
    1 25 JUNE 2015 1 4.5
    1 26 JUNE 2015 2 5.5
    1 27 JUNE 2015 3 6.5
    1 28 JUNE 2015 4 7.5
    1 29 JUNE 2015 5 5.67


    1 30 JUNE 2015 6 6.67
    1 1 JULY 2015 7 7.67
    1-2 JULY 2015 8 8.67
    1-3 JULY 2015 9 9.67
    1 TO 4 JULY 2015 10 10.67
    1 5 JULY 2015 11 11.67
    1 6 JULY 2015 12 12.67
    2 JUNE 29, 2015 5.67 0
    2 30 JUNE 2015 1 6.67
    2 1 JULY 2015 2 7,67
    2 2 JULY 2015 3 8.67
    2-3 JULY 2015 4 9.67
    2-4 JULY 2015 5 10.67
    2-5 JULY 2015 6 11.67
    2-6 JULY 2015 7 12.67
    3 JUNE 17, 2015 0 0
    3 18 JUNE 2015 1 1
    3 19 JUNE 2015 2 2
    3 20 JUNE 2015 3 3
    3 21 JUNE 2015 4 4
    3 22 JUNE 2015 5 5
    3 23 JUNE 2015 6 6
    3 24 JUNE 2015 7 3.5
    3 25 JUNE 2015 8 4.5
    3 26 JUNE 2015 9 5.5
    3 27 JUNE 2015 10 6.5
    3 28 JUNE 2015 11 7.5
    3 29 JUNE 2015 12 5.67
    3 30 JUNE 2015 13 6.67
    3 1 JULY 2015 14 7.67
    3 2 JULY 2015 15 8.67
    3 3 JULY 2015 16 9.67
    3-4 JULY 2015 17 10.67
    3-5 JULY 2015 18 11.67
    3 6 JULY 2015 19 12.67

    41 selected lines.

  • Rewrite the query to improve the performance and the optimized below cost.

    Oracle 10g.

    ----------------------

    Query

    UPDATE FACETS_CUSTOM. MMR_DTL

    SET

    CAPITN_PRCS_IND = 2,

    FIL_RUN_DT = Current_fil_run_dt,

    ROW_UPDT_DT = dta_cltn_end_dttm

    WHERE CAPITN_PRCS_IND = 5

    AND HSPC_IND = 'Y '.

    AND EXISTS (SELECT 1

    OF FACETS_STAGE. CRME_FUND_DTL_STG STG_CRME

    WHERE STG_CRME. MBR_CK = MMR_DTL. MBRSHP_CK

    AND MMR_DTL. PMT_MSA_STRT_DT BETWEEN STG_CRME. ERN_FROM_DT AND STG_CRME. ERN_THRU_DT

    AND STG_CRME. FUND_ID IN ('AAB1', '1AA2', '1BA2', 'AAB2', '1AA3', '1BA3', ' 1 B 80 ', ' 1 HAS 80 '))

    AND EXISTS (SELECT 1

    OF FACETS_CUSTOM. FCTS_TMS_MBRID_XWLK XWLK

    WHERE XWLK. MBR_CK = MMR_DTL. MBRSHP_CK

    AND MMR_DTL. PMT_MSA_STRT_DT BETWEEN XWLK. HSPC_EVNT_EFF_DT AND XWLK. HSPC_EVNT_TERM_DT);

    Explain the plan of the query

    -----------------------------------------------

    Hash value of plan: 3109991485

    -------------------------------------------------------------------------------------------------------

    | ID | Operation | Name                  | Lines | Bytes | Cost (% CPU). Time |

    -------------------------------------------------------------------------------------------------------

    |   0 | UPDATE STATEMENT.                       |     1.   148. 12431 (2) | 00:02:30 |

    |   1.  UPDATE                       | MMR_DTL |       |       |            |          |

    |   2.   SEMI NESTED LOOPS.                       |     1.   148. 12431 (2) | 00:02:30 |

    |*  3 |    HASH JOIN RIGHT SEMI |                       |    49.  5488. 12375 (2) | 00:02:29 |

    |   4.     TABLE ACCESS FULL | FCTS_TMS_MBRID_XWLK |  6494 | 64940 |    24 (0) | 00:00:01 |

    |*  5 |     TABLE ACCESS FULL | MMR_DTL |   304K |    29 M | 12347 (2) | 00:02:29 |

    |*  6 |    TABLE ACCESS BY INDEX ROWID | CRME_FUND_DTL_STG |     1.    36.     5 (0) | 00:00:01 |

    |*  7 |     INDEX RANGE SCAN | IE1_CRME_FUND_DTL_STG |     8.       |     1 (0) | 00:00:01 |

    -------------------------------------------------------------------------------------------------------

    Information of predicates (identified by the operation identity card):

    ---------------------------------------------------

    3 - access("XWLK".") MBR_CK "=" MMR_DTL. " ("' MBRSHP_CK")

    filter ("XWLK". "HSPC_EVNT_EFF_DT" < = INTERNAL_FUNCTION ("MMR_DTL". " PMT_MSA_STRT_DT') AND

    'XWLK '. "" HSPC_EVNT_TERM_DT "> = INTERNAL_FUNCTION ("MMR_DTL". "PMT_MSA_STRT_DT")) "

    5 - filter("CAPITN_PRCS_IND"=5 AND "HSPC_IND"='Y')

    6 filter (("STG_CRME". "FUND_ID" = "1 HAS 80 ' OR 'STG_CRME'." " FUND_ID "="1AA2"OR"

    'STG_CRME '. "FUND_ID"= '1AA3' OR 'STG_CRME'. "FUND_ID" = "1 B 80 ' OR 'STG_CRME'. '. "FUND_ID" = "1BA2" OR "

    'STG_CRME '. "FUND_ID"= "1BA3" OR "STG_CRME". "FUND_ID"= "AAB1" OR "STG_CRME". ("FUND_ID"="AAB2") AND

    'STG_CRME '. "" ERN_FROM_DT "< = INTERNAL_FUNCTION ("MMR_DTL". "PMT_MSA_STRT_DT") AND "

    'STG_CRME '. "" ERN_THRU_DT "> = INTERNAL_FUNCTION ("MMR_DTL". "PMT_MSA_STRT_DT")) "

    7 - access("STG_CRME".") MBR_CK "=" MMR_DTL. " ("' MBRSHP_CK")

    I could not optimize this query for best performance and optimized the cost... Can someone guide me on this.

    Thank you

    DS

    You think you're going to lines updates 85K, Oracle think it will update a line.

    At the time where the existence of the first test runs that oracle think already up to 49 lines, which is probably why he uses the loop join nested for the second test. (In your version of Oracle, the subquery introduced existence a very bad assumption (small) on the amount of data will survive).

    It is possible that you will get better performance if you hint Oracle using a hash join for testing the existence - and you might want to think what test will eliminate most of the data and that we can first force.

    Having said that, however, note that MMR_DTL research is a considerable fraction of the cost of the query - and an analysis is an easy thing for Oracle cost properly - if, despite your comments on update a column with a clue to this topic, you will find that the query can be more effective if you use an index. This is more likely to be the case if data ' WHERE CAPITN_PRCS_IND = 5 AND HSPC_IND = 'Y' "is well grouped (perhaps the latest data added to the table).". "  You could then reduce the cost of maintaining this index by creating an index based on a feature that indexes only the lines where the predicate are both true so that the 2 update deletes the index entries and allows the index remain as thin as possible.

    Concerning

    Jonathan Lewis

  • How to optimize this query?

    Hello

    I have a query like this:

    Merge into the table st1

    using (select * from (select pk, value, diff_value, m_date, row_number () over (PARTITION pk ORDER BY diff_value) rnk)

    from (select distinct / * + Full (t1) full (t2) * / t1.pk, t2.m_date)

    , Case when (t1.m_date = t2.m_date) then "CORRESPONDENCE".

    When (t2.m_date BETWEEN t1.m_date-1 and t1.m_date + 1) then ' MATCHED WITH +/-1gg.

    When (t2.m_date BETWEEN t1.m_date-2 and t1.m_date + 2) then "MATCHED WITH +/-2 days.

    else "

    end value_match

    Case when (t1.m_date = t2.m_date) then 0

    Where (t2.m_date BETWEEN t1.m_date + 1 and t1.m_date - 1) then 1

    Where (t2.m_date BETWEEN t1.m_date + 1 and t1.m_date - 1) then 2

    else "

    end diff_value

    of table t2, t1 table

    where t1.value is null

    and t1.id = t2.id)

    where value_match is not null)

    where rnk = 1) s

    on (st1.pk = s.pk)

    WHEN MATCHED THEN

    Update set st1.value = s.value_match, st1.diff_value = s.diff_value, st1.up_date = s.m_date

    where st1.value is null.

    Explain the plan:

    EXPLAIN_PLAN1.jpg

    Table1 a record 3Million and table 2 has 1 million records.

    I used gather stats before you run this query and 'Full' trick, even in this case, he is running for 45 minutes.

    Please suggest the best solution to optimize this query.

    Thanks in advance.

    Remove the tips.

    No need for the separate.

    Get the diff by ceil (abs(t2.m_date-t1.m_date)) and the filter for that where value_diff<>

    Assing the statement ".. MATCHED" lately in the update clause.

    Maybe give exactly to your needs with a small example may be the query may be getting more simplified or not what you want it to do.

  • How to write this query?

    Hi people,

    I need to get a query in which a set of records, I get ONLY those which previous registry has a field with a value to this topic. Other values, the field can contain are not necessary.

    I know that sounds easy but... I can't get it.

    So, for Oracle 10 g 2... Here's my query:

    SELECT a.person_id, a.person_status, a.message_id, a.order_id

    OF t_HR one

    WHERE a.person_status = "rejected".

    AND a.id >

    (SELECT max (b.id)

    OF t_HR b

    WHERE b.person_id = a.person_id

    and b.order_id = a.order_id

    AND b.person_status! "revised =".

    B.ID AND < a.id)

    ORDER BY desc a.id

    Let me explain:

    1 - HR table is a table of people. These people has serveral STATUS.

    2 - ID is a sequential (each www.voyages-sncf.com has a different identification number).

    3 - the application must get THAT all people "rejected".

    4. - However, (subquery) I need ONLY those that previous register (the second register) holds a status of "OK". If the person holds a "revised" status he's not, he should be the next register (the third)

    5.-L' ORDER ID DESC, so is the first register must have a STATUS = "rejected" and the second a 'OK '.  IF the second register = "revised", then the third register must be 'OK '. And I need this query.

    HOW DO?

    My problem: the subquery gives you previous register of the same guy, but... it does not give you the value of the State, I need, which is 'OK '.

    I tried to add to the subquery...

    SELECT max (b.id)

    OF mod_human_resource b

    WHERE b.person_id = a.person_id

    and b.order_id = a.order_id

    AND b.person_status = 'OK '.

    AND b.id < a.id

    ... but if I have 5 records of that person, the first is "rejected", the second is "accepted", the third is 'new' and the fourth is 'OK'... the subquery gives you the 4th register and which is not correct for me, it must be only the second one (prior to the first State registry).

    I need to be a query, because I need to use it on a MERGER for a DWH.

    If there is another way (function, or even a procedure) to make the MERGER rather than with a request, which would be ok too. I am poor DWH knowledge.

    Thanks in advance.

    Hello

    So, you need to know if a line is the 'first' line, and you should also know what is the 'next' status, (even the 'first' and 'next' are already defined).  This sounds like a job for analytical functions.  ROW_NUMBER can tell you if a line is first or not, and LEAD can tell you what a value on the next row.

    Since you post CREATE TABLE and INSERT statements for your own table, I'll use the table scott.emp to illustrate.

    Consider these data from scott.emp:

    SELECT DeptNo

    ename

    work

    FROM scott.emp

    ORDER BY deptno

    ename DESC

    ;

    Output:

    DEPTNO ENAME JOB

    ---------- ---------- ---------

    10 MILLER CLERK

    PRESIDENT OF KING 10

    MANAGER 10 CLARK

    20 SMITH CLERK

    ANALYST SCOTT 20

    20 JONES MANAGER

    20 FORD ANALYST

    20 ADAMS CLERK

    30 WARD SALESMAN

    SELLER OF 30 TURNER

    30 MARTIN SALESMAN

    30 JAMES CLERK

    MANAGER BLAKE 30

    30 ALLEN SALESMAN

    Now, let's say we want only who know the departments where the forefront (in order descending ename) a job = 'CLERK', and the following line (also in descending by ename order) = "ANALYST" job, and we want to know the ename of the first row.  In other words, the correct output is:

    DEPTNO ENAME

    ---------- ----------

    20 SMITH

    Note that deptno = 10 is not included, even if the first task is to "CLERK." that was because the second job in deptno = 10 is the "PRESIDENT", not "ANALYST."

    Here's a way to get these results:

    WITH got_analytics AS

    (

    SELECT ename, deptno, job

    ROW_NUMBER () OVER (PARTITION BY deptno

    ORDER BY ename DESC

    ) AS r_num

    LEAD (employment) OVER (PARTITION BY deptno

    ORDER BY ename DESC

    ) AS next_job

    FROM scott.emp

    )

    SELECT deptno, ename

    OF got_analytics

    WHERE r_num = 1

    AND job = 'CLERK '.

    AND next_job = 'ANALYST '.

    ;

    I hope that answers your question.

    If this isn't the case, then, as Dan (and the FAQ forum) said, post CREATE TABLE and INSERT statements for some sample data and the exact results you want from these data.

    Post your query, based on the one I have posted more top and ponit out where he gets results.

    Always say what version of Oracle you are using (for example, 11.2.0.2.0).

    See the FAQ forum: https://forums.oracle.com/message/9362002#9362002

  • Dialog box that says ' Creative Cloud Installer wants to make changes. Type your password for this purpose"- I type my password for Adobe ID (creative cloud), but it will not accept it. who should I go for help with this query?

    Dialog box that says ' Creative Cloud Installer wants to make changes. Type your password for this purpose"- I type my password for Adobe ID (creative cloud), but it will not accept it. who should I go for help with this query?

    It does not ask the cloud your computer admin password password!

  • What is the name of this query

    Hi all

    I want to know the name of this query to learn about it, because I don't understand how it works.

    Select the columns tab "(select table columns)" table.

    I saw him, but I don't know how it works.

    Thanks in advance

    It is a scalar subquery expression.

    "An expression of scalar subquery is a subquery that returns exactly one column of a line."

    The value of the scalar subquery expression is the value of the select item from the list of the subquery.

    If the subquery returns 0 rows, then the value of the scalar subquery expression is NULL.

    If the subquery returns more than one line, then Oracle returns an error. »

    See:

    http://docs.Oracle.com/CD/E16655_01/server.121/e17209/expressions013.htm#SQLRF52093

    For example, we can write

    Select empno,

    Ename,

    DEPTNO,

    (select dname dept where deptno = emp.deptno) dname

    WCP

    order by empno

    /

    Instead of

    Select e.empno,

    e.Ename,

    e.DEPTNO,

    d.DNAME

    from emp e,.

    d Dep.

    where e.deptno = d.deptno

    order by empno

    /

Maybe you are looking for