Problem updating multiple rows in the table using subquery

Hi everyone, I can't update multiple lines with a subquery. Here is the configuration:
create table mytable (
col_a number primary key,
col_b number,
col_c number,
col_d number);

insert into mytable values (1 ,1,1,15);
insert into mytable values (2 ,1,2,7 );
insert into mytable values (3 ,1,3,11);
insert into mytable values (4 ,1,4,23);
insert into mytable values (5 ,1,5,14);
insert into mytable values (6 ,2,1,50);
insert into mytable values (7 ,2,2,41);
insert into mytable values (8 ,2,3,13);
insert into mytable values (9 ,2,4,12);
insert into mytable values (10,2,5,19);
insert into mytable values (11,3,1,10);
insert into mytable values (12,3,2,92);
insert into mytable values (13,3,3,81);
insert into mytable values (14,3,4,17);
insert into mytable values (15,3,5,66);
insert into mytable values (16,4,1,54);
insert into mytable values (17,4,2,41);
insert into mytable values (18,4,3,22);
insert into mytable values (19,4,4,24);
insert into mytable values (20,4,5,17);
For this example, by using an update statement (or merge if this is better), I mean set values for col_d where col_b = 3 equal to that of the col_d where col_b = 1 and col_c equal between them. Results should resemble the following after the update:
col_a col_b col_c col_d
1     1     1     15
2     1     2     7
3     1     3     11
4     1     4     23
5     1     5     14
6     2     1     50
7     2     2     41
8     2     3     13
9     2     4     12
10    2     5     19
11    3     1     15
12    3     2     7
13    3     3     11
14    3     4     23
15    3     5     14
16    4     1     54
17    4     2     41
18    4     3     22
19    4     4     24
20    4     5     17
I see her there within my reach using this query, where I want to put b_col_d = a_col_d, but miss me something, this query returns too many rows when it is used in the update statement.
select * from (
  select col_a as a_col_a, col_b as a_col_b, col_c as a_col_c, col_d as a_col_d
  from mytable
  where col_b = 1
  ) a, (
  select col_a as b_col_a, col_b as b_col_b, col_c as b_col_c, col_d as b_col_d
  from mytable
  where col_b = 3
  ) b
where a.a_col_c = b.b_col_c
Update column_d set mytable = (select? where?)

Can someone help me get there? I use 10 gr 2.

Thank you!
Mark

I hope that's what you're looking for:

SQL > UPDATE mytable myt1
  2  SET    col_d = ( SELECT myt2.col_d
  3                   FROM   mytable myt2
  4                   WHERE  myt2.col_b = 1
  5                   AND    myt1.col_c = myt2.col_c
  6                 )
  7  WHERE  col_b = 3
  8  AND    EXISTS
  9         ( SELECT NULL
 10           FROM   mytable myt2
 11           WHERE  myt2.col_c = myt1.col_c
 12         )
 13  ;

5 rows updated.

SQL > SELECT * FROM mytable ORDER BY col_a;

               COL_A                COL_B                COL_C                COL_D
-------------------- -------------------- -------------------- --------------------
                   1                    1                    1                   15
                   2                    1                    2                    7
                   3                    1                    3                   11
                   4                    1                    4                   23
                   5                    1                    5                   14
                   6                    2                    1                   50
                   7                    2                    2                   41
                   8                    2                    3                   13
                   9                    2                    4                   12
                  10                    2                    5                   19
                  11                    3                    1                   15
                  12                    3                    2                    7
                  13                    3                    3                   11
                  14                    3                    4                   23
                  15                    3                    5                   14
                  16                    4                    1                   54
                  17                    4                    2                   41
                  18                    4                    3                   22
                  19                    4                    4                   24
                  20                    4                    5                   17

20 rows selected.

Thank you very much for providing the sample data in a format that is easy to consume form, as well as the expected results.

Tags: Database

