Update of the columns with a subquery

Hello

I use Oracle 11.2 and need to update the columns with sub query.

Create table tb_base (id number (5), note number (5), number YR_MO (6), total_score_season number (5), season_start_yr_mo number (6));

Create table tb_season (season_id number (5), number start_yr_mo (6), end_yr_mo number (6));

insert into tb_base (1, 30, 201005, 0, 0);
insert into tb_base (1, 12, 201008, 0, 0);
insert into tb_base (1, 43, 201105, 0, 0);
insert into tb_base (1, 10, 201107, 0, 0);
insert into tb_base (2, 15, 201006, 0, 0);

insert into tb_season (1, 201005, 201009);
insert into tb_season (2, 201104, 201108);

I want to updat the column total_score_season and season_start_yr_mo with a single UPDATE statement as follows:
tb_base 1, 30, 30, 201005, 201005
42, 1, 12, 201008 201005
201104, 201105, 1, 43, 43
53, 1, 10, 201107 201104
2, 15, 201006, 15 201005

Hello

If it is not obvious how to do an UPDATE, and then UPDATE maybe is the wrong tool for the job. Try instead MERGER:

MERGE INTO  tb_base     dst
USING (
          SELECT  b.id, b.score, b.yr_mo
       ,       SUM (b.score) OVER ( PARTITION BY  b.id
                                   ,          s.start_yr_mo
                                   ORDER BY          b.yr_mo
                         ) AS running_score_season
       ,       s.start_yr_mo
       FROM       tb_base     b
       JOIN       tb_season     s  ON   b.yr_mo  BETWEEN  s.start_yr_mo
                                      AND       s.end_yr_mo
      )               src
ON    (    dst.id     = src.id
      AND  dst.yr_mo     = src.yr_mo
      )
WHEN MATCHED THEN UPDATE
SET   dst.total_score_season     = src.running_score_season
,     dst.season_start_yr_mo     = src.start_yr_mo
;

'Total_Score_Season' means the total score at the end of the season. You're uisng as column name for a cumulative score up to certain point, perhaps in mid-season. Maybe another name, like running, or season_score_so_far scoreseason would be clearer.

No matter what you call, a cumulative total calls the analytic function SUM. While you can use analytical functions in the corellated subqueries used in UPDATE statements, they are extremely inefficient, because they must be calculated for each line, so does want a cumulative total suggests that you want to MERGE rather than UPDATED.

Tags: Database

