How to update columns with the value of other lines in the same table

Hello

I use Oracle 11.2, I'd use SQL statements to update a column based on values in other rows in the same table. Here are the details:

create table TB_test (number 4 myId, crtTs date, date of MDPU);

insert into tb_test (1, to_date ('20110101', 'YYYYMMDD'), null);
insert into tb_test (1, to_date ('20110201', 'YYYYMMDD'), null);
insert into tb_test (1, to_date ('20110301', 'YYYYMMDD'), null);
insert into tb_test (2, to_date ('20110901', 'YYYYMMDD'), null);
insert into tb_test (2, to_date ('20110902', 'YYYYMMDD'), null);

After you run the SQL code, I would like to have the following result:

1, 20110101, 20110201
1, 20110201, 20110301
1, 20110301, null
2, 20110901, 20110902
2, 20110902, null

Thanks for your suggestion.

I guess you need this, otherwise please explain logic correctly:

SQL> merge into tb_test t
  2  using (
  3    select rowid as rid
  4         , lead(crtts) over(partition by myid order by crtts) as updts
  5    from tb_test
  6  ) v
  7  on (t.rowid = v.rid)
  8  when matched then update
  9   set t.updts = v.updts
 10  ;

5 rows merged.

SQL> select * from tb_test order by 1,2;

      MYID CRTTS     UPDTS
---------- --------- ---------
         1 01-JAN-11 01-FEB-11
         1 01-FEB-11 01-MAR-11
         1 01-MAR-11
         2 01-SEP-11 02-SEP-11
         2 02-SEP-11

Tags: Database