Similar Questions

  • Problem updated 16 rows, it updates all rows in the table

    I tried to update of 16 ranks with AL as the name of divion with the script as below:

    Update wire_center set a.wg_id = '63'
    where EXISTS (select b.div_name from wire_center, title b, area c, ns_workgroups d).
    where B.DIV_NAME =' al ' AND a.dis_id = c.dis_id and b.div_id = c.div_id and d.wg_id = a.wg_id);

    When I run it, it updates all rows in the table.

    However when I run the select subquery, it returns 16 ranks.

    It seems where EXISTS is the problem. If I exist, it gave me error ora-00936.

    How to fix? Need urgent help please. DB is 10g.

    Thanks in advance.

    If I understand correctly what you want, you must not include wire_center once again in the second select (in your clause).
    The second selection has no relationship to the table you're updating, so because select the second returns 16 rows, it is always set to true will. For each line. If all the rows will be updated.

    Instead of

    Update wire_center a set a.wg_id='63'
    where EXISTS (select b.div_name from wire_center a, division b, district c, ns_workgroups d
    where B.DIV_NAME='AL' AND a.dis_id=c.dis_id and b.div_id=c.div_id and d.wg_id=a.wg_id );
    

    Try this:

    Update wire_center a set a.wg_id='63'
    where EXISTS (select b.div_name from division b, district c, ns_workgroups d
    where B.DIV_NAME='AL' AND a.dis_id=c.dis_id and b.div_id=c.div_id and d.wg_id=a.wg_id );
    

    Published by: big on 29-dec-2010 14:27

  • Select the row of the table using radiobutton

    Dear all,

    I want to have a column that will a radiobutton and button in the page. When the user clicks the button, so I want a task by taking the data from the selected row. I kept the rows in the table to a single selection. When the user selects the row (without selecting the radiobutton control), then I can also get the current line and get the data from that. But when the user presses the button (in its actionlistener) then how can I know if the user has selected the radiobutton control or not in the current line.
    Can someone give me guidance in this problem...

    Kind regards
    David

    JDeveloper 11.1.1.4.0

    Santosh,
    where is the property of the value of the bound radio button for? It is an attribute of the line? If so you can access the value via the current line.

    Timo

  • Delete multiple rows in the table.

    How to remove several rows in the table at the same time?

    I have a table with 10 rows.

    I have a loop for delete lines 1, 3 and 6.

    It deletes the row 1 and 3 of the Index out of Bounds exception.

    I know why this is happening.

    I'm looking for a function or something to delete several lines at once?

    Thank you

    It must be a logic error.

    For each row, you delete, you should reduce the number of indexes.

    For example

    If you want to delete lines 1, 3 and 6 of a table that contains 6 rows

    Remove row1 as Table.Row [0].instanceManager.removeInstance ();    [now how many lines will become 5]

    Remove row3 as Table.Row [1].instanceManager.removeInstance ();     [Note that index is 1 because you have already deleted a record, and the index starts at 0.

    Remove row6 as Table.Row [3].instanceManager.removeinstance ();      [already two deleted record, so 6-2-1 = 3 is your index to remove]

    You understand the logic?

    Nith

  • Update multiple rows in a table at a time

    Hi all

    I write a stored procedure that fills all columns in a table, that table with a query with the exception of a single column, say 'ABC' column in this table. I have to load this left on column using a different query that refers to some other tables. How can I update this column 'ABC' with all its rows of data from a different query?

    Thank you very much!!

    If you post an update with a create table and insert statements, it would be useful... I think that you want to update a table with a select from another table using a join... is that correct?

    c customer update
    Set client_name =)
    Select client_name
    of another_customer_table one
    where a.customer_id = c.customer_id_from_original_customer_table
    )
    where c.customer_id in (select customer_id from another_customer_table)

    Where clause is required only if you don't want to update missing customer_ids to null.

  • How can I get the number of rows in the table using Sunopsis API target in ODI 10 g?

    Hi guys,.

    In fact, I want to send an alert message as soon as the interface is run from a package. I've included alert OdiSendMail that sends an email once the interface is running.

    Could someone please tell how to get the number of rows inserted into the table the Sunopsis API target.

    I tried to use < % = odiRef.getNbRows () % >, but this has not worked for me. Since I am a beginner, could you please help out me

    This is my mail to send ODI format

    "The population of data managed to < % = odiRef.getSysDate () % >

    Total of lines in the target table is: < - need an API code-> "


    Kind regards

    Clinton

    Published by: LawrenceClinton on February 25, 2013 20:53

    Hello

    Create the project with details below variable

    Nom_de_variable: Total_Row_Count

    Type of variable: Variable discount

    Definition tab:
    Data type: digital
    Action: No persistent

    Refresh the tab:
    Schema: provide the schema of your repository to work and be

    SELECT log.nb_row
    THE journal of snp_step_log, snp_scen_step step
    WHERE log.nno = step.nno
    AND step.scen_no = (SELECT scen_no FROM snp_scen_step WHERE step_name='<%=odiRef.getPrevStepLog ('STEP_NAME") % > ')
    AND log.sess_no = '< % = odiRef.getSession ("SESS_NO") % >' code at low
    AND step.step_name = ' < % = odiRef.getPrevStepLog ("STEP_NAME") % > '

    Note: add this variable after the stage of interface in your package (after the interface anywhere you can place), you can add this variable before ODISendEmailNotification in your package

    call this varciable * #Total_Row_Count * in the Notification of ODISendEmail

    for example :

    Data has been fulfilled successfully in < % = odiRef.getSysDate () % >
    Total no of lines filled are: * #Total_Row_Count *

    it will work

    cordially,
    Phanikanth

    edited by: Phanikanth on February 28, 2013 01:13

    edited by: Phanikanth on February 28, 2013 01:14

  • Help to update all rows in the Table with a sequence

    Hi all

    I have a table with the name Report_Status, now we have added a new [Batch_ID] column in the table. The new column takes a value in a sequence. Can someone tell me a normal SQL queries or the procedure for updating each line in turn to an existing table in the value of the sequence? We cannot delete the existing data, must be added for each record batch code.

    example of name of sequence: Report_Status_Batch_ID_SEQ

    Report_Status name and the column name of the table Batch_ID

    Thanks for your time to help out me.

    Kind regards

    Ashish

    Thanks John,

    or I could write as update dept set id = ID_SEQ. NEXTVAL;

    This also works. Thanks for the idea.

  • How to get multiple rows in the table is displayed on the form

    I am looking for a way to get more table rows that appear on the form.  I created a table with a header and a line with the add-in and remove button option.  I like how the sample purchase order form has it setup in the designer it shows one line and it appears on the form so the table has three rows to start with.  How do I do that?  I looked at the code on the sample and I couldn't find it anywhere.  Please help, thanks.

    Hello

    I have an example here in the construction of a dynamic array markets.

    http://www.assuredynamics.com/index.php/category/portfolio/building-dynamic-tables/

    You must select the line and then go to object > Binding palette and set it to repeat. You can set the minimum and initial number to what you want.

    The form should be saved in a dynamic form of XML in the Save as dialog box.

    Also the page must be set to Flowed, so that as the table grows, push another object down. In addition, you need to configure the paging settings so that the flow of the page as the number of lines is superior to a single page.

    Give a start and if you need more help,

    Niall

    Ensure the dynamics

  • Update multiple rows in a table with one value of another multiple

    Using SQL Server

    How this can be done:

    In all the lines in the upper table PS_CA_BILL_PLAN, we want to update the field LAST_XREF_SEQ_NUM with the MAX (XREF_SEQ_NUM) + 1 of the bottom table

    Where the CONTRACT_NUM and the BILL_PLAN_ID are equal

    This SQL below (in green) works for one line only

    SQL.jpg

    I think it's the SQL code you're looking for:

    UPDATE PS_CA_BILL_PLAN SET LAST_XREF_SEQ_NUM =

    (SELECT MAX (XREF_SEQ_NUM) + PS_CA_BP_XREF 1 B)

    WHERE B.CONTRACT_NUM = A.CONTRACT_NUM AND B.BILL_PLAN_ID = A.BILL_PLAN_ID)

  • Rows in the table LT until merger of all workspaces

    Database 11.2.0.4

    In our environment, we ran a few important updates in the workspaces and then merged these live workspaces.  Up to 25% of the data has been updated in those workspaces.  When these update batch workspaces have been created, previous workspaces exist who are not yet ready for the merger.

    I noticed that the underlying tables of the LT now have about 25% more lines than the live view.  We have now discovered that dbms_wm.refreshWorkspace works really poor.  This statement is used to take seconds or minutes to the maximum, but can now take half an hour or more.

    We recorded a support with Oracle request and provided details of the plan to see if we can get some resolution/advice explaining.

    In the meantime, I have a question for the forum help my understanding of the works of the Manager of the workspace.

    In the example below, I have table "wm_test_table", "test_workspace1" and "test_workspace2".  In "test_workspace1", I update all rows in the table - the table LT now has 2 rows for each record.  I merge 'test_workspace1' live and LT still has 2 rows for each record.  The other lines are not removed until "test_workspace2" is merged and removed.


    SQL> -- Create a test table
    SQL> create table wm_test_table (
      2     id integer primary key,
      3     name varchar2(100));
    
    
    Table created.
    
    
    SQL> -- Insert 1,000 rows
    SQL> insert into wm_test_table (
      2     select rownum, 'before'
      3     from dual
      4     connect by level <= 1000);
    
    
    1000 rows created.
    
    
    SQL> commit;
    
    
    Commit complete.
    
    
    SQL> -- Version enable it
    SQL> execute dbms_wm.enableVersioning('wm_test_table');
    
    
    PL/SQL procedure successfully completed.
    
    
    SQL> -- Create two workspaces
    SQL> execute dbms_wm.createWorkspace('test_workspace1');
    
    
    PL/SQL procedure successfully completed.
    
    
    SQL> execute dbms_wm.createWorkspace('test_workspace2');
    
    
    PL/SQL procedure successfully completed.
    
    
    SQL> -- Go to test_workspace1 and update all the rows
    SQL> execute dbms_wm.goToWorkspace('test_workspace1');
    
    
    PL/SQL procedure successfully completed.
    
    
    SQL> update wm_test_table set name = 'after';
    
    
    1000 rows updated.
    
    
    SQL> commit;
    
    
    Commit complete.
    
    
    SQL> -- Check how many rows in wm_test_table_lt - we expect 2,000 as we haven't merged yet
    SQL> select count(*) from wm_test_table_lt;
    
    
      COUNT(*)
    ----------
          2000
    
    
    SQL> -- Merge and remove test_workspace1
    SQL> execute dbms_wm.goToWorkspace('LIVE');
    
    
    PL/SQL procedure successfully completed.
    
    
    SQL> execute dbms_wm.mergeWorkspace('test_workspace1', remove_workspace=>true);
    
    
    PL/SQL procedure successfully completed.
    
    
    SQL> -- Check how many rows in wm_test_table_lt
    SQL> -- I'd hope there'd only be 1,000 but there is 2,000
    SQL> select count(*) from wm_test_table_lt;
    
    
      COUNT(*)
    ----------
          2000
    
    
    SQL> -- Let's see if compressWorkspace will get rid of them
    SQL> execute dbms_wm.compressWorkspace( -
    >       workspace => 'LIVE',   -
    >       auto_commit => true,   -
    >       remove_latest_deleted_rows => true);
    
    
    PL/SQL procedure successfully completed.
    
    
    SQL> -- They're still there
    SQL> select count(*) from wm_test_table_lt;
    
    
      COUNT(*)
    ----------
          2000
    
    
    SQL> -- Now merge test_workspace2
    SQL> execute dbms_wm.mergeWorkspace('test_workspace2', remove_workspace=>true);
    
    
    PL/SQL procedure successfully completed.
    
    
    SQL> -- and the additional 1,000 rows are removed
    SQL> select count(*) from wm_test_table_lt;
    
    
      COUNT(*)
    ----------
          1000
    

    Is it possible to safely remove the extra lines in LT without waiting until we merged the workspaces that existed before the update batch has been done?  I hope that it will so improve the performance of refreshWorkspace.


    I thought that something like this can do, but it does not remove the other ranks, is:

    execute dbms_wm.PurgeTable(
     table_id => 'test.wm_test_table', 
     archive_table => null, 
     where_clause => 'id not in (select id from test.wm_test_table)', 
     instant=>null, 
     workspace => 'LIVE', 
     purgeAfter => false);
    

    This question is similar to

    Lines left in the table LT after removal of the direct workspace and the removal of all workspaces

    and

    How to delete soft deleted records?

    but I'm still not sure.

    Thank you

    John

    Hello

    This behavior is expected and it is due to the test_workspace2 workspace.  A compression operation will not allow compressed range to include a backup implicit point that is created for a child workspace.  This is done to preserve the integrity of this workspace.  The initial 1000 lines are all lines that are visible from test_workspace2.  Removal of these lines would leave test_workspace2 will 0 lines in there.  Lines 1000 updates are the lines that do not appear on LIVE.  So, given that the two sets of lines are needed, none of the lines can be deleted physically.  A compression operation will never change the view of data for any workspace.  It only removes the unnecessary records.  The two options in order to be able to compress these lines is to either delete (as you did) or refresh test_workspace2.  The refresh operation would move the implicit savepoint for the workspace to a version that isn't after all 2000 lines in the table _LT and allow the dressing to remove first half of them because they are no longer needed by any child workspace.

    With respect to the performance of RefreshWorkspace, I would need to see a trace of the transaction file.  But who should rather get through the SR you filed.

    Kind regards

    Ben

  • Display the option button if the number of rows in the table is equal to 1

    I'm having issues there buy maybe a simple script out there.

    I have a table which can have up to four lines. If the table contains only one row, an option button can be clicked to change the format/entry of cells in this row. I have to hide this box of a user option, if they added more of a line to the table and make it available again if they reduce the number of lines to one.

    I have everything else works well (radio buttons change line format, add/delete rows in the table using buttons, number of rows in the first column)

    Anyone have any ideas?

    See you soon

    Bobby

    You can place it in the code that adds more lines when running... If you have questions send the fomr to [email protected] so I can check it out...

    Thank you

    Srini

  • How to set current row in the table after use the button tab on inputText

    Hi all
    My first post.., I am beginner in ADF and I'll try to explain my problem.

    For now we use ADF 11 g (11.1.1.4), in a jsff page I have a table with a column of inputText.
    On the the inputText valueChangeListener, I invoke a method in a bean viewScope that call an EJB method, make some services to the EJB on the modified line. After that, I update the VO and the table (because other values in the row have been changed) and reset the focus on the inputText even modified by the user with javaScript, because the focus was lost after updating.
    So far, everything works fine.

    When I use the arrow keys to change the selected line in the table, this is excellent work (focus is always in the inputText next or previous), but if the user tries the key tab allows you to change the current line, the inputText on the line that has the focus, but the current row in the table is not changed (I think that is normal).

    My question: How can I change the current line after the tab key in this case?

    I don't know if it is really clear, not easy to explain, do not hesitate to ask for more details.
    Thanks in advance.

    Hello

    My question: How can I change the current line after the tab key in this case?

    Use a client event to listen to the keyboard entry and intercept the tab. Use af:serverListener to call the server to set the rowKey on the table and deliver a PPR for the table to repaint

    See example 11 on http://www.oracle.com/technetwork/developer-tools/adf/learnmore/index-101235.html#CodeCornerSamples

    to learn more on how to use the client listener and the receiving server

    Frank

  • Problem with the removal of the first row of the table

    Hi all

    I have a problem with my forms with tables where I have buttons to add or remove rows from the table. Adding the lines works fine but deleting lines has a problem when deleting the first row. Example: Here is a table with two rows where column 1 has the add and remove buttons and column 2 user imput that looks like this:

    Column 1
    Column 2
    Add and remove buttons hererank 1
    Add and remove buttons hererank 2
    Add and remove buttons hererank 3

    If the user deletes line 2 the table looks like which is correct:

    Column 1
    Column 2
    Add and remove buttons hererank 1
    Add and remove buttons hererank 3

    If the user deletes line 1 then the table looks like which is false since the ranks 2 and 3 should be left:

    Column 1
    Column 2
    Add and remove buttons hererank 1
    Add and remove buttons hererank 3

    The code is pretty simple:

    Table3.row2.instanceManager.addInstance (1); for the click event of the button Add line

    Table3.row2.instanceManager.removeInstance (1); for the click event of the button Delete line

    What I am doing wrong?

    Thanks in advance!

    Al

    The expression will be based on your structure... done .This refers to the current object, parent refers to the container parent of this object and index is the index of this object. So watching your hierarchy and from the key, you will need this.parent to join the line subform is it in (usually it is th esubform that repeats). If it's in a subform, you will need to add another parent. Continue on this road until you get to the repeating subform and then ask its index. Your expression might look like this.parent.parent.parent.index. Using this.index, you find the instance of the button object and there are just people and that's why you always get 0.

    Make sense?

    BTW your form is attached not... so I can't check what you should put it.

    Paul

  • Trying to create the table using Clause any Union with Select multiple stmts

    The motion seeks to get the substring of the value to for example.
    If the value is * ASA 2 * then so do ASA
    where as if the value is * 1.5 TST * it sholud come as TST as wise for others too.
    I am trying to execute stmt SQL written but in error as below:

    * ' ORA-00998 must appoint this expression with the alias column 00998.00000 - must appoint this expression with the column alias. "

    CREATE TABLE TEST_CARE AS
    (
    SELECT row_id, old_care_lvl, SUBSTR(old_care_lvl,3), len test_care_lvl FROM
    WHERE LENGTH (old_care_lvl) = 5
    UNION ALL
    SELECT row_id, old_care_lvl, SUBSTR(old_care_lvl,3), len test_care_lvl FROM
    WHERE LENGTH (old_care_lvl) = 7
    UNION ALL
    SELECT row_id, old_care_lvl, SUBSTR(old_care_lvl,3), len test_care_lvl FROM
    WHERE LENGTH (old_care_lvl) = 14
    UNION ALL
    Row_id SELECT, old_care_lvl, SUBSTR (old_care_lvl, 3), LEN test_care_lvl
    WHERE LENGTH = 7 AND old_care_lvl (old_care_lvl) = "Regular."
    );

    I want to create the table using the above given the multiple selection by using the Union ALL clause, but trying to create run query error like "ORA-00998 must appoint this expression with the alias column 00998.00000 - must appoint this expression with the column alias.

    Please guide me how to approach to solve this problem.
    Thanks in advance.

    Try this->

    CREATE TABLE TEST_CARE
    AS
      select *
      from (
              SELECT row_id, old_care_lvl,SUBSTR(old_care_lvl,3), len FROM test_care_lvl
              WHERE LENGTH(old_care_lvl) =5
              UNION ALL
              SELECT row_id, old_care_lvl,SUBSTR(old_care_lvl,3), len FROM test_care_lvl
              WHERE LENGTH(old_care_lvl) =7
              UNION ALL
              SELECT row_id, old_care_lvl,SUBSTR(old_care_lvl,3), len FROM test_care_lvl
              WHERE LENGTH(old_care_lvl) =14
              UNION ALL
              SELECT row_id, old_care_lvl,SUBSTR(old_care_lvl,3),LEN FROM test_care_lvl
              WHERE LENGTH(old_care_lvl) =7 AND old_care_lvl ='Regular'
          );
    

    N.B.: Not tested...

    Kind regards.

    LOULOU.

  • problem of adjustment of height of the rows in the tables in CS6

    I read all the debates devoted to setting the height of the rows in the tables and I can't find the answer to my problem.

    I placed a table that was created in Word.  I've done several times, but now I can not adjust the heights of less than 5mm line without losing the text in the line.  I went up to line on "exactly".  As soon as I try to reduce the height of the text disappears, but there is plenty of space above and below the text.   Hoping someone can help.

    Check the cell INSET on the cell options.

