problem of query by comparing records from the same table against each other

Hi, i'ms truggling to see where I am going wrong here. I have data from cards of following music: -.
with charts_table as (select 1 id,'oasis' product_name,0801 week, 20 week_chart_position, 20 highest_chart_position from dual
union select 2,'oasis',0802,14,14 from dual
union select 3,'oasis',0803,17,14 from dual
union select 4,'oasis',0804,29,14 from dual
union select 5,'kings of leon',0801,55,55 from dual
union select 6,'kings of leon',0802,22,22 from dual
union select 7,'kings of leon',0803,35,22 from dual
union select 8,'kings of leon',0804 , 11 , 11  from dual
union select 9,'bob dylan',0801, 88 , 88  from dual
union select 10,'bob dylan',0802,79 , 79  from dual
union select 11,'bob dylan',0803,54,54 from dual
union select 12,'bob dylan',0804,22,22 from dual
union select 13,'kid rock',0801,19,19 from dual
union select 14,'kid rock',0802,24,19 from dual
union select 15,'kid rock',0803,55,0 from dual
union select 16,'kid rock',0804,44,0 from dual)
italic , I need to identify the rogue data where the highest_position is incorrectly registered lower or 0 as there
position of the highest week was at an earlier date. (stick with me) for example in this kid of rock data initially charted at position 19. What follows
week (0802) his week_chart_position slipped to 24 and the position the highest card is correctly saved at 19 (ie the week 1). The problem
is available in week 0803 and 0804 when somehow the data was corrupted and the higest_chart_position is trying to read 0 (instead of 19). How can I write a query to identify this error?

is the best I have at the moment:-
select * from charts_table ct1 where 
nvl2(highest_chart_position,0,99999)>
(select min(week_chart_position) from charts_table ct2
where ct2.week<ct1.week
and ct1.product_name=ct2.product_name)
but it's not working. can someone help and tell where I'm wrong. Thanks a lot for the research/resolve.

Ahh, I was suggesting using the lag function to ensure that highest chart position is not fall lower than highest prior chart positions. Your concern is he reports a higher chart position that she never had. In this case I use the MIN function as an analytic function rather than as an aggregate function:

with t1 as (
  select ct.*
       , min(week_chart_position) over( PARTITION by product_name order by week) highest
    from charts_table ct
   order by product_name, week
)
select * from t1 where highest != highest_chart_position;

Conversely, you place your order of cards, which is the highest rank is 1, thew me. I saw the high word and the expected value in the highest chartposition column to be on the rise, but has seen examples of it decreases. It was an oversight on my part.

Published by: Sentinel on October 9, 2008 08:31

Tags: Database

