Update of column with the rank on the parition

Hi, I have a table A given as:
MASTER_ID, CHILD_ID
100...
100...
100...
101...
101...
102...
102...
102...
102...

The initial values of CHILD_ID are garbage and should be updated as follows:
For each unique MASTER_ID the CHILD_ID should have values beginning with 1 (with lowest rowid)
so when this column is updated the table should look like:
MASTER_ID, CHILD_ID
100, 1
100, 2
100, 3
101, 1
101, 2
102, 1
102, 2
102, 3
102, 4

Select MASTER_ID, CHILD_ID,
Rank() over (partition by MASTER_ID order by rowid) as rank
a.

give the good ranking, but when I try to update the CHILD_ID with this rank

Update)
Select MASTER_ID, CHILD_ID,
Rank() over (partition by MASTER_ID order by rowid) as rank
of (A)
Set CHILD_ID = row_id

I get the error:
SQL error: ORA-01732: non-legal data manipulation operation on this point of view
01732 00000 - 'operation not legal data manipulation on this point of view'

Can you please help me with an sql update to set this child_id?
Thank you
create table masta
(
   pk_id number,
   ch_id number
);

insert into masta values (1, 0);
insert into masta values (1, 0);
insert into masta values (1, 0);

insert into masta values (2, 0);

ME_XE?merge into masta m
  2  using
  3  (
  4     select
  5        row_number() over(partition by pk_id order by 1) rn,
  6        rowid as the_rowid
  7     from masta
  8  ) m1
  9  on
 10  (
 11     m.rowid = m1.the_rowid
 12  )
 13  when matched then update set m.ch_id = m1.rn;

4 rows merged.

Elapsed: 00:00:00.14
ME_XE?select * from masta;

             PK_ID              CH_ID
------------------ ------------------
                 1                  1
                 1                  2
                 1                  3
                 2                  1

4 rows selected.

Elapsed: 00:00:00.21
ME_XE?

Ideally, you do not want to fix the application so that 'unwanted values' are not met initially in the table.

Tags: Database