Maybe you are looking for

  • How to add between a date range + category

    I use numbers 3.6.2 on a macbook pro with El Captain 10.11.5 I have a financial spreadsheet that I want to shoot the numbers a second sheet that has a book running.  Ideally, I would like to have a book current running year round.  I can't figure out

  • My px4-300r stuck at 95% for days

    I have peripheral storage 300r px4. He is stuck at 95% for days. I tried the solutions mentioned in the similar topics (how to remove the discs and re-seat,...) Please help me even if I lose my data

  • (Xbox) On Black Ops 2, we were given millions of deaths on our stats by a modder

    I was enjoying a game of Black Ops 2 with some friends when we were caught in the trap by a Modder who gave us 10 million deaths on our stats. I have the video. How can I return my stats? Help me please! 10 million is ridiculous. I have the video evi

  • 10.3 Simulator and choose types of devices

    I tried two simulators 10.0.3.1052 and 10.0.3.698, but both versions only give me the option to select the device of resolution 1280 x 720 (when the sim card starts first to the top). Is there a magical way choose 720 x 720 or devices of resolution 1

  • BlackBerry Smartphones Device Encryption never complete (padlock icon when it is locked)

    Hello I recently added a password and on Device Encryption on my new (3G) curve 9300. Now I get a padlock just under system date and battery indicator when the phone is locked. My problem is that the padlock icon is always open, never closed. BlackBe