Similar Questions

  • Insert/update the column with the clob data type

    Hi all

    ORCL Version: 11g.

    I have a table with the clob data type.

    Test12

    (col1 clob);

    I'm trying to insert/update to update the column with more than 4000 characters.

    But due to the limitation of tank 4000, I could not Insert/Update.

    Need your help in resolving this issue.

    THX

    Rod.

    The limit of 4000 characters is incorrect.  That pertains only to the varchar2 data type.  A clob can hold more than 4 G.

    Here is an example that shows how to insert it, I found...

    Otherwise, here is a way 'dirty' to do.

    insert into your_table (COLA, COLB)

    values

    (PRIMARY_KEY, PART 1 OF DATA)

    ;

    Update your_table

    Define COLB = COLB | PART 2 OF BIG DATA

    where COLA = PRIMARY_KEY;

    Update your_table

    Define COLB = COLB | PART 3 OF BIG DATA

    where COLA = PRIMARY_KEY;

    .. and so on...

    I don't know that I personally recommend the second style...  But he could do the job.

  • A statement UPDATE changes the line if the update changes the column even value?

    HI -.

    I have a main classification and the corresponding audit table. I have a trigger. I created using the suggestion:http://www.runningoracle.com/product_info.php?products_id=211

    Now the problem that I am facing is that: if I run the same query (same value) update on the main table for n times (with the same data)... my audit table is updated with a new record every time. He is not able to determine that the value is the same.

    My goal is to load the audit table data if the value in a cell in the main table is really have changed.

    A statement UPDATE changes the line if the update changes the column even value?

    Best regards

    I have a main Table and the corresponding Audit Table. I have a trigger. I created using the suggestion:http://www.runningoracle.com/product_info.php?products_id=211

    Why?

    You try to resolve what made you what are the PROBLEM that select as the solution?

    Now the problem that I am facing is that: if I run the same query (same value) update on the main table for n times (with the same data)... my audit table is updated with a new record every time.

    Yes - it is EXACTLY what you say that you wanted to do and EXACTLY what the code does in this article.

    He is not able to determine that the value is the same.

    I think you mean that the code does NOT check if the value is the same. You can certainly change the code to do this check. But I have a question to start with your need for such a solution.

    My goal is to load the audit table data if the value in a cell in the main table is really have changed.

    OK - then modify the code to check EACH COLUMN and compare its NEW value to the OLD value and insert only the table of audit if SOME (or columns you care specifically) have changed.

    A statement UPDATE changes the line if the update changes the column even value?

    It depends on what you mean by 'change the line '. Of course to update a column with the SAME value does not change the resulting data value.

    But the physical structure of the line, the location of the pieces of line and information in the header block (YVERT, etc.) will change. And, as others have said Oracle doesn't know and doesn't care, so if the new value of a column is the same as the old value will create redo, undo and the rest.

    Start over and tell us what PROBLEM you're trying to solve. Then we can help you find the best way to solve it.

  • My date and time settings are set in Egypt and whenever updates of the laptop with microsoft server time, it increases of 1 h.

    My date and time settings are set in Egypt and whenever updates of the laptop with microsoft server time, it increases of 1 h.
    Recently in Egypt, changes of daylight has been cancelled and I guess that's the cause of the problem!
    Any ideas?

    If time was recently cancelled, you can go to your control panel:
    Panel-> data and time-zone (tab) >
    and uncheck the "automatically adjust clock for daylight saving time.

    Otherwise, you probably need to adjust your zone settings on your computer using Microsoft time zone Editor.

    TZEdit: <> http://download.microsoft.com/download/5/8/a/58a208b7-7dc7-4bc7-8357-28e29cdac52f/tzedit.exe >

    HTH,
    JW

  • Try to download an update of the firmware with a file extension ROM Vista says it's an unrecognized file type

    Title: download original ROM files on Vista

    Try to download an update of the firmware with a file extension ROM Vista says it's an unrecognized file type. Patches?

    You must get the update of the firmware that can handle the type of file the same place ROMA, you got the firmware file.  From what I see, they seem specific to the product updated. http://www.FileInfo.com/extension/ROM if they don't have it, talk to their technical support staff and ask them what type of program is required to install the firmware provided in ROM format and where you can get it.  Vista doesn't have the ability to read a file ROM type  Some software must be added to give this capability.

    Here is an article I read: "various hardware upgrades and emulators use the ROM file extension." A user needs a flash program usually available on the website of the manufacturer of the motherboard. The user must not flashing BIOS unless the user knows that he has the image of his mother. An error or the computer may be turned off when the wrong BIOS image is flashed. "So be very careful here, or you can completely disable your system.

    I hope this helps.

    Good luck!

    Lorien - MCSA/MCSE/network + / has + - if this post solves your problem, please click the 'Mark as answer' or 'Useful' button at the top of this message. Marking a post as answer, or relatively useful, you help others find the answer more quickly.

  • update of NULL in the column with the values of the adjacent column

    Examples of data

    with test_data as
    (select 1 as one, null as two, 2 as three,5 as four, 6 as five, null as six from dual
    union all
    select 1 as one, null as two, 2 as three,5 as four, 6 as five, null as six from dual)
    select * from test_data;
    

    This is one of those cases, the case may be where any value of a column can be null

    or two similar columns can be null. If the column is null then I want to update the adjacent column

    average value of the column, if the first column is null so I want to take the next non-null column and update, if the last column is null

    so I want to take prev not zero column and to update.

    In this case would be my expected output

    Un Two Three Four Five Six
    11.52566
    123566

    Prospects for the future the suggesion or advice.

    Or, using Analytics:

    SQL> with test_data (id, one, two, three, four, five, six) as (
      2    select 1, 1   , null, 2   , 5, 6, null  from dual union all
      3    select 2, 1   , null, 3   , 5, 6, null  from dual union all
      4    select 3, 1   , null, null, 5, 6, null  from dual union all
      5    select 4, null, null, null, 5, 6, null  from dual
      6  )
      7  select *
      8  from (
      9    select id
     10         , cell
     11         , case when next_nn is not null and prev_nn is not null
     12             then (next_nn + prev_nn)/2
     13             else nvl(next_nn, prev_nn)
     14           end val
     15    from (
     16      select id
     17           , cell
     18           , val
     19           , last_value(val) ignore nulls over(partition by id order by cell) as prev_nn
     20           , first_value(val) ignore nulls over(partition by id order by cell range between current row and unbounded following) as next_nn
     21      from test_data
     22      unpivot include nulls (val for cell in (one as 1, two as 2, three as 3, four as 4, five as 5, six as 6) )
     23    )
     24  )
     25  pivot ( min(val) for cell in (1 as "ONE", 2 as "TWO", 3 as "THREE", 4 as "FOUR", 5 as "FIVE", 6 as "SIX") )
     26  ;
    
            ID        ONE        TWO      THREE       FOUR       FIVE        SIX
    ---------- ---------- ---------- ---------- ---------- ---------- ----------
             1          1        1,5          2          5          6          6
             2          1          2          3          5          6          6
             3          1          3          3          5          6          6
             4          5          5          5          5          6          6
    
  • Update updated the columns with null are excluded from the DB adapter

    Hi all

    My fusion system interacts with DB2 using the DB adapter.

    I have a script that I need to update a flag in the table after completing my bpel workflow. But by updating the value of indicator to "Processsed", there are other columns with a NULL value.

    How to solve this problem. I don't want to do, the other columns should not be alerted.

    Thank you

    Richa

    Hello

    The issue has been resolved. In fact, I've updated the update statement, excluding items that couldnot be updated top link mappings. I had to create a new adapter and it worked.

    Concerning

  • Return only the columns with different values.

    Hi all

    I am trying to solve a seemingly trivial problem but can't seem to get any clear answer anywhere in any forum. Consider a single table with 5 columns and only two lines:
    row_id           first_name         last_name        age            gender
    1                    John                  doe               27             M
    1                    Jane                  doe               27              F  
    Assume that there is no primary key or any other constraint. Also assume that there are only and only two lines of this table. So I need basically a query that, overall above lines, would return first name and sex, because they are only different columns in two lines, as well as their values. Even if I can get sort of column names that are different and that would be enough that I can easily access the values later. It is also important to remember that I may not know the names of the columns in the columns, so basically, somehow, I use user_tab_columns in the process.

    Any help appreciated.

    Published by: 894302 on May 1, 2013 10:35

    Hello

    894302 wrote:
    The exact release could be just a varchar variable that lists all the columns that have different values with each column name separated by commas. We'll call the query you want as a QUERY. So, if I do something like

    Select the REQUEST of double; Output should be: 'first_name, equality of the sexes. Is this possible?

    In this case:

    SELECT     RTRIM ( DECODE (a.row_id,     b.row_id,     NULL, 'row_id,')     ||
                    DECODE (a.first_name, b.first_name, NULL, 'first_name,') ||
              DECODE (a.last_name,  b.last_name,  NULL, 'last_name,')  ||
              DECODE (a.age,        b.age,          NULL, 'age,')      ||
              DECODE (a.gender,     b.gender,     NULL, 'gender,')
               , ','
               )          AS different_columns
    FROM     rhit_table_x  a
    JOIN     rhit_table_x  b  ON  a.ROWID     < b.ROWID
    ;
    

    Output:

    DIFFERENT_COLUMNS
    ---------------------------------------
    first_name,gender
    

    This assumes that you have really a table, then you can use ROWID to distinguish the lines, since there is no primary key.
    If this isn't the case, you first assign ROW_NUMBER in a subquery.

  • Online update of the column data in the sql statement

    Hello Experts,

    I created a sql report
    select apex_item.checkbox(1,RES_RTNG_ID) as "RATING ID",RES_ID,SKILL_SET_ID, to_char(EVALUATION_DATE,'DD-MM-YYYY') d, EVALUATOR, RATE_TYPE, RATING ,REMARKS
    from HRMS_RESOURCE_RATING
    where TRAINING_ID = :P51_TRNG_PLN_ID
    I have a box that is used to delete rows in the report.
    and I want features to update the data in the column of the report.
    Please help me how to do this.

    Thank you
    Jitendra

    Hi all

    I did it with inline sql reports updated. Those who want to implement online update of sql reports

    Please see the link below:
    http://roelhartman.blogspot.in/2009/11/UPDATEABLE-interactive-report-websheets.html?z

    Thank you
    Jitendra

  • So I think that I can not decrease the scale of the column with an alter statement

    Just learned this, kind of interesting for me. If the column contains data I can increase the scale, but I can't then decreased to its original scale, even if the numbers would be fit. Also from what I've read the only case to reduce a scale is to null, remove, or perhaps copy the column in a column being added.

    Just think it's kinda weird that you cannot reduce a number column, even if the values would be fit.

    This has something to do with the zero no significant?

    This has something to do with the zero no significant?

    Non - Oracle stores ONLY significant digits - not leading or zeros to the right.

  • How can I replace the value of the column with a particular value in SQL

    Hi all

    Is someone can you please tell me how can I format my output with the replacement of a column value with a specific value that really depends on the current value of the column

    I am executing the following SQL statement

    Select state, count (id) from < table_name > where composite_dn = < composite_dn_name > Group by State;
    My current performance is:

    State Instance number
    1 3
    3 28

    I want to replace the value in the State column as follows

    No.OfInstances State
    3 filled
    28 faulted

    I want '1' to be reppaced of 'Done' and '3' is replaced by 'Faulted. '

    Is is possible with SQL or PL/SQL, if it is then how I can achieve this required. Help, please!

    Thanks in advance!

    Published by: Roshni Shankar on October 27, 2012 12:38 AM

    Hi Claude,.

    I guess this CASE clause can be simulated by a DECODE and also it is very easy to use.
    See - http://www.techonthenet.com/oracle/functions/decode.php

    select decode(t1.state,t2.state_id,t2.state_name), t1.count_id
    from  t2, (select state,count(id) count_id
     from 
     where composite_dn= 
    group by state) t1
    where t1.state = t2.state_id;
    

    HTH
    Vanessa B.

    Published by: Vanessa B on October 27, 2012 14:02
    -link added

    Published by: Vanessa B on October 27, 2012 14:19
    -added code sample

  • Software update of the Cobcern with FirmWare...

    Hello... I just downloaded and updated the installer of the firmware from the sandisk website. (SansaUpdaterInstall.exe)

    Then after I got it and reopen my player (sansa fuze) even once, I can't seen mp3 files that I copied before the Installer update. I open the music folder and it is empty, but in the player, I can see all of the mp3 files. Is - this bacause of the installer? How can I do to see the files?

    And when I re - connect to my computer, I see no internal memory. I see only two letters of SANSA FUZE G: drive and removable drive H: (SD card)

    I think that I have installed the version install evil!

    answer me someone as soon as possible please...

    Looks like you got the player in MTP mode and the update of the firmware he switched to MSC.

    Change it in settings > USB MTP mode & the player will appear as a portable device - Sansa Fuze.

    S

  • Update of the blackBerry with BB link software

    Hello!

    I have a message on my Z10 that the new OS 10.2.0.424 update is available, I don't have wifi around me networks. So I want to do the update via the link of BB, but it can't find the new update.

    Why this new update does not appear on the BB link?

    Thank you

    Rejna85

    The update is finally...

    Rejna85

  • Using the same value of the column with two conditions as different results

    Hi all

    Maybe it's easy to answer what I'm trying to do now, but my brain really stop and I need your opinion. I have a table of account which has values like below

    Account Fairy OBJ type of Unit year
    11500196AA130

    20

    12000196AA1302012000-196AA1302011000-196AA1302023000196AA1302024500-196AA13020

    If I can get flow and receiveable with two different query sql like underneath, but I can't get the flow and the reciveable with the sum in a row and sql. Please give me some opinions to go.

    Thank you

    SELECT

    To_char (Trim (Account)) Account_no,

    (select accountname in AddressBook where accountname addid = TO_CHAR (TRIM (account))),

    Sum(Fee/100) flow

    Of

    account tb1

    where

    obj in ('196') and tb1.type = 'AA' and TO_CHAR (TRIM (unit)) = "130".

    and year = '20'

    and tax > 0

    Group

    account

    order by

    account;

    SELECT

    To_char (Trim (Account)) Account_no,

    (select accountname in AddressBook where accountname addid = TO_CHAR (TRIM (account))),

    Sum(Fee/100) reciveable

    Of

    account tb1

    where

    obj in ('196') and tb1.type = 'AA' and TO_CHAR (TRIM (unit)) = "130".

    and year = '20'

    and tax < 0

    Group

    account

    order by

    account;

    SELECT

    Trim (Account) Account_no,

    (select accountname in AddressBook where accountname addid = TRIM (account)),

    Sum (case when fairy< 0="" then="" fee/100="" else="" 0="" end)="">

    sum (case where fees > 0 then charge/100 0 otherwise end) flow

    Of

    account tb1

    where

    obj in ('196') and tb1.type = 'AA' and TO_CHAR (TRIM (unit)) = "130".

    and year = '20'

    Group

    account

    order by

    account;

  • update change the date with sysdate column

    Hello
    I want to add a new column, modify_date, to an existing table.

    This column must have sysdate when ever a record is updated.

    Please let me know the best way to do this same thing.

    Trigger? I think that some problem of mutation?

    Thanks and greetings

    Use a before update trigger and set the: new.modify_date such as SYSDATE.

    SQL> create table t(no number, last_updated date)
      2  /
    
    Table created.
    
    SQL> insert into t values(1,'')
      2  /
    
    1 row created.
    
    SQL> commit
      2  /
    
    Commit complete.
    
    SQL> edit
    Wrote file afiedt.buf
    
      1  create or replace trigger t_trig before update on t for each row
      2  begin
      3     :new.last_updated := sysdate;
      4* end;
    SQL> /
    
    Trigger created.
    
    SQL> update t set no = 2 where no = 1
      2  /
    
    1 row updated.
    
    SQL> select * from t
      2  /
    
            NO LAST_UPDA
    ---------- ---------
             2 04-DEC-08
    

