Simplify this query

When I run this query, it will be 1 hour to run it. can we change this query to do this?
SELECT /*+ CHOOSE */a.*, b.*
  FROM (SELECT (  TO_DATE (ucraudt_new_value, 'DD-MON-YYYY')
                - TO_DATE (ucraudt_old_value, 'DD-MON-YYYY')
               ) AS diff,
               ucraudt.*
          FROM ucraudt
         WHERE ucraudt_column_code = '1GED'
           AND ucraudt_new_value IS NOT NULL
           AND ucraudt_old_value IS NOT NULL
           AND ucraudt_user_id LIKE 'UCPVCRV%') a,
       ucrserv b
WHERE a.diff > 74
   AND a.ucraudt_cust_code = b.ucrserv_cust_code
   AND a.ucraudt_prem_code = b.ucrserv_prem_code
   AND b.ucrserv_status_ind = 'A'

Try following

SELECT
     a.*, b.*
FROM
       ucraudt a,
       ucrserv b
 WHERE (TO_DATE (a.ucraudt_new_value, 'DD-MON-YYYY') - TO_DATE (a.ucraudt_old_value, 'DD-MON-YYYY'))  > 74
   AND a.ucraudt_cust_code = b.ucrserv_cust_code
   AND a.ucraudt_prem_code = b.ucrserv_prem_code
   AND b.ucrserv_status_ind = 'A'
   and a.ucraudt_column_code = '1GED'
   AND a.ucraudt_new_value IS NOT NULL
   AND a.ucraudt_old_value IS NOT NULL
   AND a.ucraudt_user_id LIKE 'UCPVCRV%' 

Also post the query execution plan

Tags: Database

