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.

Tags: Database

Similar Questions

  • 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

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

  • ORA-22992: cannot use LOB Locators selected from the remote tables...

    Oracle 10.2.1.0.4
    Solaris 10

    We try to access a table in another database via a db_link.

    The table we are trying to access has a LOB.

    We get the following error: ora-22992: cannot use LOB Locators selected from the remote tables.

    Is there a way to get around this? We need the data in the BLOB field.

    Thank you.

    See on metalink:

    ORA-22992 has a workaround solution in 10 gr 2
    DOC - ID: 436707.1

    Werner

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

  • Key Reg primary and foreign at the same table

    Hello

    It is - ok if create primary and foreign keys on the same table itself?

    For example:

    In a table X, I have two columns such as Codeunite and ParentUnitcode.
    Is it ok if create Mt10 as principal and ParentUnitcode as foreign keys in the table itself?

    The foreign key base table: OrganizationalUnit
    Foreign key columns: ParentUnitCode
    Primary/Unique key base: OrganizationalUnit
    Primary/unique key col: Mt10

    Can anyone answer me on that?

    Concerning
    Rajesh

    OWB,

    If the two tables are favouring a one-to-one relationship so there no need to have two separate tables and so you can have free referential foreign keys in the same table where the primary key.

  • 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

    ;

  • How to make the two direct DBS update and write through the same cache?

    Hello
    Sorry for any fault of grammar since English is not my first language.
    Our team is planning to migrate a project centered on the DB to the solution of database consistency. A problem that we face is that a large part of the business logic was implemented in DB stored procedures, which would take the time and effort to rewrite in the entity of consistency model. To for this transition process harmonious and progressive, is there a way to support the two BD update and write through on the same cache? In other words, I want that stored procedures will continue to manage the demand for legacy, after the update, update the web service (update) or (expel/get) the cache; While new logic of business on the same Entity (Cahce), I want to reach write-through. Is it achievable? Thank you!

    Hello

    So what you're really asking is a way to ensure that updates made directly to the DB of your existing system are pushed in cache coherence.

    It is a common problem with a number of possible solutions according to your specific needs.

    If you do not need data in cache, in other words, don't mind if it's a bit outdated, then you can use the expiry and refresh to come so that consistency will expire entries the cache and reading in that the DB to the need to hide.

    If you need accurate data in the cache, it is when it changes on the DB you had to push as soon as possible in the cache, then you need another way. It depends probably the DB that you use and what functionality it provides to allow you to hang change tables.

    The SIG London yesterday there was a presentation on the Oracle Golden Gate to do this kind of thing. I think that it is based on the use of the APP and of course an Oracle DB.

    JK

  • Create the table in select * from the other table

    DB: 11.2.0.2

    How to create a table with structure only not empty the data that we do not generally create table in select * from... But the command will pull the data also. How to create without data?

    create table foo
    in select * bar
    where 1 = 0

  • check point and polygon in the same table

    Hello

    Can I store two different types of geometry as the Point and polygon in a table with one column of the SDO-Gemetry? I read somewhere that we can only store one type either points, polygon or line in one table with only on column geom, is that correct? If so, what would be the solution if the data is of type points and polygons? Should I create different table?

    Appreciate any response or any type of recommendation.

    FYI - I use oracle 10g with oracle spatial extension.

    Best regards

    Marwa

    M

    The fact that you combine several types of geometry in the same column has obvisouly some impact in the way that you investigate this type of column.

    you could include a clause, for example, to filter on the type of geometry: where a.loc.gtype = 2001 (point gtype).

    you could also, as you ask specifically points, through the access of the x and the coordinate of the point y (type) with a.loc.sdo_point.x =... and even for y.

    but I think in your case, once you have built your spatial index, it may be better (in terms of performance) to fetch the spatial operator that uses the spatial index.

    so the where clause would be: where sdo_relate (a.loc, (2001,8307,SDO_POINT_TYPE (107.53,32.53,null), null, null), sdo_geometry "mask = EQUALITY '") = 'TRUE '.

    Luke

  • 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

  • Group by and count on the same table

    Hello

    DB version: 11.2.0.3

    Tried to SQL:

    create table log1 (identification number, status varchar2 (15) date of insert1);

    insert into log1 values (1, 'SUCCESS', sysdate-1);

    insert into log1 values (2, 'SUCCESS', sysdate-2);

    insert into log1 values (3, 'FAIL', sysdate-1);

    insert into log1 values (4, 'FAIL', sysdate-2);

    insert into log1 values (5, 'FAIL', sysdate-1);

    insert into log1 values (6, 'ERROR', sysdate-1);

    insert into log1 values (7, 'ERROR', sysdate-1);

    insert into log1 values (8, 'ERROR', sysdate-1);

    insert into log1 values (9, 'ERROR', sysdate-1);

    WITH AS TBL1

    (SELECT THE STATE OF 'SUCCESS' OF THE DUAL UNION ALL

    SELECT "FAIL" STATUS OF THE DUAL UNION ALL

    SELECT 'PENDING' STATUS OF DOUBLE)

    SELECT NVL (TBL1. STATUS OF STATE, 'TOTAL'), COUNT (LOG1.ID)

    FROM TBL1 JOIN LOG1 FULL EXTERNAL

    ON LOG1. STATUS = TBL1. STATUS

    GROUP BY ROLLUP (TBL1. STATUS)

    STATUSCOUNT (LOG1.ID)
    IN CASE OF FAILURE3
    SUCCESS2
    PENDING0
    TOTAL (*)4
    TOTAL9

    t need only State of SUCCESS, FAILURE, WAITING. and NUMBER of global ID omit the rest of type status as ERROR, etc. EXECUTION in the result set.

    I don't need the TOTAL (*) of the above result set. Which is total (Error).

    Please tell us how the query above can be changed to produce the result to wait.

    Thank you

    Hello

    So, you want to see all the tbl1 lines, if they have a match in log1, and for the grand total of lines, you must count the log1 lines if they have a match with tbl1.  This looks like a job for a FULL OUTER JOIN:

    SELECT t.status

    EARL of (l.status) AS cnt

    OF log1 l

    FULL OUTER JOIN tbl1 t WE t.status = l.status

    GROUP OF ROLLUP (t.status)

    HAVING t.status IS NOT NULL

    OR of a GROUP (t.status) = 1

    ;

  • 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

  • Error 0x8024402c when installing 8.1 update and Apps from the store

    m trying to update my windows 8 to 8.1 version but it shows the error code: 0x8024402c, I tried to install other applications his mistake even giving

    Please help me out

    Hi yogesh,

    Please contact the Microsoft community.

    I suggest you refer to the suggestion of Geethu B replied on 22 October 2013.

    http://answers.Microsoft.com/en-us/Windows/Forum/windows8_1-windows_install/error-code-0x8024402C/41c16c48-4a51-472a-9137-b4627d6f8fa0

    Hope this solves the problem. If the problem persists, you can write to us and we will be happy to help you further.

  • The creative cloud is compatible with mac and windows from the same download?

    Because at the time there was a different version for each apple mac or windows systems. And I can't seem to find this information on the Web site.

    Creative cloud installation is different for mac and windows machine. You can Download Adobe Creative Cloud apps | CC free trial Adobe from there.

Maybe you are looking for