Maybe you are looking for

  • How can I find the music marked with an exclamation point in my itunes library?

    How can I find the music marked with an exclamation point in my itunes library?

  • Satellite P50 - B - internal error power after upgrade to Windows 10

    After the upgrade to Windows 10 and after reboot, I got Internal_Power_Error and laptop restarts automatically.After signing, get the same error of internal power and the repetitions of the process. I contacted Toshiba Support and they did NOT have a

  • Satellite c855 - VGA port suddenly does not work

    Hi people I have the toshiba satellite-c855 - 17 c. Windows 7.Usually, I work on my laptop and monitor, which I use by plugging the vga cable. I use this set to the top for more than a year and never had any problems. Now, nothing happens when I plug

  • Inspiron 530, GeForce 8400 gs, driver Windows XP

    I had a GeForce 8300 gs video card of which went wrong. I bought GeForce 210 PCI-E which was not compatible, because I couldn't find a driver that was compatible. Now, I bought a GeForce 8400 hoping I might have better luck. It is not 2 days. So I ho

  • Downgrade from W7 W8

    Hello, I have recently upgraded to Windows 8 and gel when plugged into question. I was able fix the issue after almost 2 weeks of searching on Google (by changing the power to high-performance setting). Now, I want to know how can I downgrade to W7 W