Similar Questions

  • Can anyone simplify this query

    Here's a DOF from two tables

    1. CREATE TABLE (EMPL)
    NUMBER OF SNO
    ENAME VARCHAR2 (25).
    USE VARCHAR2 (25).
    KEY ELEMENTARY SCHOOL (SNO)
    );

    2. CREATE TABLE EMPL_DET)
    NUMBER OF SNO
    SAL VARCHAR2 (25)
    );


    Here are tables LMD
    INSERT INTO EMPL (SNO, ENAME, JOB) VALUES (1, 'SMITH', 'CLERK');
    INSERT INTO EMPL (SNO, ENAME, JOB) VALUES (2, 'SMITH', 'MANAGER');
    INSERT INTO EMPL (SNO, ENAME, JOB) VALUES (3, 'TOM', 'CLK');

    INSERT INTO EMPL_DET (SNO, SAL) VALUES (1, '1000');
    INSERT INTO EMPL_DET (SNO, SAL) VALUES (2, "10000");
    INSERT INTO EMPL_DET (SNO, SAL) VALUES (3, '900');


    I want to calculate TotalSAL (column: empl_det.) SAL) of each employee (empl.ename) with job-description (empl.job).

    Means I want following the lines of output
    1.(Job,TotalSAL,Ename)-> (CLERK, 11000, SMITH)
    2->.(Job,TotalSAL,Ename) (MANAGER, 11000, SMITH)
    3->.(Job,TotalSAL,Ename) (CLK, 900, TOM)

    I tried to write down to unique ename

    Select JOB, x.sal, ename in empl,
    (
    Select sum (sal) sal empl_det where sno in
    (select sno to empl where ename = 'SMITH')
    ) x
    where ename = 'SMITH '.
    order by ename

    each ename, I draw from this query. How can I make the ename list (TOM SMITH) to this request?
    Or can anyone simplify this query?

    Hello

    in this case, you need to use is analytical functions:

    that is to say:

      SELECT e.job, SUM (d.sal) OVER (PARTITION BY e.ename) AS "TotalSal"
           , e.ename
        FROM empl e, empl_det d
       WHERE d.sno = e.sno AND e.ename IN ('SMITH', 'TOM')
    ORDER BY e.ename;
    
    JOB                         TotalSal ENAME
    ------------------------- ---------- -------------------------
    CLERK                          11000 SMITH
    MANAGER                        11000 SMITH
    CLK                              900 TOM                      
    

    Kind regards.
    Al

    Published by: Alberto Faenza on 27 November 2012 15:34
    Corrected query

  • How to simplify this query in sql simple select stmt

    Hello

    Please simplify the query

    I want to convert this query in a single select statement. Is this possible?
    If uarserq_choice_ind is not null then

    Select ubbwbst_cust_code
    From ubbwbst,utrchoi
    Where utrchoi_prop_code=ubbwbst_cancel_prod
    Else

    Select max(utvsrvc_ranking)
    From utvsrvc,ubbwbst
    Where utvsrvc_code=ubbwbst_cancel_prod
    End if
    Select ubbwbst_cust_code as val
    From   ubbwbst,utrchoi
    Where  utrchoi_prop_code=ubbwbst_cancel_prod
    AND    uarserq_choice_ind is not null
    union all
    Select max(utvsrvc_ranking) as val
    From   utvsrvc,ubbwbst
    Where  utvsrvc_code=ubbwbst_cancel_prod
    and    uarserq_choice_ind is null
    

    Without more information, we are unable to combine the two queries in 1 without a union.
    Looks like you select values totally disperate of totally different tables

  • [8i] not to simplify this query?

    In my application, I need to pick up three possible prices for each part number in an array.
    The three possible prices that I need to recover are:
    (1) the price paid on the order more recently closed,
    (2) the price mentioned on the first pending order, and
    (3) the price on the furthest on pending order.

    However, there is a complication in that. Orders include the part number, but may also include a prefix of 4 characters (always the same) on a reference number which should be treated the same as the reference database. For example, "DSB-part1" should be considered "part1", "DSB-part2" should be regarded as 'part 2', etc.

    In addition, it is quite possible for several orders expected or closed on the same day, and I want to only return the price of one of these lines for each of the 3 methods of pricing.

    (Technically, there is another level of complication to this, but if I have problems with it later, I'll create a new job for him...)

    Some examples of data:
    CREATE TABLE     part
    (     part_no          VARCHAR2(9)     NOT NULL,
         part_desc     VARCHAR2(25),
         qty_instock     NUMBER,
         CONSTRAINT part_pk PRIMARY KEY (part_no)
    );
    
    INSERT INTO     part
    VALUES ('part1    ','description 1 here',5);
    INSERT INTO     part
    VALUES ('part2    ','description 2 here',10);
    INSERT INTO     part
    VALUES ('part3    ','description 3 here',0);
    
    CREATE TABLE     ords
    (     ord_no               NUMBER NOT NULL,
         ord_part_no          VARCHAR2(9),
         date_closed          DATE,
         orig_dock_date          DATE,
         date_due_instock     DATE,
         unit_price          NUMBER,
         qty_order          NUMBER,
         ord_stat          VARCHAR2(2),
         CONSTRAINT ords_pk PRIMARY KEY (ord_no)
    );
    
    INSERT INTO     ords
    VALUES (1,'part1    ',To_Date('01/01/2009','mm/dd/yyyy'),To_Date('12/01/2008','mm/dd/yyyy'),To_Date('12/15/2008','mm/dd/yyyy'),100,10,'CL');
    INSERT INTO     ords
    VALUES (2,'part1    ',To_Date('01/31/2009','mm/dd/yyyy'),To_Date('01/01/2009','mm/dd/yyyy'),To_Date('01/05/2009','mm/dd/yyyy'),105,15,'CL');
    INSERT INTO     ords
    VALUES (3,'part1    ',To_Date('01/31/2009','mm/dd/yyyy'),To_Date('01/15/2009','mm/dd/yyyy'),To_Date('01/20/2009','mm/dd/yyyy'),100,20,'CL');
    INSERT INTO     ords
    VALUES (4,'part1    ',To_Date('12/31/1900','mm/dd/yyyy'),To_Date('01/15/2009','mm/dd/yyyy'),To_Date('01/25/2009','mm/dd/yyyy'),103,10,'OP');
    INSERT INTO     ords
    VALUES (5,'ORD-part1',To_Date('12/31/1900','mm/dd/yyyy'),To_Date('01/20/2009','mm/dd/yyyy'),To_Date('01/31/2009','mm/dd/yyyy'),101,10,'OP');
    
    INSERT INTO     ords
    VALUES (6,'ORD-part2',To_Date('01/01/2009','mm/dd/yyyy'),To_Date('12/01/2008','mm/dd/yyyy'),To_Date('12/15/2008','mm/dd/yyyy'),100,10,'CL');
    INSERT INTO     ords
    VALUES (7,'part2    ',To_Date('01/31/2009','mm/dd/yyyy'),To_Date('01/01/2009','mm/dd/yyyy'),To_Date('01/05/2009','mm/dd/yyyy'),105,15,'CL');
    INSERT INTO     ords
    VALUES (8,'ORD-part2',To_Date('01/31/2009','mm/dd/yyyy'),To_Date('01/15/2009','mm/dd/yyyy'),To_Date('01/20/2009','mm/dd/yyyy'),100,20,'CL');
    INSERT INTO     ords
    VALUES (9,'part2    ',To_Date('12/31/1900','mm/dd/yyyy'),To_Date('01/15/2009','mm/dd/yyyy'),To_Date('01/25/2009','mm/dd/yyyy'),103,10,'OP');
    INSERT INTO     ords
    VALUES (10,'ORD-part2',To_Date('12/31/1900','mm/dd/yyyy'),To_Date('01/20/2009','mm/dd/yyyy'),To_Date('01/31/2009','mm/dd/yyyy'),101,10,'OP');
    
    INSERT INTO     ords
    VALUES (11,'part3    ',To_Date('01/01/2009','mm/dd/yyyy'),To_Date('12/01/2008','mm/dd/yyyy'),To_Date('12/15/2008','mm/dd/yyyy'),100,10,'CL');
    INSERT INTO     ords
    VALUES (12,'part3    ',To_Date('01/31/2009','mm/dd/yyyy'),To_Date('01/01/2009','mm/dd/yyyy'),To_Date('01/05/2009','mm/dd/yyyy'),105,15,'CL');
    INSERT INTO     ords
    VALUES (13,'ORD-part3',To_Date('01/31/2009','mm/dd/yyyy'),To_Date('01/15/2009','mm/dd/yyyy'),To_Date('01/20/2009','mm/dd/yyyy'),100,20,'CL');
    INSERT INTO     ords
    VALUES (14,'ORD-part3',To_Date('12/31/1900','mm/dd/yyyy'),To_Date('01/15/2009','mm/dd/yyyy'),To_Date('01/25/2009','mm/dd/yyyy'),103,10,'OP');
    INSERT INTO     ords
    VALUES (15,'part3    ',To_Date('12/31/1900','mm/dd/yyyy'),To_Date('01/20/2009','mm/dd/yyyy'),To_Date('01/31/2009','mm/dd/yyyy'),101,10,'OP');
    And here's my ugly query to get the results I want. It can be simplified?
    SELECT     p.part_no
    ,     p.part_desc
    ,     p.qty_instock
    ,     a2.unit_price          AS last_closed_price
    ,     a2.qty_order          AS last_closed_qty
    ,     a2.date_closed          AS last_closed_date
    ,     b2.unit_price          AS first_open_price
    ,     b2.qty_order          AS first_open_qty
    ,     b2.date_due_instock     AS first_open_date
    ,     c2.unit_price          AS last_open_date
    ,     c2.qty_order          AS last_open_qty
    ,     c2.date_due_instock     AS last_open_date
    FROM     part p
    ,     (
         SELECT     lc.part_no
         ,     lc.unit_price                                             
         ,     lc.qty_order
         ,     lc.date_closed
         ,     ROW_NUMBER() OVER(PARTITION BY lc.part_no ORDER BY lc.orig_dock_date DESC)     AS rnk_nbr
         FROM     (
              SELECT     CASE
                        WHEN     SUBSTR(o.ord_part_no,1,4)     = 'ORD-'
                        THEN     SUBSTR(o.ord_part_no,5)
                        ELSE     o.ord_part_no
                   END                                        AS part_no
              ,     o.date_closed
              ,     o.orig_dock_date
              ,     o.unit_price
              ,     o.qty_order
              FROM     ords o
              ) lc     --for last closed
         ,     (
              SELECT     a.part_no
              ,     MAX(a.date_closed)     AS last_closed_date
              FROM     (
                   SELECT     CASE
                             WHEN     SUBSTR(o.ord_part_no,1,4)     = 'ORD-'
                             THEN     SUBSTR(o.ord_part_no,5)
                             ELSE     o.ord_part_no
                        END                                        AS part_no
                   ,     o.date_closed
                   FROM     ords o
                   ) a
              GROUP BY     a.part_no
              ) a1
         WHERE     lc.part_no     = a1.part_no
         AND     lc.date_closed     = a1.last_closed_date
         ) a2
    ,     (
         SELECT     fo.part_no
         ,     fo.unit_price                                             
         ,     fo.qty_order
         ,     fo.date_due_instock
         ,     ROW_NUMBER() OVER(PARTITION BY fo.part_no ORDER BY fo.orig_dock_date DESC)     AS rnk_nbr
         FROM     (
              SELECT     CASE
                        WHEN     SUBSTR(o.ord_part_no,1,4)     = 'ORD-'
                        THEN     SUBSTR(o.ord_part_no,5)
                        ELSE     o.ord_part_no
                   END                                        AS part_no
              ,     o.date_due_instock
              ,     o.orig_dock_date
              ,     o.unit_price
              ,     o.qty_order
              FROM     ords o
              WHERE     o.ord_stat     = 'OP'
              ) fo     --for first open
         ,     (
              SELECT     b.part_no
              ,     MIN(b.date_due_instock)     AS first_open_date
              FROM     (
                   SELECT     CASE
                             WHEN     SUBSTR(o.ord_part_no,1,4)     = 'ORD-'
                             THEN     SUBSTR(o.ord_part_no,5)
                             ELSE     o.ord_part_no
                        END                                        AS part_no
                   ,     o.date_due_instock
                   FROM     ords o
                   WHERE     o.ord_stat     = 'OP'
                   ) b
              GROUP BY     b.part_no
              ) b1
         WHERE     fo.part_no          = b1.part_no
         AND     fo.date_due_instock     = b1.first_open_date
         ) b2
    ,     (
         SELECT     lo.part_no
         ,     lo.unit_price                                             
         ,     lo.qty_order
         ,     lo.date_due_instock
         ,     ROW_NUMBER() OVER(PARTITION BY lo.part_no ORDER BY lo.orig_dock_date DESC)     AS rnk_nbr
         FROM     (
              SELECT     CASE
                        WHEN     SUBSTR(o.ord_part_no,1,4)     = 'ORD-'
                        THEN     SUBSTR(o.ord_part_no,5)
                        ELSE     o.ord_part_no
                   END                                        AS part_no
              ,     o.date_due_instock
              ,     o.orig_dock_date
              ,     o.unit_price
              ,     o.qty_order
              FROM     ords o
              WHERE     o.ord_stat     = 'OP'
              ) lo     --for last open
         ,     (
              SELECT     c.part_no
              ,     MAX(c.date_due_instock)     AS last_open_date
              FROM     (
                   SELECT     CASE
                             WHEN     SUBSTR(o.ord_part_no,1,4)     = 'ORD-'
                             THEN     SUBSTR(o.ord_part_no,5)
                             ELSE     o.ord_part_no
                        END                                        AS part_no
                   ,     o.date_due_instock
                   FROM     ords o
                   WHERE     o.ord_stat     = 'OP'
                   ) c
              GROUP BY     c.part_no
              ) c1
         WHERE     lo.part_no          = c1.part_no     --EDIT: changed from l1 to c1
         AND     lo.date_due_instock     = c1.last_open_date     --EDIT: changed from l1 to c1
         ) c2
    WHERE     p.part_no     = a2.part_no
    AND     a2.part_no     = b2.part_no
    AND     b2.part_no     = c2.part_no
    AND     a2.rnk_nbr     = 1
    AND     b2.rnk_nbr     = 1
    AND     c2.rnk_nbr     = 1
    And here are the results, I expect to get, according to data from the sample:
    .                         LAST_     LAST_     LAST_          FIRST_     FIRST_     FIRST_          LAST_     LAST_     LAST_
    .                    QTY_     CLOSED_     CLOSED_     CLOSED_          OPEN_     OPEN_     OPEN_          OPEN_     OPEN_     OPEN_
    PART_NO     PART_DESC          INSTOCK     PRICE     QTY     DATE          PRICE     QTY     DATE          PRICE     QTY_     DATE_
    ---------------------------------------------------------------------------------------------------------------------------------
    part1     description 1 here     5     100     20     1/31/2009     103     10     1/25/2009     101     10     1/31/2009
    part2     description 2 here     10     100     20     1/31/2009     103     10     1/25/2009     101     10     1/31/2009
    part3     description 3 here     0     100     20     1/31/2009     103     10     1/25/2009     101     10     1/31/2009
    Published by: user11033437 on February 5, 2010 08:48
    Correction of errors (see the comments in the request above for changes)

    Hello

    It is a little bit shorter than what you have posted, probably more effective and (in my opinion) much easier to debug and maintain:

    SELECT       p.part_no
    ,       p.part_desc
    ,       p.qty_instock
    ,       o.last_closed_price
    ,       o.last_closed_qty
    ,       o.last_closed_date
    ,       o.first_open_price
    ,       o.first_open_qty
    ,       o.first_open_date
    ,       o.last_open_price
    ,       o.last_open_qty
    ,       o.last_open_date
    FROM       part          p
    ,       (          -- Begin in-line view o for pivoted order data
               SELECT    part_no
               ,          MAX (CASE WHEN lc_num = 1 THEN unit_price           END)  AS last_closed_price
               ,          MAX (CASE WHEN lc_num = 1 THEN qty_order              END)  AS last_closed_qty
               ,          MAX (CASE WHEN lc_num = 1 THEN date_closed            END)  AS last_closed_date
               ,          MAX (CASE WHEN fo_num = 1
                                    AND  ord_stat = 'OP'  THEN unit_price       END)  AS first_open_price
               ,          MAX (CASE WHEN fo_num = 1
                                    AND  ord_stat = 'OP'  THEN qty_order        END)  AS first_open_qty
               ,          MAX (CASE WHEN fo_num = 1
                                    AND  ord_stat = 'OP'  THEN date_due_instock END)  AS first_open_date
               ,          MAX (CASE WHEN lo_num = 1
                                    AND  ord_stat = 'OP'  THEN unit_price       END)  AS last_open_price
               ,          MAX (CASE WHEN lo_num = 1
                                    AND  ord_stat = 'OP'  THEN qty_order        END)  AS last_open_qty
               ,          MAX (CASE WHEN lo_num = 1
                                    AND  ord_stat = 'OP'  THEN date_due_instock END)  AS last_open_date
               FROM     (       -- Begin in-line view to get lc_, fo_, lo_num
                             SELECT    gpo.*
                       ,           ROW_NUMBER () OVER ( PARTITION BY  part_no
                                                        ORDER BY        date_closed          DESC
                                          ,            ord_no               DESC
                                        )      AS lc_num
                       ,           ROW_NUMBER () OVER ( PARTITION BY  part_no
                                                        ORDER BY        CASE
                                                      WHEN  ord_stat = 'OP'
                                                      THEN  1
                                                      ELSE  2
                                                  END
                                          ,           orig_dock_date
                                          ,            ord_no
                                        )      AS fo_num
                       ,           ROW_NUMBER () OVER ( PARTITION BY  part_no
                                                        ORDER BY        CASE
                                                      WHEN  ord_stat = 'OP'
                                                      THEN  1
                                                      ELSE  2
                                                  END
                                          ,           orig_dock_date          DESC
                                          ,            ord_no               DESC
                                        )      AS lo_num
                       FROM      (       -- Begin in-line view gpo to get part_no
                                      SELECT  ords.*
                               ,       RTRIM ( CASE
                                            WHEN  ord_part_no  LIKE 'ORD-%'
                                                      THEN  SUBSTR (ord_part_no, 5)
                                                      ELSE  ord_part_no
                                                     END
                                       )               AS part_no
                               FROM       ords
                                  )     gpo   -- End in-line view gpo to get part_no
                         )      -- End in-line view to get lc_, fo_, lo_num
               GROUP BY  part_no
           ) o          -- End in-line view o for pivoted order data
    WHERE       RTRIM (p.part_no)     = o.part_no
    ORDER BY  p.part_no
    ;
    

    Whenever you want to have a WHERE clause only applies to certain columns, think about a pivot and expression BOX.

    Is there a reason to have raw triling part_no or ord_part_no?

    Published by: Frank Kulash, February 5, 2010 13:26

    After this announcement, I saw that Max had posted essentially the same query.
    In general, where the solution of Max seems more simple, I like her way better.
    The only possible exception is tested for "OP" at the derivation of the values first_open and last_open. If none of the lines to an ord_stat part_no = "OP", solution of Max will be used a the lines with another ord_stat. That does not occur in the sample data, and I don't know if this is still possible in your application, but if this is the case, I don't know this isn't what you want.

  • 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 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.

  • 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

    /

  • Need a help with this query max

    Select SEARCH_ID, SEARCH_KEYWORD, ASSET_TYPE, COUNT of RELEVANCY_TABLE

    where SEARCH_KEYWORD = 'search_keyword '.

    and ASSET_TYPE is not null

    558search_keyword3Office
    559search_keyword7Table

    In fact, I have to get the asset_type which the count is the maximum. In this case, it should be "table". Any help?

    adfSonal wrote:

    Is there another way? I have to write this query in Java. So, I prefer avoiding rank or such functions.

    I will not get the desired result using simply select, where, max, rownum clauses?

    What do you mean by "I must write this query in Java? Means of the query will be run against a database of Oracle, correct?

    Any means using ROWNUM

    Select *.

    de)

    Select search_id

    search_keyword

    County

    asset_type

    of relevancy_table

    where search_keyword = 'search_keyword '.

    and asset_type is not null

    Order

    by count desc

    )

    where rownum = 1

  • This query may be fixed/rewritten?

    Version: 11.2

    The following query retrieves data from three sources. 1 query SELECT and 2 table (TAB1, tab 3) internal (TAB2).
    The stats are updated in the tables involved. But this query is very slow. Any room for improvement?

    This is the query
    select sum(TAB2.N_AVL_BAL) as c1, sum(TAB2.N_AVL_BAL_LCY) as c2
      from WMS_XTDI_CUST TAB1,
      ( SELECT A.*
             FROM WMS_XTF_ACCT_BALANCE A
            INNER JOIN WMS_STDI_REPFLAG C
               ON A.C_CRM_INST = C.C_CRM_INST
              AND A.N_LATEST_REC_SEQ = C.N_REP_FLAG) TAB2, WMS_XTDI_CUST_ACCT_REL TAB3
            where (TAB1.C_CRM_INST = TAB3.C_CRM_INST and
                  TAB1.ID_CUST = TAB3.ID_CUST and
                  TAB2.C_ACCT_CCY = TAB3.C_ACCT_CCY and
                  TAB2.C_CRM_INST = TAB3.C_CRM_INST and
                  TAB2.C_SRC_SYST = TAB3.C_SRC_SYST and
                  TAB2.X_ACCOUNT_NO = TAB3.X_ACCOUNT_NO and
                  TAB1.X_RLSHIP_MGR_ID = '7883943')
    The plan of the explain command
    ----------------------------------------------------------------------------------------------------------------------------------------
    | Id  | Operation                             | Name                   | Rows  | Bytes |TempSpc| Cost (%CPU)| Time     | Pstart| Pstop |
    ----------------------------------------------------------------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT                      |                        |     1 |    92 |       |   231K  (2)| 00:46:16 |       |       |
    |   1 |  SORT AGGREGATE                       |                        |     1 |    92 |       |            |          |       |       |
    |*  2 |   HASH JOIN                           |                        |  3527K|   309M|       |   231K  (2)| 00:46:16 |       |       |
    |   3 |    PART JOIN FILTER CREATE            | :BF0000                |   233 |  3262 |       |    36   (0)| 00:00:01 |       |       |
    |   4 |     TABLE ACCESS BY GLOBAL INDEX ROWID| WMS_XTDI_CUST          |   233 |  3262 |       |    36   (0)| 00:00:01 | ROWID | ROWID |
    |*  5 |      INDEX RANGE SCAN                 | IDX_XT_CUST_08         |   233 |       |       |     3   (0)| 00:00:01 |       |       |
    |*  6 |    HASH JOIN                          |                        |  2240K|   166M|   247M|   231K  (2)| 00:46:15 |       |       |
    |   7 |     PART JOIN FILTER CREATE           | :BF0001                |  4556K|   195M|       |   163K  (2)| 00:32:44 |       |       |
    |*  8 |      HASH JOIN                        |                        |  4556K|   195M|       |   163K  (2)| 00:32:44 |       |       |
    |   9 |       PART JOIN FILTER CREATE         | :BF0002                |     6 |    54 |       |     3   (0)| 00:00:01 |       |       |
    |  10 |        TABLE ACCESS FULL              | WMS_STDI_REPFLAG       |     6 |    54 |       |     3   (0)| 00:00:01 |       |       |
    |  11 |       PARTITION RANGE ALL             |                        |    14M|   495M|       |   163K  (2)| 00:32:43 |     1 |    40 |
    |  12 |        PARTITION LIST JOIN-FILTER     |                        |    14M|   495M|       |   163K  (2)| 00:32:43 |:BF0002|:BF0002|
    |  13 |         TABLE ACCESS FULL             | WMS_XTF_ACCT_BALANCE   |    14M|   495M|       |   163K  (2)| 00:32:43 |     1 |   400 |
    |  14 |     PARTITION LIST AND                |                        |  7096K|   223M|       | 40133   (1)| 00:08:02 |KEY(AP)|KEY(AP)|
    |  15 |      TABLE ACCESS FULL                | WMS_XTDI_CUST_ACCT_REL |  7096K|   223M|       | 40133   (1)| 00:08:02 |KEY(AP)|KEY(AP)|
    ----------------------------------------------------------------------------------------------------------------------------------------
    
    Predicate Information (identified by operation id):
    ---------------------------------------------------
    
       2 - access("TAB1"."C_CRM_INST"="TAB3"."C_CRM_INST" AND "TAB1"."ID_CUST"="TAB3"."ID_CUST")
       5 - access("TAB1"."X_RLSHIP_MGR_ID"='7883943')
       6 - access("A"."C_CRM_INST"="TAB3"."C_CRM_INST" AND "A"."C_ACCT_CCY"="TAB3"."C_ACCT_CCY" AND
                  "A"."C_SRC_SYST"="TAB3"."C_SRC_SYST" AND "A"."X_ACCOUNT_NO"="TAB3"."X_ACCOUNT_NO")
       8 - access("A"."C_CRM_INST"="C"."C_CRM_INST" AND "A"."N_LATEST_REC_SEQ"="C"."N_REP_FLAG")
    
    31 rows selected.
    SQL >
    SQL >


    -Line account tables involved in the query
    SQL> select count(*) From WMS_XTDI_CUST;
    
      COUNT(*)
    ----------
       1943009
    
    SQL> select count(*) From WMS_STDI_REPFLAG;
    
      COUNT(*)
    ----------
             6
    
    SQL> select count(*) From WMS_XTF_ACCT_BALANCE;
    
      COUNT(*)
    ----------
      14430174
    
    SQL>
    SQL> select count(*) from WMS_XTDI_CUST_ACCT_REL;
    
      COUNT(*)
    ----------
       7096125

    Omega-3 wrote:
    Thanks Jac.
    I tested your version EXISTS in the query. But the result set is different.

    Why you use two tables - WMS_XTDI_CUST TAB1 and WMS_XTDI_CUST_ACCT_REL?

    No column is selected among them?

    And it seems that they do not have a relationship 1:1 or n: 1 with the basic query...

  • What is the problem with this query?

    Hello

    I need some advice on how to change the following query (with perhaps some analytical function) to speed it up. Currently, it takes 6 + minutes. This query is executed in response to a request from the front-end application and 6 + min is certainly unacceptable.

    I am trying to provide as much information I can think, but if more information is needed, please let me know.

    I have a table called "wave_result". It contains millions of rows. PK is Wave_Id, Version_nbr, node_nbr and prod_nbr. For each 'wave_id + node_NBR + prod_nbr' there are several versions (version_nbr). In the following query, I try to extract a line with MAX version_nbr for combination of ' wave_id + node_NBR + prod_nbr.

    H3. Request:
    SELECT ip1.fnln_cat,
                ip1.sub_cat,
                ip1.bus_cat,
                NVL (SUM (ip1.lsu), 0) val
        FROM ideal_prod ip1, ideal_store s, wave_result wr
       WHERE  wr.wave_id = 51
             AND wr.prod_nbr = ip1.prod_nbr
             AND wr.wave_id = ip1.wave_id
             AND wr.version_nbr =
                    (SELECT MAX (wr1.version_nbr)
                       FROM wave_result wr1
                      WHERE   wr1.wave_id = wr.wave_id
                            AND wr1.node_nbr = wr.node_nbr
                            AND wr1.prod_nbr = wr.prod_nbr)
             AND NVL (wr.ovrd_dcsn_nm, wr.dcsn_nm) = 'Add'
             AND s.wave_id = wr.wave_id
             AND s.node_nbr = wr.node_nbr 
    GROUP BY ip1.fnln_cat, ip1.sub_cat, ip1.bus_cat
    H3. Rank of charges:
    ========
    "wave_result" is the largest table with millions of rows.
    Table                Total Rows       Rows for wave_id = 51
    Ideal_prod           188K             38K
    Ideal_store          3K               574
    Wave_result          90M              19M
    H3. Stats
    are updated almost daily by the DBA (not sure if that's a good or bad).

    H3. Explain the Plan:
    =========
    SELECT STATEMENT ALL_ROWS Cost: 330,737 Bytes: 401,787 Cardinality: 14,881 
         12 HASH GROUP BY Cost: 330,737 Bytes: 401,787 Cardinality: 14,881 
              11 VIEW VIEW SYS.VM_NWVW_2 Cost: 330,737 Bytes: 401,787 Cardinality: 14,881 
                   10 FILTER 
                        9 HASH GROUP BY Cost: 330,737 Bytes: 2,425,603 Cardinality: 14,881 
                             8 HASH JOIN Cost: 327,183 Bytes: 41,233,784 Cardinality: 252,968 
                                  1 INDEX RANGE SCAN INDEX (UNIQUE) APAPOPR.XPKIDEAL_STORE Cost: 4 Bytes: 13,202 Cardinality: 574 
                                  7 HASH JOIN Cost: 327,178 Bytes: 35,415,520 Cardinality: 252,968 
                                       5 HASH JOIN Cost: 198,619 Bytes: 18,764,328 Cardinality: 183,964 
                                            3 TABLE ACCESS BY INDEX ROWID TABLE APAPOPR.IDEAL_PROD Cost: 939 Bytes: 2,272,380 Cardinality: 37,873 
                                            4 TABLE ACCESS FULL TABLE APAPOPR.WAVE_RESULT Cost: 197,063 Bytes: 7,974,414 Cardinality: 189,867 
                                       6 INDEX RANGE SCAN INDEX (UNIQUE) APAPOPR.XPKWAVE_RESULT Cost: 82,467 Bytes: 721,495,854 Cardinality: 18,986,733 
    H3. Sample data:
    ===========
       For wave_id = 51
                     there are 28466854 rows in "wave_result" table
    
       For wave_id = 51 and node_nbr = '0201' and  prod_nbr = '0226960'
                     there are 3 rows in "wave_result" table
    H3. Version of database information are below to:
    =========================
    Oracle Database 11g Enterprise Edition Release 11.1.0.7.0 - 64bit Production
    PL/SQL Release 11.1.0.7.0 - Production
    CORE            11.1.0.7.0                        Production
    TNS for IBM/AIX RISC System/6000: Version 11.1.0.7.0 - Production
    NLSRTL Version 11.1.0.7.0 - Production
    Thank you very much!!

    Concerning
    For wave_id = 51
                     there are 28466854 rows in "wave_result" table
    
       For wave_id = 51 and node_nbr = '0201' and  prod_nbr = '0226960'
                     there are 3 rows in "wave_result" table
    

    As you mentioned that this particular request is called by a front screen, how different search criteria you get it to show on the front end?
    Assuming that if the user is searching just of wave_id = 51 which lines returns to 28466854, it is certainly not a good approach to go look up many records to put end Front end is very light and can not hold many records and will be finally the web server will throw off out of memory exception. Always a limit on the number of records that you retrieve from the database and display it on the front plane.
    OR
    The second case where the user makes a search by wave_id, node_nbr and prod_nbr, even if she returns to 3 lines, behind the scens, he still made a full scan on the wave_result table. If we go with analytical function, we can reduce a sweep of extra table on the wave_result table. Also the table has millions of lines and the max (version_nbr) seems to be a frequently used on this table sub query. While inserting a record into the table wave_result, you must probably have a calculated value that will tell you its value max and you will choose just the rank of this value, instead of at each time max(). something like below...

    SELECT ip1.fnln_cat,
                ip1.sub_cat,
                ip1.bus_cat,
                NVL (SUM (ip1.lsu), 0) val
        FROM ideal_prod ip1, ideal_store s, wave_result wr
       WHERE ipl.wave_id = 51
             AND wr.prod_nbr = ip1.prod_nbr
             AND wr.wave_id = ip1.wave_id
             AND wr.wave_id = s.wave_id
             AND wr.node_nbr = s.node_nbr
             AND wr.precomputed_max_value = 'MAX'
             AND NVL (wr.ovrd_dcsn_nm, wr.dcsn_nm) = 'Add'
    GROUP BY ip1.fnln_cat, ip1.sub_cat, ip1.bus_cat;
    
  • Sorting and grouping - two months in this query

    Hi all

    Thanks a lot for JAC

    I am doing a project for the construction company, I faced this problem by grouping points according to the relationship between these points the
    Relationship is from 1 to 100. If the point between what means there is relationship between these points has come.

    resolve this question already, but the results does not correct when the table contains more data.

    SQL - sorting and grouping.
    Jac and thanks a lot to him.

    This example for more details

    for example, I have these points
    id   location         percentage   comments 
    1     loc 1,2          20%                that mean point  1 and 2 close to each other by 20%
    2     loc 1,3          40%              that mean point 1 and 3 close to each other byy 40%
    3     Loc 8,6          25%               that mean point 8 and 6 close to each other by 25% 
    4     Loc  6,10        20%
    5     LOC 11,10        10 %
    6     LOC 15,14         0%
    In addition, we can see the relationship between these points as follows
    -points 1,2,3 in a group why because close 1.2 and 1.3 a relationship which means 1.3 also hid the relationship.
    -Points 6,8,10,11 in the second group there are the relationships between them.
    - but no relationship between 1, 2 or 3 with any point of 6,8,9,10,11
    -as well as no relationship between 15, 14, which means 14 in the third group and 15 in the fourth group.


    Whati need?

    to group the points that growing a relationship according to the percentage value


    The most important part is to group the points. For EXAMPLE, the query below, the gropuing is not correct.

    I have the following table with data
    drop table temp_value; 
    create table temp_value(id number(10),location varchar2(20), percentage number(9)); 
     
     
    insert into temp_value values  (1,'LOC 1,2',10); 
    insert into  temp_value values (2,'LOC 1,3',0); 
    insert into  temp_value values (3,'LOC 1,4',0); 
    insert into  temp_value values (4,'LOC 1,5',0); 
    insert into  temp_value values (5,'LOC 1,6',0); 
    insert into  temp_value values (6,'LOC 2,3',0); 
    insert into  temp_value  values(7,'LOC 2,4',0); 
    insert into  temp_value values (8,'LOC 2,5',30); 
    insert into  temp_value values (9,'LOC 2,6',0); 
    insert into  temp_value values (10,'LOC 3,4',0); 
    insert into  temp_value values (11,'LOC 3,5',0); 
    insert into  temp_value values (12,'LOC 4,5',40); 
    insert into  temp_value values (13,'LOC 4,6',0); 
    insert into  temp_value values (14,'LOC 6,7',40);
    insert into  temp_value values (15,'LOC 7,2',0);
    insert into  temp_value values (16,'LOC 8,2',60);
    insert into  temp_value values (17,'LOC 8,3',0);
    insert into  temp_value values (18,'LOC 3,1',0);
    insert into  temp_value values (19,'LOC 9,6',30);
    insert into  temp_value values (20,'LOC 11,2',0);
    insert into  temp_value values (22,'LOC 12,3',10);
    insert into  temp_value values (23,'LOC 19,3',0);
    insert into  temp_value values (24,'LOC 17,3',0);
    insert into  temp_value values (24,'LOC 20,3',0);
    When I used this query, the results is not correct

     with t as
        (select percentage,loc1,loc2,sum(case when percentage = 0 then 1
                           when loc1 in (l1,l2) then 0
                       when loc2 in (l1,l2) then 0
                       when l1 is null and l2 is null then 0
                       else 1
                  end) over(order by rn) sm
        from (     select id,location,percentage,
                           regexp_substr(location,'\d+',1,1) LOC1,
                          regexp_substr(location,'\d+',1,2)  LOC2,
                         lag(regexp_substr(location,'\d+',1,1))
                          over(order by percentage desc) l1,
                          lag(regexp_substr(location,'\d+',1,2))
                          over(order by percentage desc) l2,
                  row_number() over(order by percentage desc) rn
          from temp_value
          order by percentage desc
            )
      )
       select loc,min(sm)+1 grp
         from(
           select loc,rownum rn,sm
           from(
           select percentage,decode(rn,1,loc1,loc2) loc,sm
           from t a,
                (select 1 rn from dual union all
                 select 2 from dual ) b
           order by percentage desc,decode(rn,1,loc1,loc2) asc
          )
       )
        group by loc
       order by min(sm),min(rn);
    results


    SQL > /.
    LOC                         GRP
    -------------------- ----------
    2                             1
    8                             1
    6                             2
    7                             2
    4                             3
    5                             3
    9                             4
    1                             5
    12                            6
    3                             6
    11                           13
    
    LOC                         GRP
    -------------------- ----------
    19                           14
    17                           15
    20                           22
    
    14 rows selected.
    SQL >


    but the just is
    Location        group No
    2                  1
    8                  1
    4                  1
    5                  1
    1                  1
    6                  2
    7                  2
    9                  2
    12                 3
    3                  3
    19                 4
    17                 5
    20                 6
    Thanks in advance.

    Published by: Isabelle on November 30, 2012 03:07

    OK, I thought an it once again and found out how to include with any such percentage points.
    In your example expected output you missed the 11 that's why we got 7 groups now.
    The order of the Group 2 and 3 is ambiguous, because the highest percentage of these groups is the same.

    with connects as (
    select distinct
     loc1
    ,loc2
    ,connect_by_root(loc1) grp
    ,percentage per
    from
    temp_value
    --start with
    --percentage != 0
    connect by nocycle
    (prior loc2 = loc1
    or
    prior loc1 = loc2
    or
    prior loc1 = loc1
    or
    prior loc2 = loc2)
    and
    percentage != 0
    and
    prior percentage != 0
    )
    
    select
     loc
    ,dense_rank() over (order by decode(per,0,1,0), grp) grp
    from (
    select
     loc
    ,max(grp) keep (dense_rank first order by per desc, grp) grp
    ,max(per) keep (dense_rank last order by per nulls first) per
    from (
    select
     loc1 loc
    ,grp
    ,per
    from connects
    union
    select
     loc2
    ,grp
    ,per
    from connects
    )
    group by
    loc )
    order by 2,per desc,1
    
    LOC     GRP
    2     1
    8     1
    4     1
    5     1
    1     1
    12     2
    3     2
    6     3
    7     3
    9     3
    11     4
    17     5
    19     6
    20     7
    

    Think we are done now ;-)
    Edited by: chris227 at 30.11.2012 16:46

    Edited by: chris227 at 30.11.2012 17:12
    order corrected

    Edited by: chris227 at 30.11.2012 17:15
    simplification, no need to rank in linking subquery

    Edited by: chris227 at 30.11.2012 17:26

  • How can I change this query? to get the results I want

    This query
     
    select
     SHRTGPA_pidm, 
    SZSTCLA_TERM_CODE,
    SHRTGPA_GPA_HOURS
     from  szstcla,SHRTGPA
     where szstcla_pidm = 74246
    and SHRTGPA_pidm = szstcla_pidm 
    and SZSTCLA_TERM_CODE <> SZSTCLA_TERM_CODE_MATRIC
    and SHRTGPA_TERM_CODE = SZSTCLA_TERM_CODE
    Returns
    74246     201020     4
    74246     201120     4
    74246     201110     4
    74246     201210     4
    74246     201220     4
    I want to group the query will return
    74246 201020     4
    74246 201120     8
    74246 201110     12
    74246 201210     16
    201220 20

    Two copies

    Published by: Frank Kulash, November 8, 2012 11:32