Similar Questions

  • update of column with the number of sequence based on the condition


    Hello

    Version of DB: database Oracle 11 g Enterprise Edition Release 11.2.0.1.0 - 64 bit Production

    Here's the script to reporduce:

    CREATE TABLE T2
    (
    PARAMLOCATION NVARCHAR2 (16).
    PARAMTYPE VARCHAR2 (3 BYTE),
    PARAMNUM VARCHAR2 (3 BYTE)
    )

    Insert into T2
    (PARAMLOCATION)
    Values
    ('49');
    Insert into T2
    (PARAMLOCATION)
    Values
    (« 12 ») ;
    Insert into T2
    (PARAMLOCATION)
    Values
    (« 50 ») ;
    Insert into T2
    (PARAMLOCATION, PARAMTYPE)
    Values
    ('loc51', 'B');
    Insert into T2
    (PARAMLOCATION, PARAMTYPE)
    Values
    ('loc52', 'B');
    Insert into T2
    (PARAMLOCATION, PARAMTYPE)
    Values
    ('loc53', 'B');
    Insert into T2
    (PARAMLOCATION)
    Values
    ("loc54");
    Insert into T2
    (PARAMLOCATION)
    Values
    ("loc55");
    Insert into T2
    (PARAMLOCATION, PARAMTYPE)
    Values
    ('aoc01', 'I');
    Insert into T2
    (PARAMLOCATION, PARAMTYPE)
    Values
    ('aoc02', 'I');
    Insert into T2
    (PARAMLOCATION)
    Values
    ("loc58");
    Insert into T2
    (PARAMLOCATION, PARAMTYPE)
    Values
    ("doc03", "DL");
    Insert into T2
    (PARAMLOCATION, PARAMTYPE)
    Values
    ("doc02", "DL");
    Insert into T2
    (PARAMLOCATION, PARAMTYPE)
    Values
    ("doc01", "DL");

    I should update the column in table (paramnum) function sequential paramtype as this: also you can not order in paramlocation, its like that paramlocation comes first start sequence ordering from there based on paramtype.

    PARAMLOCATIONPARAMTYPEPARAMNUM
    49
    12
    50
    loc51B1
    loc52B2
    loc53B3
    loc54
    loc55
    aoc01AI1
    aoc02AI2
    loc58
    doc03DL1
    doc02DL2
    doc01DL3

    Please advice.

    Hello

    I'll assume you have a column called load_order, which corresponds to the order of the lines:

    CREATE TABLE T2
    (
    NUMBER OF LOAD_ORDER
    PARAMLOCATION NVARCHAR2 (16).
    PARAMTYPE VARCHAR2 (3 BYTE),
    PARAMNUM VARCHAR2 (3 BYTE)
    ) ;

    Insert into T2
    (LOAD_ORDER, PARAMLOCATION)
    Values
    (1, '49');
    Insert into T2
    (LOAD_ORDER, PARAMLOCATION)
    Values
    (2, '12');
    Insert into T2
    (LOAD_ORDER, PARAMLOCATION)
    Values
    (3, '50');
    Insert into T2
    (LOAD_ORDER, PARAMLOCATION, PARAMTYPE)
    Values
    (5, 'loc51', 'B');
    Insert into T2
    (LOAD_ORDER, PARAMLOCATION, PARAMTYPE)
    Values
    (8, 'loc52', 'B');
    Insert into T2
    (LOAD_ORDER, PARAMLOCATION, PARAMTYPE)
    Values
    (13, 'loc53', 'B');
    Insert into T2
    (LOAD_ORDER, PARAMLOCATION)
    Values
    (13.2, "loc54");
    Insert into T2
    (LOAD_ORDER, PARAMLOCATION)
    Values
    (13.5, 'loc55');
    Insert into T2
    (LOAD_ORDER, PARAMLOCATION, PARAMTYPE)
    Values
    (50, 'aoc01', 'I');
    Insert into T2
    (LOAD_ORDER, PARAMLOCATION, PARAMTYPE)
    Values
    (80, 'aoc02', 'I');
    Insert into T2
    (LOAD_ORDER, PARAMLOCATION)
    Values
    (81, 'loc58');
    Insert into T2
    (LOAD_ORDER, PARAMLOCATION, PARAMTYPE)
    Values
    (82, "doc03", "DL");
    Insert into T2
    (LOAD_ORDER, PARAMLOCATION, PARAMTYPE)
    Values
    (83, "doc02", "DL");
    Insert into T2
    (LOAD_ORDER, PARAMLOCATION, PARAMTYPE)
    Values
    (99, "doc01", "DL");

    Any data type, this column is or what are the values it contains, as long as you can derive from the order of the rows of values in the column.  (Of course, the values can be consecutive integers, only they do not have to be).  If you do not have this type of column, you don't have any order to your lines, and what you request is impossible.

    Since you have a load_order column, here's a way to get the results you requested:

    MERGE INTO dst t2

    WITH THE HELP OF)

    WITH got_grp AS

    (

    SELECT load_order

    paramlocation

    paramtype

    ROW_NUMBER () OVER (ORDER BY load_order)

    -ROW_NUMBER () OVER (PARTITION BY CASE

    WHEN paramtype IS NULL

    THEN 0

    END

    ORDER BY load_order

    ) AS the grp

    THE t2

    )

    SELECT load_order

    paramlocation

    ROW_NUMBER () OVER (PARTITION BY grp

    ORDER BY load_order

    ), Paramnum

    OF got_grp

    WHERE the paramtype IS NOT NULL

    ) CBC

    WE (dst.paramlocation = src.paramlocation)

    WHEN MATCHED THEN UPDATE

    SET dst.paramnum = src.paramnum

    ;

    Results:

    LOAD_ORDER PARAMLOCATION BY

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

    1 49

    2 12

    3 50

    loc51 5 B 1

    loc52 8 B 2

    13 loc53 B 3

    13.2 loc54

    loc55 13.5

    50 aoc01 AI 1

    80 aoc02 AI 2

    loc58 81

    1 DL of 82 doc03

    2 DL doc02 83

    3 DL of 99 doc01

  • Update of column with the same value of the column

    Hello

    I have a scenario like below:

    SRC TAR RSRC FLG
    EN101 1001 1001 Y

    1002 EN101 N
    1003 EN101 N

    Since then, my flag is there for the first record, I have identical RSRC of the CBC. Now, I need RSRC 1001 value for 2 other records as the value of these recordings is N and the target is EN101.

    All of the suggestions.

    Thank you

    merge into your_table t

    a_l'_aide_de)

    Select

    tar

    min (rsrc) rsrc

    from your_table

    where flg = 'Y '.

    Group of tar

    ) u

    on (t.tar = u.tar)

    when matched

    update the value t.rsrc = u.rsrc

    where t.rsrc is null

  • An update on an index column with the same value generates an index to the top

    An update on an index column with the same value generates an update of the index?


    Thank you

    In addition to my previous answer, see also

    http://orainternals.WordPress.com/2010/11/04/does-an-update-statement-modify-the-row-if-the-update-modifies-the-column-to-same-value/

    Riyaj Shamsudeen has this to say:
    "+ We have an index on this column v1 and we update this column indexed too." Oracle was updating the indexed column? N ° if the values match the level of the indexed column, then the code of RDBMS isn't up-to-date index, a feature for optimization again. Only the row of table is updated, and the index is not updated. + "

    Hemant K Collette

  • help me create a column with the time format

    Hi all

    Please help me find the query to create a table with 3 columns and insert the following data

    ENTITYID | CHGDATE | CHGTIME

    ======= ==================== ========

    4214984 | 2 OCTOBER 06 12.00.00.000000 AM | 07:23:57

    4214985 | 12.00.00.000000 11-SEP-08 AM | 10:52:19

    and update of the first line of the above table as follows
    ---------------------
    4214984 | 2 OCTOBER 06 07.23.57.000000 AM | 07:23:57
    ------------------------
    you will need to update the CHGDATE column with the time of the CHGTIME column.

    Sorry, we need a new format around the to_date

    select to_date(to_char(chgdate, 'mm/dd/yyyy') || ' ' || to_char(chgtime, 'hh24:mi:ss'), 'mm/dd/yyyy hh24:mi:ss') from etime3;
    

    This should telll Oracle to the format of the generated string. If this isn't the case, run select with the to_char two as separate columns to see that get us the data that we expect.

  • Documentation about adding column with the DEFAULT value.

    Hello

    http://download.Oracle.com/docs/CD/B19306_01/server.102/b14200/statements_3001.htm#i2198241

    < quote >
    If you add a column, then the initial value of each row in the new column is NULL unless you specify the DEFAULT clause. In this case, Oracle database updates each row in the new column with the value specified for the DEFAULT value. This update operation, in turn, triggers AFTER UPDATE triggers defined on the table.
    < quote >

    I am not able to understand the emphasis on the part AFTER UPDATE while the column with DEFAULT values addition triggers both BEFORE and AFTER triggers defined on UPDATE.

    According to the documents of 11 g

    http://download.Oracle.com/docs/CD/B28359_01/server.111/b28286/statements_3001.htm#i2133105

    n.m. (u) r only changes with the NON NULL columns, but focus on AFTER UPDATE is still there. No trigger defined on the fire of the update in this case.

    This insistence is intended? If not, IMO, it should be changed.

    Kind regards

    Hi Sissi. After further discussion, we have added some information about a change in behavior. Here is what says the next version of the doc:

    When you add a column, the initial value of each row in the new column is null.

    * If you specify the DEFAULT clause for a column NOT NULL, then the default value is stored as metadata, but the column itself is not populated with data. However, the following queries that specify the new column are rewritten so that the default value is returned in the result set.

    This optimized behavior differs from earlier versions, when as part of operation ALTER TABLE Oracle database updated every line in the newly created with the default column and then fired defined update triggers on the table. In this release, no trigger is triggered because the default value is stored only in the form of metadata.

    * If you specify the DEFAULT for a nullable column clause, then the default value is added to existing lines under this ALTER TABLE statement, and any update triggers defined on the table are activated. This behavior also means if you change a NOT NULL column with a default value to be nullable.

    Hope that helps to clarify the matter further.

    Kind regards
    Diana

  • I have a column with two values, separated by a space, in each line. How to create 2 new columns with the first value in a column, and the second value in another column?

    I have a column with two values, separated by a space, in each line. How do I create 2 new columns with the first value in one column and the second value in another column?

    Add two new columns after than the original with space separated values column.

    Select cell B1 and type (or copy and paste it here) the formula:

    = IF (Len (a1) > 0, LEFT (A1, FIND ("", A1) −1), ' ')

    shortcut for this is:

    B1 = if (Len (a1) > 0, LEFT (A1, FIND ("", A1) −1), ' ')

    C1 = if (Len (a1) > 0, Member SUBSTITUTE (A1, B1 & "", ""), "")

    or

    the formula of the C1 could also be:

    = IF (Len (a1) > 0, RIGHT (A1, LEN (A1) −FIND ("", A1)), "")

    Select cells B1 and C1, copy

    Select cells B1 at the end of the C column, paste

  • Updated my iPad with the new OS.

    Updated my iPad with the new OS. Initially it lights, so I have two buttons held together and finally happened. Now it is stuck on the screen with the time and will not move forward, nor is it resets. Is there any solution for this?

    Thank you

    Kat

    Try a forced reboot. Hold down the home and Sleep/Wake buttons simultaneously for about 15-20 seconds , until the Apple logo appears. You won't lose anything.

    If a force restart does not help try recovery Mode:

    https://support.Apple.com/en-us/HT201263

  • I've updated for picture with the captain and when I plug in my iPhone it loads the same pictures twice each time how it stop loading the same things every day?

    I've updated for picture with the captain and when I plug in my iPhone it loads the same pictures twice each time how it stop loading the same things every day?  I tried to make the old default iphoto but picture still open when I plug in my iPhone?

    I tried to make the old default iphoto but picture still open when I plug in my iPhone?

    When the iPhone is connected and Photos opens, select iPhone in sideba of the windowr of Photos. Then, uncheck the option 'Open for this iPhone Photos' below the toolbar.  Do this for all your iPhones. The hook should be unmarked for each device individually.

  • How to take a column of duplicate names and fill a different column with the same names, excluding duplicates?

    How to take a column of duplicate names and fill a different column with the same names, excluding duplicates?

    I find easier to use this copy separate Automator Service (download Dropbox).

    To install in your numbers > Services, double-click menu just the package downloaded .workflow and if necessary give permissions in system preferences > security & privacy.

    To use, just:

    1. Select the cells in the column with duplicate names.
    2. Choose separate copy in numbers > Services menu.
    3. Click once in the upper cell where you want the deduplicated values appear.
    4. Command-v to paste.

    SG

  • Dvd player MediaSmart not working - when a Blu - ray Disc I'm invited to be updated, but even with the update it will not play.

    I have laptop HP Pavilion dv7-1260.  When I put in a blu - ray disc I'm propmted to get an update.  Even with the update, it will not play a blu ray.

     

    Hello

    ·         What is the update message that you receive?

    ·         Do you have an error message when you try to read the Blu - ray Disc?

    ·         This problem occurs only with Blu ray discs or any of the others?

    To play a Blu - ray disc, you must use a non-Microsoft program that supports Blu - ray playback and a device that can play Blu - ray discs.

    See the link below:

    http://Windows.Microsoft.com/en-us/Windows7/MPEG-and-DVD-video-frequently-asked-questions

  • Querying the oracle table that has a column with the name of "FILE".

    Hi all

    I need to have an oracle table that has the column with the name "FILE".

    I can query all columns with the query "select * from table".

    But I'm not able to use the query "select the table file.

    This table is converted from ms access to oracle and I need to have this column with the name "FILE".

    Any suggestions?

    Thank you

    Abdellaoui

    Hello

    FILE is a keyword from Oracle, so it's not a good name,

    Use FILEDATE, or DATE_FILED, or something else that is not in V$ RESERVED_WORDS. KEYWORD as the name column.

    If you need to use the FILE, then you must use quotation marks.

  • [8i &amp; 10g XE] How to compare a column with the text of a column with numbers

    This is probably a simple question, but I can't seem to find the answer. I tried variations on use to_number to_char and interpreters, all nothing will do.

    The real problem is that I have a table with a column of numbers in a database 8i (datatype = NUMBER (3)) and a column with the text (datatype = CHAR (3)), and I need to compare two values.

    This table has thousands of lines, but here's an example of the data in these two columns:
    TXT     CHR
    -----------
    001     1
    001     2
    002     2
    XXX     1
    003     3
    What I want to do is select the lines where the TXT and CHR VALUES do not match, then it would be (given my example):
    TXT     CHR
    -----------
    001     2
    XXX     1
    But, I'm having difficulties in comparing two columns, because they are not the same type of data.

    I tried to work on a simplified version of the problem in the 8i database both my 10g XE database:
    SELECT     *
    FROM     (
         SELECT     1.000               AS nbr
         ,     TO_CHAR(1.000,'000')     AS txt
         ,     CAST('001' AS CHAR(3))     AS chr
         FROM     dual
         )
    WHERE     txt = chr
    ;
    But this also returns any line, and it isn't even the "XXX" in the text column to treat in this example.

    Can someone help me understand what it is that I'm missing?

    Thank you!

    Hello

    user11033437 wrote:
    This is probably a simple question...

    Simple if you know the secret; maddening if you do not have.

    ... I tried to work on a simplified version of the problem in the 8i database both my 10g XE database:

    SELECT     *
    FROM     (
         SELECT     1.000               AS nbr
         ,     TO_CHAR(1.000,'000')     AS txt
         ,     CAST('001' AS CHAR(3))     AS chr
         FROM     dual
         )
    WHERE     txt = chr
    ;
    

    Try:

    TO_CHAR (1.000, 'FM000')
    

    By default, TO_CHAR leaves place the beginning of the string to a sign less, in which case he should ever one, TO_CHAR (1,000, '000') returns makes 4 characters, not 3. "FM" in the format said TO_CHAR do not add a space.

    If it does not, after the release of DUMP (txt) for the few lines that you can't match, so that we can see exactly what is in them.

  • SVN: #155021. You cannot update this file with the help of Dreamweaver Subversion integration because a new Subversion client application on your machine has made updates to the file Subversion meta data. For more information on this problem

    Can Hello anyone help?

    After you configure Subversion in Dreamweaver, I get this error again!

    SVN: 155021 #. You cannot update this file with the help of Dreamweaver Subversion integration because a new Subversion client application on your machine has made updates to the file Subversion meta data. For more information about this issue, see http://www.adobe.com/go/dw_svn_en.

    even after following the instructions http://www.Adobe.com/go/dw_svn_en Download the extension, python change the var in windows to say «;» C:\Python26'

    with & without quotes, with or without; before C

    command > comparability of Subversion to get the following error "the Conversion process has failed. Please make sure you have Python installed and you check Python PATH parameter'

    I managed to get all the files after the installation, I locked, unlocked and commit a file to test fact so that was all works well, the only part I'm not retrieves the latest version, this is because Subversion is 1.6.2 and dreamweaver must revert to the version 1.4.5 on local to work, the compile someone at - it an idea what to try next in order to make it work?

    Just a reminder!

    1. I configure Subversion through guidelines on http://help.Adobe.com/en_US/Dreamweaver/10.0_Using/WS80FE60AC-15F8-45a2-842E-52D29F540FED. html
    2. I managed to get the latest SVN version
    3. Lock, unlock and commit a file
    4. Installed Python in C:\Python26 change the path in windows system properties > advanced > Environment Variables > system variables > New > Python =; C:\Python26 also C:\Python26
    5. I also tried the same thing in the User Variables
    6. Installed the extension DW Subversion compatibility

    7. Tried to run the compatibly with the command > Subversion comparability in DW

    Welcome any suggestion to solve this?

    Hello

    There has been a lot of problems using svn with dw, and I know many people who have stopped trying to operate correctly.

    As much as I know dw will not work with newer versions of svn (over 1.5), and even then, there are a lot of problems, a possible solution is to try subweaver (at- http://code.google.com/p/subweaver/ ), as this has solved some of the problems associated with the use of tsvn dw environment.

    PZ

  • Columns with the same name but different data or precision length.

    DB version: 10 gr 2

    In a diagram, I need to find all of the columns with the same name but different or precision data length (if the column is of type number). I know I have to use the USER_TAB_COLS view for this. But I need to find a logic to compare the names of columns and its lengths.

    something like

    select t1.table_name
         , t1.column_name
         , t1.data_type
         , t1.data_length
         , t2.table_name
         , t2.column_name
         , t2.data_type
         , t2.data_length
      from user_tab_columns t1
         , user_tab_columns t2
     where t1.table_name != t2.table_name
       and t1.column_name = t2.column_name
       and t1.data_type = t2.data_type
       and t1.data_length != t2.data_length
    

    ... could be a starting point.

Maybe you are looking for