Similar Questions

  • 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

  • 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

  • Comparison of columns in the same Table, with the exception

    Hi all


    I need compare two values of column in the same table. But should ignore a few characters like space, comma, point

    Following should be consistent
    'ABcd f' = 'AbCDf'
    'xyz ..' = 'xy z'
    How is it possible


    See you soon

    Sexy
    with t as
    (
    select 'ABcd f' col1,'AbCDf' col2 from dual union all
    select 'xyz ..','xy z' from dual union all
    select 'xyz ..x','xy z' from dual
    )
    select *
    from t
    where upper(translate(col1,'a ,.','a')) =
               upper(translate(col2,'a ,.','a'));
    
    COL1    COL2
    ------- -----
    ABcd f  AbCDf
    xyz ..  xy z  
    

    Published by: JAC on 4 February 2013 17:22

  • Update the same column in the same table

    Hello

    How do update you a records of column in the same table?

    I have purchase_order of the table. Consists of column ID, color, Purchase_No, Sub purchase.


    < pre >

    Create table Purchase_Order)
    Identification number,
    color varchar2 (10),
    purchase_No varchar2 (5).
    purchase_sub varchar2 (2));
    < / pre >

    < pre >

    ID color Purchase_No purchase Sub
    6416 S1406 PURPLE 3
    6415 S1406 GREEN 2
    6414 S1406 GREEN 1
    6419 S1406 3
    6417 S1406 1
    6418 S1406 2

    < pre >

    6 unique ID records is in the same site. But 2 ID will be confined to a purchase_sub.

    For example

    6416,6419 purchase_no S1406 but has the same purchase_sub who need 3.I 6419 color "Purple".

    Expected results:
    < pre >

    ID color Purchase_No purchase Sub
    6416 S1406 PURPLE 3
    6415 S1406 GREEN 2
    6414 S1406 GREEN 1
    6419 S1406 PURPLE 3
    6417 S1406 GREEN 1
    6418 S1406 GREEN 2

    < / pre >

    Thank you!

    Published by: CrackerJack on May 4, 2009 15:04

    Hello

    Try this,

    UPDATE Purchase_Order a
       SET a.Colour = (SELECT colour
                         FROM Purchase_Order b
                        WHERE a.Purchase_Sub = b.Purchase_Sub
                          AND b.colour Is Not Null)
     WHERE a.Colour Is Null
    

    Kind regards
    Christian Balz

  • Make a column based on another value of the column in the same table

    JDev 11.1.1.6.0

    It may be a silly question but I'm stuck

    I need to conditionally return a column that said Condition is as if the value in the other column of the same table B is equal to F. I should make A column only when this condition is met. I tried the following code:

    < af:column sortProperty = "PhoneNumber1."
    sortable = "false".
    headerText = "#{bindings." A.hints.PhoneNumber1.label}.
    ID = "c146.
    rendering = "#{rank." PhoneNumber1ResponseFlag eq 'F'} ">"
    < af:outputText value = "#{rank." PhoneNumber1}.
    ID = "ot130" / >
    < / af:column >
    < af:column sortProperty = "PhoneNumber1ResponseFlag."
    sortable = "false".
    headerText = "#{bindings." B.hints.PhoneNumber1ResponseFlag.label}.
    ID = "c80" rendered = "true" >
    < af:outputText value = "#{rank." PhoneNumber1ResponseFlag}.
    ID = "ot129" / >
    < / af:column >

    The data presented in the table for the PhoneNumber1ResponseFlag column are F. Still my condition does not work.

    Well, I've finally sorted it myself by managed bean.

    Here are the steps I followed:

    1. Wrote a new bean managed.
    2. Added a Boolean variable called flag and made to true when the "'PhoneNumber1ResponseFlag ' current line attribute is equal to F."
    3. Changing the property made column on this indicator.
  • How to use photoshop with the same account on more than one laptop?

    How to use photoshop with the same account on more than one laptop?

    a single user license entitles you to two facilities simultaneous/activations.  so, what's the problem?

  • Can we have hash partition and list on 2 columns in the same table. How.

    Can we have hash partition and list on 2 columns in the same table. How.

    Work on 10g release 2

    # List-Hash is a valid composite partitioning strategy 11 from g.

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

  • Question: Is foreign key references to a different column in the same table

    Hello

    I'm not grasp this concept. Would you be able to provide the logic how and why you need to create a foreign key that references a column of the same table?

    For example, referencing the schema, SupervisorNo @ here is a FOREIGN KEY to the "EmpNo" column in the same table.

    EMPLOYEE (EmpNo, title, Fname, Lname, phone, Email, date of birth, sex, salary, HireDate, OutNo, SupervisorNo @)


    Why would you want to do this and how can it be used? If you are filling out the data in the table, and this constraint is enabled, you will not be able to load whatever it is because the "EmpNo" column will be empty.


    Any input you can provide me with this would be greatly appreciated. Thank you!



    His name

    Self-referential integrity constraints

    Lets say that we have the table emp with emp_no and mgr_no columns.

    Now all managers should also be an employee too. How can I check this when inserting data into this table? I applied auto integrity constraint. It would help me to make sure no handler (doesn't mean no mgr_no) must be registered if this Manager is not an employee of this organization. Which means, I won't be able to enter any mgr_no in the column until and unless that the same employee also is an employee (emp_no from the same table).

    Check the link as well below.

    Data integrity

    Thank you

    Ishan

  • the row_number() value another column in the same table

    Hi all
    I have NODE_DETAILS which has following of table columns

    MSLINK
    NODE_ID
    RNUM

    I update RNUM with row_number() value in ordering the mslink. I tried after update statement

    Update)
    Select / * + bypass_ujvc * / rnum has, row_number() over (order of mslink) NODE_DETAILS rrnum
    )
    set a = rrnum

    But he throws ORA-01732: non-legal data manipulation operation on this display error

    How can I update row_number() rnum column?

    Thank you
    Sujnan

    You can use MERGE

    SQL> create table node_details
      2  (mslink number
      3  ,node_id number
      4  ,rnum number
      5  )
      6  /
    
    Table created.
    
    SQL>
    SQL> insert into node_details
      2  select -rownum, rownum, null
      3    from all_objects
      4   where rownum <= 5
      5  /
    
    5 rows created.
    
    SQL>
    SQL> select *
      2    from node_details
      3  ;
    
        MSLINK    NODE_ID       RNUM
    ---------- ---------- ----------
            -1          1
            -2          2
            -3          3
            -4          4
            -5          5
    
    SQL>
    SQL> merge into node_details n
      2  using (select rowid rid,rnum a, row_number() over (order by mslink) rrnum from NODE_DETAILS) v
      3  on (v.rid = n.rowid)
      4  when matched then
      5     update set rnum = rrnum
      6  ;
    
    5 rows merged.
    
    SQL>
    SQL> select *
      2    from node_details
      3  ;
    
        MSLINK    NODE_ID       RNUM
    ---------- ---------- ----------
            -1          1          5
            -2          2          4
            -3          3          3
            -4          4          2
            -5          5          1
    
    SQL>
    SQL> 
    
  • place a select query calculation in a different column in the same table

    How can I put my calculation result in a column named within the same table?

    I have a table called: dgpercentagedatachart

    I use the columns of this dgpercentagedatachart: totalcecrating divided by lowestfeederrating times 100 to get the percentage

    In the query, I gave the result the Alias of the cal

    What I want is to put this result in my application or my calculation (in percentage) in my column "percent" on my table of dgpercentagedatachart vacuum.

    How can I configure this syntax?

    This is the select query, I came with:

    Select dgpercentagedatachart.totalcecrating, dgpercentagedatachart.lowestfeederrating,.

    100.00*dgpercentagedatachart.totalcecrating/dgpercentagedatachart.lowestfeederrating as cal

    of dgpercentagedatachart;

    Here are the results:

    CAL lowestfeederrating Totalcecrating

    8,978 7.48166666666666666666666666666666666667 120

    30.951 25.7925 120

    5.04                         120                          4.2

    Hello

    2685870 wrote:

    How can I put my calculation result in a column named within the same table?

    I have a table called: dgpercentagedatachart

    I use the columns of this dgpercentagedatachart: totalcecrating divided by lowestfeederrating times 100 to get the percentage

    In the query, I gave the result the Alias of the cal

    What I want is to put this result in my application or my calculation (in percentage) in my empty column '%' on my table of dgpercentagedatachart .

    How can I configure this syntax?

    This is the select query, I came with:

    Select dgpercentagedatachart.totalcecrating, dgpercentagedatachart.lowestfeederrating,.

    100.00*dgpercentagedatachart.totalcecrating/dgpercentagedatachart.lowestfeederrating as cal

    of dgpercentagedatachart;

    Here are the results:

    CAL lowestfeederrating Totalcecrating

    8,978 7.48166666666666666666666666666666666667 120

    30.951 25.7925 120

    5.04                         120                          4.2

    To change an existing column in a table, you can use the UPDATE or MERGE, instructions like this:

    UPDATE dgpercentagedatachart

    Percentage of VALUE = 100,00 * totalcecrating

    / lowestfeederrating

    ;

    Noramlly, tables is not redundant columns like this.  If percent can always be calculated from other columns, then it is probably better to calculate at run time and store it in the database, where you will have to be updated each time the columns it depends on change.  You can use a view to avoid having to encode them the calculation.

    If you really don't want a column that can be calculated in the same lines other columns, then use a virtual column (if you use Oracle 11 or higher).

    I hope that answers your question.

    If this isn't the case, please post a small example of data (CREATE TABLE and only relevant columns, INSERT statements) for all of the tables involved and also publish outcomes from these data.

    If you ask on a DML statement, such as UPDATE, the sample data will be the content of the or the tables before the DML, and the results will be the State of the or the tables changed when it's all over.

    Explain, using specific examples, how you get these results from these data.

    Always say what version of Oracle you are using (for example, 11.2.0.2.0).

    See the FAQ forum: Re: 2. How can I ask a question on the forums?

  • Copy the value1 to value2 column column in the same table

    Hi all

    If I want to update the values in column 2 stack with stack column. It's the same table 'Data_GU '.


    RACK_ID STACK STACK2
    S1009 706161522 S8322
    S1009 706168051 S8322
    S1009 706116417 S8322
    S1009 706126698 S8322

    I came with

    UPDATE DATA_GU C
    SET STACK2 =)
    SELECT battery
    OF DATA_GU B
    ) ;

    I have an error returns unique lines and more than one line... How to on this subject. Thank you guys.
    UPDATE DATA_GU
    SET STACK2 = STACK
    /
    

    SY.

  • How to create a LOV the same table in the adf.

    Hi all

    I have a simple table of database with over any dependant on other tables. In my table one of my name of the column as "Gander".
    Since I have given in table mod "or F. What is my requirement in simple with table search page that I want to search with Gander field also with other areas.
    while I want to show that gender as LOV in the search panel.
    I tried with when we have a relationship with some other table.but how to create LOV to field when the field in the same table only.

    as M or F even use cases or not like.


    Concerning
    Siva

    Siva,

    What is your version of JDev? What technology you are using (BC4J?)?

    Assuming you are using BC for the model, you tried to create another object view based on query something like

    select distinct gender, gender from your table
    

    and then use this VO for LOV?

    Arun-

  • Conflicts of SQL columns on the same table

    I have a table with two columns used for the same purpose. Examples (start_date and career_history_start) (end_date or career_history_end).
    The columns have different (varchar2 and date) data types. The developer has used the data type date (start_date) to write the report and of the use of time even the vachar2 to manully insert date. We decided to use a single (varchar2). Meanwhile, there are differences in the two columns. We will find the difference between the two columns start_date (date) and career_history_start (varchar2).

    No idea what query will accomplish this considering they gave different type on the same table?

    Here is the description of the table:

    career_history / / DESC
    Name of Type Null
    --------------------------- -------- -------------
    CAREER_HISTORY_ID NOT NULL NUMBER (38)
    PERSON_ID NUMBER (38)
    CAREER_HISTORY_START VARCHAR2 (50)
    CAREER_HISTORY_END VARCHAR2 (50)
    CAREER_HISTORY_DESC VARCHAR2 (250)
    CAREER_HISTORY_RECNUM NUMBER (38)
    Start_date DATE
    End_date DATE
    CAREER_HISTORY_ACTIVE_FLAG CHAR (1)
    CAREER_HISTORY_DELETED_FLAG CHAR (1)
    CAREER_HISTORY_UPDATE_TIME TIMESTAMP (6)
    CAREER_HISTORY_UPDATED_BY VARCHAR2 (50)
    NUMBER OF SORT_NUM

    The customer doesn't care how the data is stored in the database. He care how it is displayed.

    What happens if your client has another office in a zone schedule differnet and each office needs display their time locally, but the server must store all the time in the same zone. How do they manage that when storing dates as a varchar data type. You get that kind of built-in functionality when you use a date data type

    In its simplest form if the customer is always display the data as a string, and then create a view from top of the table and convert dates in any format the customer prefers and that expose to the client.

    In this way, you get data stored properly and the client allows you to view data in the way they prefer

Maybe you are looking for