Maybe you are looking for

  • M30X 129: problem of battery or power management road

    Hi all I had a lot of trouble with my laptop recently; When using just the battery I can watch it unloads and a minute, he can read about 30% or more, depending on the laptop will be beep like crazy and then go into hibernation. I put it to Hibernate

  • Scan to e-mail, works not while on the other are. HP Color 200 M251nw

    I work for a large company, with thousands of hp printers.  We have very strict policies for printers.  Our network is configured to accept internal emails on port 25 to our using unencrypted SMTP address or user names, only IP. Last week I installed

  • How to set up printing wireless of my lide 120 to a brother hl-3170cdw

    Seems like scanners are the poor relative in the community, I do not see any place to ask this question other than "General Discussion." To expand on my subject line... nothing ignite communication between my Windows laptop 8.1 and my printer Brother

  • Error 80070005 on update driver

    I recently updated my driver for 6150 is nForce 430; There is an optional update that I tried to install it but I get the error code 80070005 (there is no additional number is this error code) I must say that I have Winndows Vista Home premium 64-bit

  • Needed support replacing WRT610N

    I have more product Linksys here and tell everyone to use Linksys for years I have many items in cabnet do not use. Well I got a $$$ WRT610N and totally had problems and it all boils down internal the software of linksys. "I run all these on my home