Query logic

TABLE A

========

Q_ID

1

2

3

4

5

6

TABLE B

=======

Q_ID Q_NAME

1 INS

2 LED

UPD 3

INS 4

5 LED

UPD 66

44 INS

55 LED

UPD 66

Result

======

Need to update the Table A Qid WITH Max Q_ID in table B.

Basically to maximize A QID of table based on the Table B Q_ID

TABLE A

========

Q_ID

44

55

66

44

55

66

update from table_a a

set q_id =)

with t as)

Select b.q_id,

Max (b.Q_ID) on max_q_id (b.q_name score)

of table_b b

When status = 'A '.

)

Select t.max_q_id

t

where t.q_id = a.q_id

)

where q_id in)

Select b.q_id

of table_b b

When status = 'A '.

)

/

SY.

Tags: Database

Similar Questions

  • Reg: query logic.

    Hi Experts,

    I have a requirement of query which am not able to achieve correctly-

    2 tables like this:

    WITH t1 AS)

    SELECT the 1 id, cm_dt of TO_DATE('04.04.2014','dd.mm.yyyy') OF double UNION ALL

    SELECT 2 id, TO_DATE('01.11.2013','dd.mm.yyyy') cm_dt FROM dual UNION ALL

    SELECT 2 id, TO_DATE('29.09.2013','dd.mm.yyyy') cm_dt FROM dual UNION ALL

    Select 3 id, to_date('17.05.2013','dd.mm.yyyy') cm_dt of the double

    )

    t2 AS)

    SELECT the 1 id, TO_DATE('04.05.2014','dd.mm.yyyy') fh_dt, ov 'b', 'a' double UNION ALL nv

    SELECT 1 id, TO_DATE('21.05.2014','dd.mm.yyyy'), 'y', 'x' FROM dual UNION ALL

    SELECT 2 id, TO_DATE('01.10.2013','dd.mm.yyyy'), 'r', 'e' FROM dual UNION ALL

    SELECT the id 2, TO_DATE('15.10.2013','dd.mm.yyyy'), 'x', 'q' FROM dual UNION ALL

    SELECT 2 id, TO_DATE('29.03.2014','dd.mm.yyyy'), 'f', 'b' FROM dual

    )

    My requirement is :


    Check - 1

    For each ID of "t1", need to check in 't2' If a record exists. If the 'id' doesn't exist, you need to select another table called "table_y". Here, T1.ID = 3

    Check the box - 2

    If the ID exists, for each 'id' and T1. CM_DT - is any record in T2 where (T1.id = T2.id and T1. CM_DT < T2. Take FH_DT) then the OV (old value) of the last record.

    Otherwise, if no such folder i.e. all have T1. CM_DT > T2. FH_DT - take NV (new value) of the first record.

    Expected results :

    1 04.04.2014 b

    2 01.11.2013 q

    2 29.09.2013 r

    3 17.05.2013 < some value from another table >

    My try and try again...

    WITH t1 AS)

    SELECT the 1 id, cm_dt of TO_DATE('04.04.2014','dd.mm.yyyy') OF double UNION ALL

    SELECT 2 id, TO_DATE('01.11.2013','dd.mm.yyyy') cm_dt FROM dual UNION ALL

    SELECT 2 id, TO_DATE('29.09.2013','dd.mm.yyyy') cm_dt FROM dual UNION ALL

    Select 3 id, to_date('17.05.2013','dd.mm.yyyy') cm_dt of the double

    )

    t2 AS)

    SELECT the 1 id, TO_DATE('04.05.2014','dd.mm.yyyy') fh_dt, ov 'b', 'a' double UNION ALL nv

    SELECT 1 id, TO_DATE('21.05.2014','dd.mm.yyyy'), 'y', 'x' FROM dual UNION ALL

    SELECT 2 id, TO_DATE('01.10.2013','dd.mm.yyyy'), 'r', 'e' FROM dual UNION ALL

    SELECT the id 2, TO_DATE('15.10.2013','dd.mm.yyyy'), 'x', 'q' FROM dual UNION ALL

    SELECT 2 id, TO_DATE('29.03.2014','dd.mm.yyyy'), 'f', 'b' FROM dual

    )

    ------

    x AS

    (SELECT

    T1.ID, t1.cm_dt, t2.fh_dt, nv, ov,

    ROW_NUMBER() over (PARTITION BY t2.id ORDER BY fh_dt asc) Clotilde,

    MIN (T2.fh_dt) on min_dt (PARTITION BY t2.id),

    Max (T2.fh_dt) on max_dt (PARTITION BY t2.id)

    T1 LEFT JOIN t2 ON (t1.id = t2.id)

    )

    --------

    SELECT distinct

    x.ID, x.cm_dt, x.fh_dt,

    (CASE WHEN cm_dt < = min_dt THEN)

    (SELECT t2.ov FROM t2 WHERE t2.id = AND t2.fh_dt x.id = x.min_dt)

    ON THE OTHER

    (SELECT t2.ov FROM t2 WHERE t2.id = AND t2.fh_dt x.id = x.min_dt)

    Chk1 END)

    X

    ORDER BY id, cm_dt

    Please give some guidance. Help much appreciated.

    -Nordine

    (on Oracle 11.2.0.3.0)

    I think it's something like what you are looking for (if you had provided details of the 'other' table you want to get the values of, then I could have tried integrate this logic in this query, rather than do something using the double table!):

    WITH t1 AS (SELECT 1 id, TO_DATE('04.04.2014','dd.mm.yyyy') cm_dt FROM dual UNION ALL
                SELECT 2 id, TO_DATE('01.11.2013','dd.mm.yyyy') cm_dt FROM dual UNION ALL
                SELECT 2 id, TO_DATE('29.09.2013','dd.mm.yyyy') cm_dt FROM dual UNION ALL
                select 3 id, to_date('17.05.2013','dd.mm.yyyy') cm_dt from dual),
         t2 AS (SELECT 1 id, TO_DATE('04.05.2014','dd.mm.yyyy') fh_dt, 'a' nv,'b' ov FROM dual UNION ALL
                SELECT 1 id, TO_DATE('21.05.2014','dd.mm.yyyy') , 'x','y' FROM dual UNION ALL
                SELECT 2 id, TO_DATE('01.10.2013','dd.mm.yyyy') , 'e','r'  FROM dual UNION ALL
                SELECT 2 id, TO_DATE('15.10.2013','dd.mm.yyyy') , 'q','x' FROM dual UNION ALL
                SELECT 2 id, TO_DATE('29.03.2014','dd.mm.yyyy') , 'b','f' FROM dual),
        res as (select t1.id,
                       t1.cm_dt,
                       t2.fh_dt,
                       t2.nv,
                       t2.ov,
                       min(case when t2.fh_dt > t1.cm_dt then t2.fh_dt end) over (partition by t2.id, t1.cm_dt) min_t2_fh_dt_past_cm_dt,
                       max(case when t2.fh_dt < t1.cm_dt then t2.fh_dt end) over (partition by t2.id, t1.cm_dt) max_t2_fh_dt_pre_cm_dt,
                       coalesce(max(case when t2.fh_dt < t1.cm_dt then t2.fh_dt end) over (partition by t2.id, t1.cm_dt),
                                min(case when t2.fh_dt > t1.cm_dt then t2.fh_dt end) over (partition by t2.id, t1.cm_dt)) comparison_dt
                from   t1,
                       t2
                where  t1.id = t2.id (+))
    select id,
           cm_dt,
           case when comparison_dt is null then (select 'zzz' from dual)
                when comparison_dt > cm_dt then ov
                when comparison_dt < cm_dt then nv
           end replacement_value
    from   res
    where  comparison_dt is null
    or     comparison_dt = fh_dt;
    
            ID CM_DT      REPLACEMENT_VALUE
    ---------- ---------- --------------------------------
             1 04/04/2014 b
             2 29/09/2013 r
             2 01/11/2013 q
             3 17/05/2013 zzz
    
  • Not able to understand the query logic

    Hello

    Please, help me to understand the logic of the query below.


    I have an emp table having 2 emp_id and mgr_id column.

    data from table.

    emp_id mgr_id
    1
    2 2
    3 3
    4 5
    5
    6 5
    6 of 7
    6 of 8
    9 7

    I run the query below...

    Select * from emp where emp_id not in (select mgr_id from emp);

    Query giving no line.

    I need to know the logic of the query.

    Thank you
    HIMS

    Because there are some NULL values in the column so 'IN' mgr_id would fail to compare values such as 1,4,8,9 which are not there in mgr_id with these NULL values.

    Use NVL like below to get the desired results...

     
    
    with t as
    ( select 1 emp_id,  null mgr_id from dual union all
      select 2,2 from dual union all
      select 3,3 from dual union all
      select 4,5 from dual union all
      select 5,null  from dual union all
      select 6,5  from dual union all
      select 7,6  from dual union all
      select 8,6  from dual union all
      select 9,7  from dual
    )
    SELECT *
      FROM t
     WHERE emp_id NOT IN (SELECT nvl(mgr_id,0) from t );
    

    Concerning
    Arun

  • LESS a matter of query logic

    I need to archive records based on multiple rules. I got this resolved with the exception of a rule. Here's the scenario.

    With the help of Oracle database 11 g 2.

    create table tab
    (   id                  number  primary key
      , status_id           number
      , supplier_id         number
      , supplier_type_id    number
      , dt_eff_from         date
    )
    /
    
    insert into tab
    values(10,2,234000,84,sysdate-10)
    /
    
    insert into tab
    values(15,2,234000,84,sysdate-15)
    /
    
    
    insert into tab
    values(20,1,234000,84,sysdate-20)
    /
    
    insert into tab
    values(30,1,234000,84,sysdate-30)
    /
    
    insert into tab
    values(40,2,234000,84,sysdate-40)
    /
    
    insert into tab
    values(50,1,234000,84,sysdate-50)
    /
    
    insert into tab
    values(60,2,999999,25,sysdate-10)
    /
    
    insert into tab
    values(70,2,999999,25,sysdate-15)
    /
    
    insert into tab
    values(80,2,999999,25,sysdate-20)
    /
    
    insert into tab
    values(90,1,777777,33,sysdate-10)
    /
    
    
    insert into tab
    values(100,2,777777,33,sysdate-20)
    /
    
    
    insert into tab
    values(110,2,777777,33,sysdate-30)
    /
    
    commit ;
    
            ID  STATUS_ID SUPPLIER_ID SUPPLIER_TYPE_ID DT_EFF_FROM
    ---------- ---------- ----------- ---------------- --------------------
            10          2      234000               84 17-APR-2015 
            15          2      234000               84 12-APR-2015 
            20          1      234000               84 07-APR-2015 
            30          1      234000               84 28-MAR-2015 
            40          2      234000               84 18-MAR-2015
            50          1      234000               84 08-MAR-2015 
            60          2      999999               25 17-APR-2015 
            70          2      999999               25 12-APR-2015 
            80          2      999999               25 07-APR-2015 
            90          1      777777               33 17-APR-2015 
           100          2      777777               33 07-APR-2015
           110          2      777777               33 28-MAR-2015 
    

    Batch: 1 approved =, 2 = rejected

    Records are analyzed on the basis supplier_id + supplier_type_id

    Rule 1: When batch = 2 dt_eff_from is greater than dt_eff_from for records with batch = 1

    We need to keep (not archives) the most recent registration rejected

    We just need to keep the most recent rejected (batch = 2) record when there is none

    approved (batch = 1) record.

    When batch = 1 has a greater dt_eff_from than the batch = 2 records, then we can archive

    all the batch = 2 files, but we have to keep (not archives) the batch = 1 sheet

    select id from tab -- initially all records are candidates to be archived
    MINUS
    select id from tab
    where <logic for rule 1 goes here>
    
    
    ID 
    ---
    15
    20
    30
    40
    50
    70
    80
    100
    110
    
    

    Based on rule 1, all the files get archived except ID = 10 ID = 60 and ID = 90

    Any thoughts?

    SELECT * FROM)

    SELECT T.*

    MAX (batch) OVER (PARTITION BY SUPPLIER_ID, SUPPLIER_TYPE_ID) last_status_id KEEP(DENSE_RANK LAST ORDER BY DT_EFF_FROM,ID)

    ROW_NUMBER() over (PARTITION OF SUPPLIER_ID, SUPPLIER_TYPE_ID ORDER BY DT_EFF_FROM DESC, ID DESC) rn

    T tab

    )

    WHERE (last_status_id = 2 AND rn = 1) OR (last_status_id = 1 AND batch = 1);

  • SQL Query logic-based price calculation

    Hi Experts,

    Here are my records in the table,
    SHIPMENT_ID     SHIP_PRICE     SHIP_ADDT_PRICE     PRODUCT_ID     PRODUCT_QTY
    1000          3.95          1          12          2
    1000          0          0          17          1
    1000          6.95          2          11          4
    1001          0          0          17          1
    1001          12.95          1          12          2
    1001          12.95          2          11          4
    1002          0          0          17          1
    1002          20.95          1          12          2
    1002          20.95          2          11          4
    For each consignment:
    I need to get maximum shipprice (no need to examine the product and quantity).
    then to manipulate as follows existing such as specimen
    SHIPMENT_ID     SHIP_PRICE     
    1000          (1*6.95)+(4-1*2)     -- Maximum Price Calculation for particular shipment
    1000          (1*0)               -- Other product calculation for same shipment
    1000          (4*2)               -- Other product calculation for same shipment
    1001          (1*12.95)+(4-1*2)     -- Maximum Price Calculation for particular shipment
    1001          (1*0)               -- Other product calculation for same shipment
    1001          (4*2)               -- Other product calculation for same shipment
    1002          (1*20.95)+(4-1*2)     -- Maximum Price Calculation for particular shipment
    1002          (1*0)               -- Other product calculation for same shipment
    1002          (4*2)               -- Other product calculation for same shipment
    Max Shipprice line for each shipment
    (SHIP_PRICE) + (remaining PRODUCT_QTY if PRODUCT_QTY > 1 + SHIP_ADDT_PRICE)

    Other than Max shipprice for each shipment
    (PRODUCT_QTY + SHIP_ADDT_PRICE)

    I am struggling here with logic how to proceed, any suggestions in connection with that?

    Thank you
    with ship as (
    select 1000 SHIPMENT_ID,3.95 SHIP_PRICE,1 SHIP_ADDT_PRICE,12 PRODUCT_ID,2  PRODUCT_QTY from dual union all
    select 1000,            0,              0,                17,           1 from dual union all
    select 1000,            6.95,            2,                 11 ,           4 from dual union all
    select 1001,             0,               0,                 17,            1 from dual union all
    select 1001,             12.95 ,          1 ,                12,            2 from dual union all
    select 1001,             12.95,           2 ,                11,            4 from dual union all
    select 1002,             0 ,              0 ,                17,            1 from dual union all
    select 1002,             20.95 ,          1,                 12 ,           2 from dual union all
    select 1002,             20.95,           2,                 11,            4 from dual )
    
    select shipment_id
          ,
          case when (max_ship = ship_price and max_qty = product_qty) then
          (1* max_ship)+ ((max_qty - min_qty )* max_ship_aat_price)
          else
           (s.PRODUCT_QTY * s.SHIP_ADDT_PRICE)
          end max_price
    from
    (select max(ship_price) over (partition by shipment_id order by shipment_id desc) max_ship,
            max(product_qty) over (partition by shipment_id order by shipment_id desc) max_qty,
            min(product_qty) over (partition by shipment_id order by shipment_id desc) min_qty,
            max(ship_addt_price) over (partition by shipment_id order by shipment_id desc) max_ship_aat_price,
            s.shipment_id,s.SHIP_PRICE,s.SHIP_ADDT_PRICE,s.PRODUCT_ID,s.PRODUCT_QTY
        from ship s)s
    order by 1,2 desc   
    

    What do you expect?

  • Need help with the query logic

    I have 2 tables.

    Table Tb1:
    THE PLAN_ID PARENT_PLAN_ID ARGUMENT
    Value null P1
    P1 P2
    P3 P2
    P4 P1

    Table tb2:
    THE PLAN_ID ACTIVITY_ID PICKING ARGUMENT
    P2 A1 5000
    P2 A2 5000
    P2 A3 5000
    P3 A1 10000
    P3 A2 10000
    P4 A1 4000


    I need to find the sum of the value of reduction of the workforce for each PLAN_ID which is parent root. In the case above, this is the argument PLAN_ID P1 (WHERE PARENT_PLAN_ID IS NULL). Please note that the value of the downsizing is repeated in the tb2 table. For example, the PLAN_ID P2 argument, the value of the downsizing is 5000 and not 5000 + 5000 + 5000.

    The result of the output for the above data must be
    THE ARGUMENT PLAN_ID DRAWDOWN
    P1 19000

    Published by: user10566312 on October 30, 2012 12:30
    with t as
    (   select t1.plan_id,t1.parent_plan_id,max(t2.DRAWDOWN) DRAWDOWN
        from tb1 t1,tb2 t2
        where t1.plan_id = t2.plan_id(+)
        group by  t1.plan_id,t1.parent_plan_id
    )
    select plan_id,sum(drawdown) drawdown
    from(
          select  connect_by_root plan_id as plan_id,
                  nvl(DRAWDOWN,0) DRAWDOWN
          from t
          start with parent_plan_id is null
          connect by parent_plan_id  = prior plan_id
        )
    group by plan_id  ; 
    
    PLAN_ID DRAWDOWN
    ------- --------
    P1         19000 
    

    Published by: JAC on October 30, 2012 13:23

  • Is what logic this correct?


    Hi gurus

    I need your advice to the monitoring scenario.

    Creating the table

    CREATE TABLE GROUP_TEST
    (
    GROUP_ID NUMBER (5));

    CREATE TABLE GROUP_TEST_DTL
    (
    GROUP_ID NUMBER (5),
    GROUP_TEST_DTL_ID NUMBER (5),
    DATE OF EFFECTIVE_DATE
    );

    Insertion

    INSERT INTO GROUP_TEST
    (
    100 SELECT FROM DUAL
    );

    INSERT INTO GROUP_TEST_DTL
    (
    SELECT GROUP_ID, 100, 1, 1 JANUARY 2014 "OF THE DOUBLE
    UNION ALL
    SELECT GROUP_ID 100, 2, 2 JANUARY 2014 ' FROM DUAL
    UNION ALL
    SELECT GROUP_ID, 100, 3, 3 JANUARY 2014 ' FROM DUAL
    );
    COMMIT;

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

    Query on table

    SELECT * FROM GROUP_TEST_DTL;

    The query result

    group_id group_test_dtl_id effective_date

    100                     1                             01-JAN-14

    100                     2                             02-JAN-14

    100                     3                             03-JAN-14

    Now I need the following output

    Output

    Group_id Min_effective_dt

    100 1 JANUARY 14

    I use the below 2 queries for this result.

    1st request

    SELECT GROUP_ID, MIN (EFFECTIVE_DATE)

    OF GROUP_TEST_DTL

    GROUP BY GROUP_ID;

    2nd request

    SEPARATE GROUP_ID, SELECT (SELECT MIN (EFFECTIVE_DATE) OF GROUP_TEST_DTL A

    WHERE A.GROUP_ID = B.GROUP_ID) MIN_DT

    OF GROUP_TEST_DTL B;

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

    My question is one that is more efficient query (I think 1st)?

    My other question is that I have my package consist of approximately 5 000 lines of code and I'm using plain effective_date in all my sliders as (select effective_date from group_test_dtl) and now my requirement has changed and the need to get/make min (effective_date) instead of plain effective_date, but in order to get the Effective_date Min, I have to use text-to-speech function Min as my 1st query and then we must consolidate all columns that exist in the select statement, but I have to change all my queries sliders, so I came to the idea of the 2nd query logic.

    Now I wonder, I did wrong and use query of query not 2nd logic 1.

    Please guide me what I need to keep my 1st query logic or the 2nd. Really appreciate your answers. Thank you

    I can tell only you should test it yourself or to consider their use quite heavy within this forum (check the difference tabibitosan/fixed, the function names...?.)

    Analytical functions Analytic Functions - the next best thing after selection according to Tom Kyte - are usually extremely efficient, despite the selection somehow inevitable within the selection because they run last (just before the order by clause).

    Concerning

    Etbin

  • Query Sub behavior strange when using Expressions regular Oracle

    I met a strange "inconsistent" when you use an Expression regular Oracle with subqueries in SQL. Here are some details on how to reproduce what I have observed. We managed to find a solution to this "problem" using a database index; I'm writing this case hoping to better understand why Regular Expressions Oracle do not seem to work in the same way that the older, standard functions integrated as INSTR, SUBSTR, AS, etc..

    Environment settings:
    This test has been done using Oracle XE (GR 11, 2) on 32-bit Windows operating system. For my test, I used the HR schema (which is delivered pre-completed with the installation of this product) with some modifications of my own.

    Make the Test objects:
    create table hr.emp_test as
    select to_char(employee_id) as emp_id_char, employee_id as emp_id,
    first_name, last_name, hire_date
    from hr.employees;
    To illustrate my test, I inserted mixed alphanumeric values and a null value in the column of my emp_id_char for good measure:
    insert into hr.emp_test (emp_id_char, first_name, last_name, hire_date)
    values ('XKCD123','TEST','LASTNAME',sysdate);
    insert into hr.emp_test (emp_id_char, first_name, last_name, hire_date)
    values (null,'TEST1','LASTNAME2',sysdate);
    commit;
    * (1) this request fails once a nonnumeric value is inserted into the emp_test table.*
    with
       sub1 as ( select to_number(emp_id_char) as emp_id, first_name, last_name, hire_date 
          from hr.emp_test  )
    select * from sub1
    where emp_id between 100 and 110
    * (2) this query works OK.*
    with
       sub1 as ( select to_number(emp_id_char) as emp_id, first_name, last_name, hire_date 
          from hr.emp_test where emp_id_char not like 'X%'  )
    select * from sub1
    where emp_id between 100 and 110
    * (3) this query works OK.*
    with
       sub1 as ( select to_number(emp_id_char) as emp_id, first_name, last_name, hire_date 
          from hr.emp_test where instr(emp_id_char,'X',1) = 0 )
    select * from sub1
    where emp_id between 100 and 110
    * (4) this query Fails.*
    with
       sub1 as ( select emp_id_char, first_name, last_name, hire_date
                   from hr.emp_test
                  where regexp_instr(emp_id_char, '[^[:digit:]]') = 0 ),
       sub2 as ( select to_number(emp_id_char) as emp_id, first_name, last_name, hire_date
                  from sub1 )
    select *
    from sub2
    where emp_id between 100 and 110
    
    ERROR:
    ORA-01722: invalid number
    * (5) even down the results of the rational expression of 3rd under the query in sequential processing order also FAILS *.
    with
       sub1 as ( select emp_id_char, first_name, last_name, hire_date
                   from hr.emp_test
                  where regexp_instr(emp_id_char, '[^[:digit:]]') = 0 ),
       sub2 as ( select to_number(emp_id_char) as emp_id, first_name, last_name, hire_date
                  from sub1 ),
       sub3 as ( select emp_id, first_name, last_name, hire_date from sub2 )
    select *
    from sub3
    where emp_id between 100 and 110
    
    ERROR:
    ORA-01722: invalid number
    * (6) that it does not like previous query as well *.
    with
       sub1 as ( select to_number(emp_id_char) as emp_id, first_name, last_name, hire_date,
           regexp_instr(emp_id_char, '[^[:digit:]]') as reg_x 
           from hr.emp_test 
           where reg_x = 0),
       sub2 as ( select emp_id, first_name, last_name, hire_date, reg_x
          from sub1 where reg_x = 0 )
    select * from sub2
    where emp_id between 100 and 110
    
    ERROR:
    ORA-00904: "REG_X": invalid identifier
    Our Solution...
    Add a hint to the query of sup that 'hiding' the result of sub1 in memory. That did the trick. This suspicion resembles only viable workaround for this behavior. Other old built-in functions (INSTR, AS, etc.) an they were automatically following the execution plan (results of cache memory) that he had to use a 'hint' to force with the function of the regular expression.

    The conclusion, which is what I would like to help to understand or explain is that:
    If you create a series of queries/sup queries or inline views, values depend on "regular expression" type built-in sql functions do not seem to stick or maintain when implemented in complex query logic.

    Any idea is appreciated!
    Thank you!

    Published by: 870810 on July 6, 2011 15:47

    870810 wrote:
    I met a strange "inconsistent" when you use an Expression regular Oracle with subqueries in SQL.

    This is the expected behavior and has nothing to do with regular expressions - much less directly (I'll explain later). Main rule: there is no WHERE clause predicate order. Even if you use views, views online, subquery factoring, optimizer etc. can extend your display, display online, a subquery factoring. And it's Optimizer who decides the order of execution predicate unless you use the ORDERED_PREDICATES key. Now, I can explain. Regular expressions are powerful enough but also everywhere in life to pay for it with the higher cost of execution. That's why optimizer decides to apply emp_id between 100 and 110 first and regexp_instr (emp_id_char, "[^ [: digit:]]'") = 0 later.

    explain plan for
    with
    sub1 as ( select emp_id_char, first_name, last_name, hire_date
    from emp_test
    where regexp_instr(emp_id_char, '[^[:digit:]]') = 0 ),
    sub2 as ( select to_number(emp_id_char) as emp_id, first_name, last_name, hire_date
    from sub1 )
    select *
    from sub2
    where emp_id between 100 and 110;
    
    SQL> @?\rdbms\admin\utlxpls
    
    PLAN_TABLE_OUTPUT
    -------------------------------------------------------------------------------------
    Plan hash value: 3124080142
    
    ------------------------------------------------------------------------------
    | Id  | Operation         | Name     | Rows  | Bytes | Cost (%CPU)| Time     |
    ------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT  |          |     1 |    57 |     3   (0)| 00:00:01 |
    |*  1 |  TABLE ACCESS FULL| EMP_TEST |     1 |    57 |     3   (0)| 00:00:01 |
    ------------------------------------------------------------------------------
    
    Predicate Information (identified by operation id):
    ---------------------------------------------------
    
    PLAN_TABLE_OUTPUT
    -------------------------------------------------------------------------------------
    
       1 - filter(TO_NUMBER("EMP_ID_CHAR")>=100 AND
                  TO_NUMBER("EMP_ID_CHAR")<=110 AND  REGEXP_INSTR
                  ("EMP_ID_CHAR",'[^[:digit:]]')=0)
    
    15 rows selected.
    
    SQL> 
    

    As you can see, optimizer uses a FULL SCAN to read data from the table and apply emp_id between 100 and 110 which translates TO_NUMBER ("EMP_ID_CHAR") > = 100 AND TO_NUMBER ("EMP_ID_CHAR")<=110. and="" obviously="" it="" fails="" trying="" to="" convert="" xkcd123="" to="" number.="" now="" cost="" of="" instr(emp_id_char,'x',1)="0" is="" lower="" and="" optimizer="" decides="" to="" apply="" instr="" first.="" therefore="" xkcd123="" is="" filtered="" out="" before="" to_number="" is="">

    SQL> explain plan for
      2  with
      3  sub1 as ( select emp_id_char, first_name, last_name, hire_date
      4  from emp_test
      5  where instr(emp_id_char, 'X') = 0 ),
      6  sub2 as ( select to_number(emp_id_char) as emp_id, first_name, last_name, hire_date
      7  from sub1 )
      8  select *
      9  from sub2
     10  where emp_id between 100 and 110;
    
    Explained.
    
    SQL> @?\rdbms\admin\utlxpls
    
    PLAN_TABLE_OUTPUT
    -----------------------------------------------------------------------------------------------
    Plan hash value: 3124080142
    
    ------------------------------------------------------------------------------
    | Id  | Operation         | Name     | Rows  | Bytes | Cost (%CPU)| Time     |
    ------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT  |          |     1 |    57 |     3   (0)| 00:00:01 |
    |*  1 |  TABLE ACCESS FULL| EMP_TEST |     1 |    57 |     3   (0)| 00:00:01 |
    ------------------------------------------------------------------------------
    
    Predicate Information (identified by operation id):
    ---------------------------------------------------
    
    PLAN_TABLE_OUTPUT
    -----------------------------------------------------------------------------------------------
    
       1 - filter(INSTR("EMP_ID_CHAR",'X')=0 AND
                  TO_NUMBER("EMP_ID_CHAR")>=100 AND TO_NUMBER("EMP_ID_CHAR")<=110)
    
    14 rows selected.
    
    SQL> 
    

    The bottom LINE: With the help of strings to store non-chaine (numeric in your case) is never a good idea and shows design problems.

    SY.

  • query to retrieve a next valid record

    Hi all

    I have a strange obligation to write a query/logic to achieve the following objectives. I have a table that has a column of date and a field that indicates whether it is a valid date or not

    Valid date
    01/11/2010 Y
    01/12/2010 N
    N 13/01/2010
    2010-02-17 Y
    2010-03-15 N
    04/11/2010 Y
    21/06/2010 Y
    2010-08-18 N
    2010-09-14 N
    2010-10-17 Y

    If a specific date is not valid, I need to go out the next valid date. So, something like

    Valid date of valid date
    01/11/2010 Y 01/11/2010
    01/12/2010 N 17/02/2010
    13/01/2010 N 17/02/2010
    17/02/2010 2010-02-17 Y
    15/03/2010 N 04/11/2010
    04/11/2010 04/11/2010 Y
    21/06/2010 Y 21/06/2010
    18/08/2010 N 17/10/2010
    14/09/2010 N 17/10/2010
    17/10/2010 Y 10/17/2010

    Is it possible in a single query?


    Thank you

    Birdy
    with t as (
               select to_date('11/01/2010','dd/mm/yyyy') dt,'Y' valid from dual union all
               select to_date('12/01/2010','dd/mm/yyyy'),'N' from dual union all
               select to_date('13/01/2010','dd/mm/yyyy'),'N' from dual union all
               select to_date('17/02/2010','dd/mm/yyyy'),'Y' from dual union all
               select to_date('15/03/2010','dd/mm/yyyy'),'N' from dual union all
               select to_date('11/04/2010','dd/mm/yyyy'),'Y' from dual union all
               select to_date('21/06/2010','dd/mm/yyyy'),'Y' from dual union all
               select to_date('18/08/2010','dd/mm/yyyy'),'N' from dual union all
               select to_date('14/09/2010','dd/mm/yyyy'),'N' from dual union all
               select to_date('17/10/2010','dd/mm/yyyy'),'Y' from dual
              )
    select  dt,
            valid,
            min(
                case valid
                  when 'Y' then dt
                end
               ) over(order by dt rows between current row and unbounded following) valid_dt
      from  t
      order by dt
    /
    
    DT         V VALID_DT
    ---------- - ----------
    11/01/2010 Y 11/01/2010
    12/01/2010 N 17/02/2010
    13/01/2010 N 17/02/2010
    17/02/2010 Y 17/02/2010
    15/03/2010 N 11/04/2010
    11/04/2010 Y 11/04/2010
    21/06/2010 Y 21/06/2010
    18/08/2010 N 17/10/2010
    14/09/2010 N 17/10/2010
    17/10/2010 Y 17/10/2010
    
    10 rows selected.
    
    SQL> 
    

    SY.

  • A delicate sql query

    Hello

    I have data like this...

    table tab1 has only a single column col1 and it contains nearly 3000 lines and all are numeric values.

    tab2 has 3 columns col1, col2.col3

    I want to generate a sql statement select with every four rows from tab1 values similar to the following.

    Now say in the following example, after that only three rows of row value 8 are there in tab1, then the select statement includes only these three values.

    Select * from tab1

    col1
    ------
    1
    2
    3
    4 select col1, col2 from tab2 where col3 in (1,2,3,4)
    5
    6
    7
    8 select col1, col2 from tab2 where col3 in (5,6,7,8)
    9
    10
    11 select col1, col2 from tab2 where col3 in (9,10,11)

    I tried in sql, but do what can you with case that it's lift error row subquery returns error a lot of lines. In pl/sql, it is possible, but how can I do this in sql?

    Thanks in advance.

    Can you please explain this somewhat unusual query logic

    Extract is very well documented ;)

    The"/ / text()" portion is an xpath function that extracts the entire text within the xml tags, that is.

     xmltype('uvwx').extract('//text()')  =>  uvwx
    

    HTH.

  • Reg and in 10G security architecture

    Hi Experts,


    I want small details about 10 G

    1 > first when the user generate reports of responses on the basis of which the web server will create physical motion and when he send the request to the server (DPR) BI first who sleeps it will reach and how the bi server that will convert to query logic.

    2 > when the user connects to the presentation server both username and password is correct but when enter the password it is showing the error failed to open a session may I know where I should start to clear this issue.


    Kind regards

    Published by: 963851 on October 21, 2012 07:32

    Given the bed Server catalog presentation and we see that as an area of activity and logical query is getting generated.
    The logical query is passed to the Server BI, BI server a RPD in memory and logical query is interpreted as a physical and passed query to the db server.

    --> Logical query presentation layer
    Physical layer + MDB--> physical request

    Hope this helps

  • Additional columns in the physical request

    Hello

    OBIEE 11.1.1.6

    It is a continuation from my previous post for a strange behavior of the physical transformation of query logic.

    Floor applied during

    I have the extra column during the transformation of physical SQL to SQL logic
    select Distinct 0 as c1, case when .... as c2 from ....
    0 c1 is an extra column that I get.

    Is that what anyone has experienced the same problem? any solution to overcome this problem?


    Thank you
    HESH

    Based on the default OOB query parameters go generate, usually with clause. No point to it trace the reasons until this does not affect your results.

    I'd say just ignore ;)

    Pls mark as an aid to correct/useful if

  • Noob in SQL Tuning seeking help

    I am learning SQL Tuning and operation of it. I know that the first step is probably explain the sql statement execution plan? My problem is that I do not understand what am I suppose to collect from the execution plan.
    SQL> set autotrace traceonly explain
    SQL> select * from employees A
         where A.last_employed =
         ( SELECT TO_CHAR(MAX(TO_DATE(PAY_DATE,'MMRR')),'MMYY')
           FROM PAYROLL_VW1 B
           WHERE B.SSN = A.SSN AND B.PAY_TYPE_CODE IN ('02','12','22','32')
          AND STATUS = 'ACTIVE' AND A.BRANCH=B.BRANCH
         );
    Execution Plan
    ----------------------------------------------------------
       0      SELECT STATEMENT Optimizer=ALL_ROWS (Cost=47427 Card=1 Bytes
              =180)
    
       1    0   FILTER
       2    1     TABLE ACCESS (FULL) OF 'EMPLOYEES' (TABLE) (Cost=1618 Card
              =297964 Bytes=53633520)
    
       3    1     SORT (AGGREGATE)
       4    3       VIEW OF 'PAYROLL_VW1' (VIEW) (Cost=2290 Card=2 Bytes=
              46)
    
       5    4         SORT (UNIQUE) (Cost=2290 Card=2 Bytes=217)
       6    5           UNION-ALL
       7    6             FILTER
       8    7               TABLE ACCESS (FULL) OF 'PAYROLL2012' (TABLE) (C
              ost=2284 Card=1 Bytes=105)
    
       9    6             FILTER
      10    9               TABLE ACCESS (BY INDEX ROWID) OF 'PAYROLL2011'
              (TABLE) (Cost=4 Card=1 Bytes=112)
    
      11   10                 INDEX (RANGE SCAN) OF 'PAYROLL2011_IDX1' (IND
              EX) (Cost=3 Card=29)
    Then what should I do with the execution plan? What else should I check on it? How to grant the request?

    Edited by: apex_disco may 1, 2012 05:57

    I don't know how the licenses work. There is a difference between EM and Grid Control. I think that if you have just one instance, Enterprise Manager is installed, and if nothing is free I think it would be. Now, how much diagnosis is free? Who knows.

    But it may not be important. If you simply play "learning SQL Tuning" say, at home on a Linux machine, you can install anything you want and play with him.

    If adding an accelerated things index (in your re-written query logic now) then you must have had a considerable amount of documents in one of these tables not indexed. If you look at the plan to explain it now, it will probably show the indexes used, fewer files searched, less costly, etc as the other plan.

    But maybe I missed your point of departure. You want to learn how to set up queries in general or you try to resolve this particular application? I took the first for my previous answers.

    As for your results, your statistics say at least high level info. Becomes coherent and physical readings went down WAY.

    Initial statistics
    ----------------------------------------------------------
    0 recursive calls
    0 db block Gets
    1172490142 compatible Gets
    4914 physical reads
    Redo 191496 size

    2340 bytes sent via SQL * Net to client
    329 bytes received via SQL * Net from client
    1 SQL * Net back and forth to and from the client
    115881 sorts (memory)
    0 sorts (disk)
    0 rows processed

    on these tables and below are my stats. I think this is a major improvement. Elapsed time was reduced significantly to 00:00:00.65

    To listen statistics
    ----------------------------------------------------------
    352 recursive calls
    0 db block Gets
    31498 compatible Gets
    1101 physical reads
    0 redo size

    2340 bytes sent via SQL * Net to client
    329 bytes received via SQL * Net from client
    1 SQL * Net back and forth to and from the client
    13 sorts (memory)
    0 sorts (disk)
    0 rows processed

    Published by: gaffe on May 1, 2012 13:18

  • Algorithm of Luhn in SQL

    Hello

    I'm writing the Luhn-10 algorithm in the SQL (Oracle 10 g). I tried it with reference query below, but it does not work for me. I want to know all of the values where the check digit is 0.

    SELECT mod (ROUND (val + 5,-1) - val, 10)
    DE)
    SELECT SUM (CASE WHEN MOD (val2, 2) = 1 THEN)
    BOX WHEN val * 2 > 10 THEN (val * 2) - 9
    Else Val * 2 END
    ELSE VAL END) val
    DE)
    SELECT TO_NUMBER (SUBSTR ('378282246310005 ', rownum, 1)) val, val2 rownum
    of the double
    CONNECT BY LEVEL < = LENGTH('378282246310005')
    order by 2 desc));

    Note:-I am able to do this in PL/SQL procedures and functions, however, would be very happy to do this in the SQL query

    Logical \Practices\Mod10 said that every 2nd digit on the right on the left should have doubled (or multiply by 2) and the figure has doubled if more than 10, then we need to subtract 9. Finally the sum of all the digits will be divided by 10.

    Thanks and greetings

    dbuser wrote:
    Hi JAC,

    For a number as below, it returns the value of 4500000000004314 negative, 5500000000000012 etc which are valid numbers as per the

    http://www.ee.unb.ca/cgi-bin/Tervo/Luhn.pl?N=5500000000000012

    OK, I was left right incorrectly... It takes just to correct to left?

    SQL> with t as
      2  (select level lv,to_number(substr(to_char(5500000000000012),level*-1,1)) d
      3   from dual
      4   connect by level <= length(5500000000000012) )
      5  select mod(sum(
      6         case when mod(lv,2)= 0 then
      7                case when d>= 5 then (d*2)-9
      8                     else d*2 end
      9              else d end),10) d1
     10  from t;
    
            D1
    ----------
             0
    
    SQL> with t as
      2  (select level lv,to_number(substr(to_char(1245879256),level*-1,1)) d
      3   from dual
      4   connect by level <= length(1245879256) )
      5  select mod(sum(
      6         case when mod(lv,2)= 0 then
      7                case when d>= 5 then (d*2)-9
      8                     else d*2 end
      9              else d end),10) d1
     10  from t;
    
            D1
    ----------
             9
    

    Published by: JAC on April 30, 2012 10:07

  • Question of the effectiveness of UNION

    Explain the plan on a union involving several selects a single table means the table is read only once for each selection. But is this actually happens? Are there tips to control this? Conceptually the table could be read only once, and each line is read that it could be provided with each selection, 'feed' the data for each selection as a set of pipelines. This is perhaps what is happening behind the scenes, but I don't know how to check or control that is relevant to me because the table involved is very large, but a union of several chooses is easier to write and maintain.

    This example shows two queries that provide the same result to illustrate what I mean. The table can be read once by using a single select that switches on the data. But the logic to pass values like this can get ugly pretty quick. A query of the unions allows each to be as simple as possible, but it seems to force the table to be read several times. Suggestions on how to optimize a popular union.

    This is Oracle Database 10 g Enterprise Edition Release 10.2.0.4.0.

    create table t1 (val1 val2 val3 number, number, type);

    insert into t1 (1, 21, 31, 1) values;
    insert into t1 (2, 22, 32, 1) values;
    insert into t1 (3, 23, 33, 1) values;
    insert into t1 (4, 24, 34, 2) values;
    insert into t1 (25, 35, 5, 2) values;
    insert into t1 (6, 26, 36, 2) values;
    insert into t1 (27, 37, 7, 3) values;
    insert into t1 (8, 28, 38, 3) values;
    insert into t1 (9, 29, 39, 3) values;

    explain plan for
    -a single query, logical ugly
    Select
    type deal when 1 then val1
    When 2 then val2
    When 3 then val3
    end
    COL1
    from t1;

    explain plan for
    -simple logic, but three queries
    Select val1 from t1 where type = 1 union of all the
    Select val2 from t1 where type = 2 union of all the
    Select val3 t1 where type = 3;

    user13027026 wrote:
    Explain the plan on a union involving several selects a single table means the table is read only once for each selection. But is this actually happens? Are there tips to control this? Conceptually the table could be read only once, and each line is read that it could be provided with each selection, 'feed' the data for each selection as a set of pipelines. This is perhaps what is happening behind the scenes, but I don't know how to check or control that is relevant to me because the table involved is very large, but a union of several chooses is easier to write and maintain.

    This example shows two queries that provide the same result to illustrate what I mean. The table can be read once by using a single select that switches on the data. But the logic to pass values like this can get ugly pretty quick. A query of the unions allows each to be as simple as possible, but it seems to force the table to be read several times. Suggestions on how to optimize a popular union.

    This is Oracle Database 10 g Enterprise Edition Release 10.2.0.4.0.

    create table t1 (val1 val2 val3 number, number, type);

    insert into t1 (1, 21, 31, 1) values;
    insert into t1 (2, 22, 32, 1) values;
    insert into t1 (3, 23, 33, 1) values;
    insert into t1 (4, 24, 34, 2) values;
    insert into t1 (25, 35, 5, 2) values;
    insert into t1 (6, 26, 36, 2) values;
    insert into t1 (27, 37, 7, 3) values;
    insert into t1 (8, 28, 38, 3) values;
    insert into t1 (9, 29, 39, 3) values;

    explain plan for
    -a single query, logical ugly
    Select
    type deal when 1 then val1
    When 2 then val2
    When 3 then val3
    end
    COL1
    from t1;

    explain plan for
    -simple logic, but three queries
    Select val1 from t1 where type = 1 union of all the
    Select val2 from t1 where type = 2 union of all the
    Select val3 t1 where type = 3;

    Well, the questions on the UNION, but your example has UNION ALL (they are not equivalent).

    In the latest version of the basis of the answer (for UNION ALL) is Yes using factorization of join.

    http://blogs.Oracle.com/optimizer/entry/optimizer_transformations_join_factorization

    That be answered, they technically correct answer would be to fix the data model. Having COL_1, COL_2, COL_X... usually reflects a design flaw.

Maybe you are looking for