Similar Questions

  • Compare the records from the same table

    Hi all

    I have this sample, the data were rendered are group by my_id, the same number of records exist for the two my_ids (2 and 62) as:

    create table myTest as
    (
    my_id number, 
    fieldA varchar2, 
    fieldB number, 
    fieldC number, 
    cost_A number, 
    cost_B number
    )
    
    insert into myTest (my_id, fieldA, fieldB, fieldC, cost_A, cost_B)
    values (2, 'MAINT', 60, 5, 10.3, 15.00);
    insert into myTest (my_id, fieldA, fieldB, fieldC, cost_A, cost_B)
    values (2, 'MAINT', 70, 6, 20.3, 25.00);
    insert into myTest (my_id, fieldA, fieldB, fieldC, cost_A, cost_B)
    values (2, 'MAINT', 80, 7, 30.3, 35.00);
    insert into myTest (my_id, fieldA, fieldB, fieldC, cost_A, cost_B)
    values (62, 'MAINT', 60, 5, 10.3, 5.00);
    insert into myTest (my_id, fieldA, fieldB, fieldC, cost_A, cost_B)
    values (62, 'MAINT', 70, 6, 20.3, 25.00);
    insert into myTest (my_id, fieldA, fieldB, fieldC, cost_A, cost_B)
    values (62, 'MAINT', 80, 7, 10.3, 15.00);
    

    How can display the records with numbers of different costs for the same unique key (fieldA, fieldB and FieldC)?

    With the help of Oracle Database 11 g Enterprise Edition Release 11.2.0.4.0.

    I hope that my message is clear.

    Thank you!

    Hello

    user9542267 wrote:

    ... I need all the exits...

    Me too.  Always show the complete, accurate, results you want from the data provided.  If you want 2 rows at the exit point, don't tell you want 1.

    Here are the results you want?

    MY_ID FIELDA, FIELDB, FIELDC OLD_COST_A COST_A OLD_COST_B COST_B

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

    MAINT 62 60 5 10.3 10.3 15 5

    MAINT 62 80 7 30.3 10.3 35 15

    If so, here's a way to get them:

    WITH got_old_costs AS

    (

    SELECT fielda, fieldb, fieldc, cost_a, my_id, cost_b

    , LAG (cost_a) OVER (PARTITION BY fielda, fieldb, fieldc)

    ORDER BY my_id

    ) AS old_cost_a

    , LAG (cost_b) OVER (PARTITION BY fielda, fieldb, fieldc)

    ORDER BY my_id

    ) AS old_cost_b

    OF mytest

    WHERE my_id IN (2, 62)

    )

    SELECT my_id, fielda, fieldb, fieldc

    old_cost_a, cost_a

    old_cost_b, cost_b

    OF got_old_costs

    WHERE my_id = 62

    AND (cost_a <> old_cost_a)

    OR cost_b <> old_cost_b

    )

    ORDER BY fielda, fieldb, fieldc

    ;

  • Support for mandatory request - insert, select from the same table

    Hi all

    I need your help to write queries effectively.
    Oracle Version: 10.2.0.3.0
    OPERATING SYSTEM: UNIX

    I have a METRICS_TBL that is mentioned below table.

    CYCLE_DATE METRIC VALUE
    08/17/2008 COST-TV 100
    08/17/2008 COST-JOURNAL 50
    08/17/2008 COST-POSTALMAIL 25
    08/17/2008-PROD-TV 10
    08/17/2008-PROD-JOURNAL 25
    08/17/2008-PROD-POSTALMAIL 5

    Any data above, I have to add (Insert into select METRICS_TBL METRICS_TBL) at the same table with the records as mentioned below.

    2008-08-17 COSTPERPROD-TV 10
    08/17/2008-COSTPERPROD-LOG 2
    2008-08-17 COST PROD-POSTALMAIL 5

    Basically, I need to calculate the cost per product for each category. Depending on the settings available, metric should also be changed as COSTPERPROD and values should be cost/prod under each category.

    Can someone help me with the query.

    Thank you

    Something like this:

    INSERT INTO metrics_tbl
    (cycle_date, metrics, value)
    SELECT cost.cycle_date
             , 'COSTPERPROD-'||cost.mtype
             ,cost.value / prod.value
    FROM  (
       select cycle_date,substr(metrics, 1, 4) mtype, substrmetrics,instr(metrics,'-')+1) mmetric
       where substr(metrics, 1, 4) = 'PROD'
       ) prod
    INNER JOIN (   select cycle_date,substr(metrics, 1, 4) mtype, substrmetrics,instr(metrics,'-')+1) mmetric
       where substr(metrics, 1, 4) = 'COST'
       ) cost on cost.cycle_date = prod.cycle_date and cost.mmetric = prod.mmetric
    

    Ideally you would divide your METRICS column into two: one for the thing you're measuring (e.g. TV or NEWSPAPER) and the other for the metric (COST, PROD, COSTPERPROD etc.). That's what I did in the views online. Without it, it's kinda a mess and behave so, if the table becomes much more.

    HTH

    Nigel cordially

    Edited by: nthomas on January 14, 2009 15:57 - small correction SQL

  • Compare several records in the same table

    Hello, I m writing a bat file so I can export information that I question.

    I have an automatic integration which checks from time to time if there is no new client´s and generates a trigger.
    If an error levonorgestrol I the process is repeated until the client is located in the database.
    I can't create tables or change the structure.


    My problem is that I need to compare the record more updated with the CLIENT_ID field
    Let me give you an example:


    Table A

    ERROR_MESSAGE CREATEDUSERID CLIENT_ID
    0 success 01/01/2009
    1 error 01/01/2009
    2 success 01/01/2009
    1 success 01/02/2009
    3 success 01/02/2009
    4 error 01/02/2009

    I need to compare "1" client created on 01/01/2009 this error gived with new records to see if there's success and my
    query should only return the client_ID '4' and export with sqlplus

    Could you please help me?

    Hello

    It works very well with my data.
    What data do you use? After a few lines of sample data (CREATE TABLE AS executable... or INSERT... statements are best) that give you wrong results.

    Make sure that everything is spelled correctly.
    For example, roughly halfway through the query, you say

    and io.status = 'Erro' 
    

    and at the end you say

    having max(decode(io.status, 'Error', 1, 2))  ...
    

    That is the same ("Error" or "Error") in both places?

  • How to compare data from the two table column by column and connect the result compared to the separate table

    Hello experts...

    I'm new to this forum and Oracle coding complex.

    I have a task in which I want to compare two tables with the same structure, same columns. My Oracle database is 11g Release 2. I want this column-by-column comparison and save the result in a separate table. (say result_table)

    The structure of the table would be as follows:

    SQL > desc emp;

    Name                                      Null?    Type

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

    EMPNO NOT NULL NUMBER 4

    ENAME VARCHAR2 (10)

    JOB                                                VARCHAR2(9)

    MGR                                                NUMBER(4)

    HIREDATE DATE

    SAL                                                NUMBER(7,2)

    COMM                                               NUMBER(7,2)

    DEPTNO NUMBER (2)

    SQL > emp_comp desc;

    Name                                      Null?    Type

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

    EMPNO NOT NULL NUMBER 4

    ENAME VARCHAR2 (10)

    JOB                                                VARCHAR2(9)

    MGR                                                NUMBER(4)

    HIREDATE DATE

    SAL                                                NUMBER(7,2)

    COMM                                               NUMBER(7,2)

    DEPTNO NUMBER (2)

    Both of the tables EMPNO is the primary key.

    I want to compare these two tables based on the EMPNO (EMPNO even is both tables).

    The result log table would be as follows:

    SQL > create table result_table

    2 (check_sr_no, number (5),)

    table_name 3 varchar2 (30),

    ROW_ID 4 varchar2 (20).

    column_name 5 varchar2 (20).

    6 column_data varchar2 (1000).

    compared_by 7 varchar2 (10));

    Table created.

    SQL > alter table result_table

    2 Add the constraint result_table_pk

    3 primary key (check_sr_no);

    Modified table.

    The name of the table and the name of the column will not be hardcoded (based parameter by user)

    I tried to use the dbms_comparison package, but do not find comparison of column to column.

    I also used by using the query operator LESS. It's showing the difference in rank.

    I also went to https://asktom.oracle.com/pls/apex/f?p=100:11:0:P11_QUESTION_ID:2151582681236

    Your help will be very appreciated.

    Thank you

    DK

    Tom Kyte has answered this question MANY times over the years. Here are links for its solution

    https://asktom.Oracle.com/pls/Apex/f?p=100:11:0:P11_QUESTION_ID:4934799800346640847

  • Selection of records in the same table problem

    Hi all

    I have the following table
    with emp as (
               select '11' A,'aaa' B, '40' C from dual union all
               select '11','bbb', '40' from dual union all
               select '33','ccc', '30' from dual union all
               select '44','ddd', '20' from dual union all
               select '11','eee', '10' from dual
              )
    My results should be
    A           B        C
     
    11        aaa      40
    11        bbb     40      
    11        eee     10
    Basically the requirement is select different combination of values in the column 'B' need to know what is the value of 'A' (it must be the same for all records of three)


    Hope in this sense.


    See you soon

    Sexy

    an attempt to rephrase:

    you want all the files "aaa", "bbb" and "EEA" where, for each set of column A, where they exist.

    that is, if games to have only "aaa" and "bbb" but not "eee" in column B, then you don't want to retrieve this record?

    Try this:

    SQL> with emp as (
      2  select '11' A,'aaa' B, '40' C from dual union all
      3  select '11','bbb', '40' from dual union all
      4  select '33','ccc', '30' from dual union all
      5  select '44','ddd', '20' from dual union all
      6  select '11','kkk', '20' from dual union all
      7  select '10','eee', '20' from dual union all
      8  select '11','eee', '10' from dual
      9  )
     10  select a, b, c
     11   from (select emp.*, count(*) over (partition by a) cnt
     12    from emp
     13   where b in ('aaa','bbb','eee'))
     14   where cnt = 3
     15  /
    
    A                                B                                C
    -------------------------------- -------------------------------- --------------------------------
    11                               eee                              10
    11                               aaa                              40
    11                               bbb                              40
    
    3 rows selected.
    

    Published by: WhiteHat June 30, 2011 11:04

  • remove the last record from the plsql table

    TYPE r_LOOPElement IS RECORD (TermID   NUMBER 
                                          );
    
    TYPE t_LOOPType IS TABLE OF r_LOOPElement INDEX BY BINARY_INTEGER;
    i_CustomerLoop      t_LOOPType ;
    
    i_CustomerLoop(1).TermID=1;
    i_CustomerLoop(2).TermID=2;
    i_CustomerLoop(3).TermID=3;
    Under certain conditions, I need to remove the last record from i_CustomerLoop

    whichi s the best way to do it. because I sometimes get duplicates for TermID in this plsql table.

    Hello

    See [removing items from Collection (DELETE method) | http://download.oracle.com/docs/cd/B28359_01/appdev.111/b28370/collections.htm#CJAFGFIG]

    Kind regards

  • Update, and select from the same table

    Hello

    I have this selection - I've tested and work

    [code]

    Select
    case
    When DTF_REEW_201301.KTMO = 1 then DTF_REEW_201301.KTBT
    of other DTF_REEW_201301.KTBT - CTF_REEW_KUM_201301.KTBT
    end

    as KTBT_ISO,

    case
    When DTF_REEW_201301.KTMO = 1 then DTF_REEW_201301.KTZN
    of other DTF_REEW_201301.KTZN - CTF_REEW_KUM_201301.KTZN
    end

    as KTZN_ISO,

    case
    When DTF_REEW_201301.KTMO = 1 then DTF_REEW_201301.KTAB
    of other DTF_REEW_201301.KTAB - CTF_REEW_KUM_201301.KTAB

    end as KTAB_ISO,

    DTF_REEW_201301.brnrn,
    DTF_REEW_201301.ktat

    Of

    reewcore. CTF_REEW_KUM_201301 CTF_REEW_KUM_201301,
    reewdq. DTF_REEW_201301 DTF_REEW_201301

    where

    (DTF_REEW_201301.BRNRN = CTF_REEW_KUM_201301.BRNRN
    and DTF_REEW_201301.KTAT = CTF_REEW_KUM_201301.KTAT)
    and CTF_REEW_KUM_201301.KTMO = (DTF_REEW_201301.KTMO - 1).

    [/ code]

    With the result of KTBT_ISO, KTZN_ISO and KTAB_ISO I want to update the table 'reewdq. DTF_REEW_201301' - and this picture is in the select statement.

    I think I've tried every update statement - but no update does not work.

    I need something like this:

    [code]

    Update reewdq. DTF_REEW_201301 set t1 t1. KTBT_ISO =

    (

    Select
    case
    When DTF_REEW_201301.KTMO = 1 then DTF_REEW_201301.KTBT
    of other DTF_REEW_201301.KTBT - CTF_REEW_KUM_201301.KTBT
    end
    as KTBT_ISO

    Of

    reewcore. CTF_REEW_KUM_201301 CTF_REEW_KUM_201301,
    reewdq. DTF_REEW_201301 DTF_REEW_201301

    where

    (DTF_REEW_201301.BRNRN = CTF_REEW_KUM_201301.BRNRN
    and DTF_REEW_201301.KTAT = CTF_REEW_KUM_201301.KTAT)
    and CTF_REEW_KUM_201301.KTMO = (DTF_REEW_201301.KTMO - 1).

    ),

    T1. KTZN_ISO =

    (

    Select

    case
    When DTF_REEW_201301.KTMO = 1 then DTF_REEW_201301.KTZN
    of other DTF_REEW_201301.KTZN - CTF_REEW_KUM_201301.KTZN
    end
    as KTZN_ISO

    Of

    reewcore. CTF_REEW_KUM_201301 CTF_REEW_KUM_201301,
    reewdq. DTF_REEW_201301 DTF_REEW_201301

    where

    (DTF_REEW_201301.BRNRN = CTF_REEW_KUM_201301.BRNRN
    and DTF_REEW_201301.KTAT = CTF_REEW_KUM_201301.KTAT)
    and CTF_REEW_KUM_201301.KTMO = (DTF_REEW_201301.KTMO - 1).

    ),

    T1. KTAB_ISO =

    (

    Select
    case
    When DTF_REEW_201301.KTMO = 1 then DTF_REEW_201301.KTAB
    of other DTF_REEW_201301.KTAB - CTF_REEW_KUM_201301.KTAB
    end as KTAB_ISO

    Of

    reewcore. CTF_REEW_KUM_201301 CTF_REEW_KUM_201301,
    reewdq. DTF_REEW_201301 DTF_REEW_201301

    where

    (DTF_REEW_201301.BRNRN = CTF_REEW_KUM_201301.BRNRN
    and DTF_REEW_201301.KTAT = CTF_REEW_KUM_201301.KTAT)
    and CTF_REEW_KUM_201301.KTMO = (DTF_REEW_201301.KTMO - 1).

    )

    [/ code]

    But this isn´t of the labour code. Someone an idea please? Can someone help me please.

    Best regards

    Heidi

    Use MERGE:

    Fusion

    in reewdq. DTF_REEW_201301 t1

    a_l'_aide_de)

    Select the case sensitive option

    When DTF_REEW_201301.KTMO = 1 then DTF_REEW_201301.KTBT

    of other DTF_REEW_201301.KTBT - CTF_REEW_KUM_201301.KTBT

    end as KTBT_ISO,

    case

    When DTF_REEW_201301.KTMO = 1 then DTF_REEW_201301.KTZN

    of other DTF_REEW_201301.KTZN - CTF_REEW_KUM_201301.KTZN

    end as KTZN_ISO,

    case

    When DTF_REEW_201301.KTMO = 1 then DTF_REEW_201301.KTAB

    of other DTF_REEW_201301.KTAB - CTF_REEW_KUM_201301.KTAB

    end as KTAB_ISO,

    reewdq. ROWID that RID

    of reewcore. CTF_REEW_KUM_201301 CTF_REEW_KUM_201301,

    reewdq. DTF_REEW_201301 DTF_REEW_201301

    where DTF_REEW_201301.BRNRN = CTF_REEW_KUM_201301.BRNRN

    and DTF_REEW_201301.KTAT = CTF_REEW_KUM_201301.KTAT

    and CTF_REEW_KUM_201301.KTMO = DTF_REEW_201301.KTMO - 1

    ) t2

    on)

    T1. ROWID = t2.rid

    )

    When Fachhochschule

    then

    Update

    the t1 value. KTBT_ISO = t2. KTBT_ISO,

    T1. KTZN_ISO = t2. KTZN_ISO,

    T1. KTAB_ISO = t2. KTAB_ISO

    /

    SY.

  • Comparison of the 2 columns from the same table

    Mr President.

    I have a table xyz that has 10 million rows

    who has 3 columns

    ProfileId personid fpersonid


    I find on all of the profileids that do not match personid = fpersonid

    is there a way better and faster outside self-join?


    Select a. profileid xyz a, b of xyz where a.personid! = b.fpersonid

    Your SQL is incorrect.

    Just use... . Select xyz profileid where personid! = fpersonid

  • compare amounts of two separate tables to each other.

    I have two tables
     table pants
    
    id amt
    1 50
    1 50
    1 50
    
    
    table jacket
    
    id amt
    1  50
    1  50
    1  50
    
    when i do a select  of say 
    
    
    SELECT  a.id, b.id   SUM(a.amt),sum(b.amt) from pants a, jacket b WHERE A.ID = B.ID group by a.id, b.id 
    
    
    /* i get a really awkaward result i can't understand what im doing wrong*/
    I would get a single line with the result set


    an a.mt b b.amt
    1 1 150 150


    but instead my amount in duplicate like 3 or four times. like the 450 and 450?
    I tried to use SEPARATE on the sql, but I got no where can someone throw me a BONE here.
    Thanks in advance.

    That's what you want...

    sudhakar@ORCL>-- Sample data in virtual tables
    sudhakar@ORCL>with pants as
      2  (select 1 id, 50 amt from dual union all
      3  select 1 id, 50 amt from dual union all
      4  select 1 id, 50 amt from dual)
      5  ,jacket as
      6  (select 1 id, 50 amt from dual union all
      7  select 1 id, 50 amt from dual union all
      8  select 1 id, 50 amt from dual)
      9  -- Actual QUERY
     10  select aid, bid, aamt, bamt
     11  from
     12  (SELECT  a.id aid,   SUM(a.amt) aamt from pants a group by a.id) pants_summary
     13  inner join
     14  (SELECT  b.id bid,   sum(b.amt) bamt from  jacket b group by  b.id ) jacket_summary
     15   on( pants_summary.aid = jacket_summary.bid)
     16   ;
    
           AID        BID       AAMT       BAMT
    ---------- ---------- ---------- ----------
             1          1        150        150
    
    sudhakar@ORCL>
    

    pre
    Sudhakar B.

  • parents and children of the same table records

    I want to create a query that is a union such that 2nd resultset is based on the game of results 1. I have a table that has parent and child records in the same table.

    Table: EVENTS
    EVENT_ID
    PARENT_EVENT_ID
    CREATED_DATE
    (other columns)

    If PARENT_EVENT_ID is null, then this is a parent record, otherwise it is a book of the child. I want to select all parent records then union them with all the related child records... something like this:

    Select * from EVENTS where CREATED_DATE < sysdate - 90 and PARENT_EVENT_ID is null - all parents
    Union
    Select * event where PARENT_EVENT_ID in (select EVENT_ID of EVENTS where CREATED_DATE < sysdate - 90 and PARENT_EVENT_ID is null)-include all children selected from the top parent

    It works but it of kind of ugly, I want to avoid using the subselect in the 2nd because it is a repeat of the 1st statement, is there a way to alias the first statement and only refer to him in the 2nd query?

    Hello

    CONNECTION is very similar to a UNION on the same table. How is this:

    SELECT     *
    FROM     events
    WHERE     created_date     < SYSDATE - 90
    START WITH     parent_event_id     IS NULL
    CONNECT BY     parent_event_id     = PRIOR event_id
         AND     LEVEL          <= 2    -- Maybe
    ;
    

    ?

    To answer your question: Yes, you can still keep the result set of a query and the Treaty as if it were a table or vew, like this:

    WITH     roots     AS
    (
         SELECT     *
         FROM     events
         WHERE     created_date     < SYSDATE - 90
         AND     parent_event_id     IS NULL
    )
    SELECT     *
    FROM     roots
         --
    UNION ALL
            --
    SELECT     *
    FROM     events
    WHERE     created_date     < SYSDATE - 90
    AND     parent_event_id     IN (
                          SELECT  event_id
                          FROM    roots
                      )
    ;
    

    It's always a bit ugly, however. I would use CONNECT BY or from Oracle 11.2, a recursive WITH clause (which is explicitly a UNION), or, in any version, a self-join, like this:

    SELECT     c.*
    FROM          events  c
    LEFT OUTER JOIN     events     p  ON   p.event_id  = c.parent_event_id
    WHERE   p.parent_event_id  IS NULL
    ;
    

    I hope that answers your question.
    If not, post a small example of data (CREATE TABLE and only relevant columns, INSERT statements) for all of the tables involved and the results desired from these data.
    Report where the queriy above (the one you decide to use) is producingthe bad results and explain, using specific examples, how you get the right results from these data in these places.
    Always say what version of Oracle you are using (for example, 11.2.0.2.0).
    See the FAQ forum {message identifier: = 9360002}

  • How to display the scroll bar on every page of the tab where the content from the same block of data?

    Hi gurus,

    Is it possible to display the scroll bar on each tab on Web tab page where the display of content from the same block of data?

    for example:

    Block of data elements used:

    1. employee ID

    2. first name

    3 first name

    4 date of hire

    scroll bar appears on the page 1

    5 salary

    6 commission

    scroll bar appears on page 2

    Many thanks in advance,

    Kind regards

    Ferrere

    Dear fendy_chang,

    A data block can have only 1 scroll bar. If the form is for display purpose, you can create several data bloks that points to the same table for each tab.

    Manu.

  • Insert select on the same table: possible without side effects?

    I have a very large table T1 containing millions of records. I need to treat its lines and create a few new lines based on selection.

    Table T1 contains events and one of them, with the code 100, is created by the further development of other events inside the table.

    My code is as follows:

    insert /*+append */ into T1 (code,...) values (100, c1,c2,...)
    select c1,c2... from T1 where (code=20 or code=10) and <other conditions>...
    
    

    as you can see I'm extract T1 lines to insert again in T1 with a different code and I use the direct path in order to get good performance.

    My fear is: choose is made from the same table I risk data loss? In general it is a good practice? Or is it better to create another table?

    Hello

    No I don't think that there may be loss of data. But that may depend on the behavior of the application and your where clause.

    I will explain how it is treated, so you can see if it's ok:

    1. the table is locked because of the insert add, (as)

    2 lines are read by select and make compatible from the State that was at the beginning of the query - 1.

    3 rows are inserted at the end, after the high-water line

    4 columns for new lines are sorted to be merged in the index

    5. high watermark is adjusted - visible new lines and lock is released

    Note that 2. and 3. occur at the same time: rows are inserted all read.

    Note that anyone can choose in the table during the operation - they see the changes committed only - if the State 1.

    all other DML are waiting for the lock being released, and will see new ranks and then

    If you have things that prevent the direct-path insert, append the hint will be ignored. So, if you must rely on close to 1. then the best lock explicitly with table lock. But I don't think that you need.

    Kind regards

    Franck.

  • Update of several rows of the same table using trigger

    Hello

    I have the following table

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

    CREATE TABLE ATT_PERMISSIONS
    (
    DATE OF PRMONTHYR,
    EMPIDNUMBER (10)DEFAULT VALUE 0,
    FROMDATETIMEDATE,
    TODMTFDATETIMEDATE,
    NUMBER OF PERMMINS (3),

    SSLINASTRETCH NUMBER (3).

    PERMATTINELIGIBLENUMBER (1)DEFAULT VALUE 0
    );

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

    I want to update the column PERMATTINELIGIBLE in the same table when each line is inserted.

    I created following trigger for that

    CREATE OR REPLACE TRIGGER SSL_AFTER_UPDATE AFTER UPDATE ON ATT_PERMISSIONS

    FOR EACH LINE

    BEGIN

    Update ATT_PERMISSIONS set PERMATTINELIGIBLE =

    (

    SELECT THE CHECK BOX

    WHEN PERMMINS < = 60

    or

    (

    PERMMINS < = SSLINASTRETCH

    AND

    COUNT (CASE WHEN PERMMINS = 0 THEN NULL OTHERWISE 1 END)

    COURSES (PARTITION BY empid, prmonthyr) < = 1

    )

    THEN 1

    0 OTHERWISE

    END

    FROM ATT_PERMISSIONS where prmonthyr =:new.prmonthyr, empid =:new.empid

    ) where prmonthyr =:new.prmonthyr, empid =:new.empid;

    END;

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

    That is to say.

    When a row is inserted the PERMATTINELIGIBLE of all records for this employee for the month is recalculated.

    There would be only two or three records for an employee for a month.

    PERMMINS is the number of minutes to use as short leave.

    If it is less than 60 is eligible for the bonus to attendance for this employee.

    If this isn't normally the date is not eligible for the attendance for this employee premium.

    but an exception is that if a short leaves of DISP employee allowed a month (IE. ( SSLINASTRETCH ) in a straight line, IE. one day, that day is eligible for the bonus of presence.

    That is to say. Why the number of short leaves in a month is taken. If it's one and done minutes use is lower to SSLINASTRETCH, then the day is eligible for the attendance bonus.

    But when I try to create the trigger I get error, PL/SQL: ORA-00907: missing a right parenthesis.

    Help, please

    I use oracle 10g

    I want to update the column PERMATTINELIGIBLE in the same table, when each row is inserted.

    No - you do NOT want to do in a trigger.

    You cannot query the same table that the trigger is activated on. -C' is a MUTANT and you will get an exception.

    When a row is inserted is recalculated to the PERMATTINELIGIBLE of all the records for that employee for that month.

    There would be only two or three records for an employee for a month.

    PERMMINS is the number of minutes to use as short leave.

    If it is less than 60 is eligible for the bonus to attendance for this employee.

    If is not normally the day is not eligible for the premium of attendance for this employee.

    but an exception is that if a short leaves of DISP employee allowed a month (IE. SSLINASTRETCH) in a straight line, IE. one day, that day is eligible for the bonus of presence.

    That is to say. Why the number of short leaves in a month is taken. If it's one and done minutes use is less than SSLINASTRETCH, then the day is eligible for the bonus of presence.

    But when I try to create the trigger I get error, PL/SQL: ORA-00907: missing a right parenthesis.

    You can not do in a trigger. Even if correct you this error of syntax, you will get the exception that I mentioned above.

    Oracle is a multi-user system. Other users can modify the same table that you use.

    So even if you could try to interview "all the records for this employee" some of them may be locked if other users access them. Also, an insert statement could try to insert two rows for an employee and the trigger could not have seen the other lines still.

    If you want to work with data SETS (all records for an employee), you must write the PL/SQL code that LOCKS all lines updated to prevent others to interfere.

    Then you make your operations but NOT in a trigger. Perform inserts and then use an UPDATE query to set this value.

    Then VALIDATE to unlock the lines.

  • Copy a few lines in the same table, but with different IDS

    Hi all
    I had this problem... I have a table with its pk (which is a sequence). With a statement select, I extract a few lines, and then I would record in the same table, but with IDS different (obviously eheheh).

    I don't know how to do...

    Thks for all help ;)


    p.s. I want to change some fields in lines before saving as well... don't know how do it more...

    example:

    ID NAME TYPE
    1 ferrari car
    2 cars lamborghini


    I'm trying to copy the second and changing its type in "BMW".

    I could result:

    1 ferrari car
    2 cars lamborghini
    3 bmw cars



    Thank you

    Assuming that your table is named cars_tb and the cars_seq of the sequence.

     insert into cars_tb
      select cars_seq.nextval, name, type
      from cars_tb
    

    This would overlap with any existing lines, but with a new id.

    If you want to duplicate a specific line and change a value, you could do something like:

     insert into cars_tb
      select cars_seq.nextval, name, 'BMW'
      from cars_tb
      where type = 'lamborghini'
    

    or better, use the PK

     insert into cars_tb
      select cars_seq.nextval, name, 'BMW'
      from cars_tb
      where id = 2
    

Maybe you are looking for