Finding Updated Columns with Values in Rows

Hello,
I have a one requierment as below.
if I have Table ITEM with Below records
ParcelID 
ItemCode
CurrentLocation
Destination
UpdatingTime
1010         
GLS
ABC
XYZ
01-12-14
1015         
PHM
SSS
ZZZ
01-12-14
If any of the column value is updated it will replicate to ITEMUPDATE table with old row and new row as well.
Suppose for ParcelID 1010 CurrentLocation Changed from ABC to CDE it will store in ITEMUPDATE table with both records
old and new and if ParcelID 1015 CurrentLocation Changed from SSS to KKK it will also store as below.
Now ITEMUPDATE table will have all records old as well as new rows as below.
ParcelID
ItemCode
CurrentLocation
Destination
UpdatingTime
1010      
GLS
ABC
XYZ
01-12-14
1010      
GLS
CDE
XYZ
02-12-14
1015      
PHM
SSS
ZZZ
01-12-14
1015      
PHM
KKK
ZZZ
01-12-14
Like this any column can update not only CurrentLocation.
Now the requirement is I want to know which columns is updated and what is the new value.
Some what results need to look like below.
ParcelID
Old_CurrentLocation
New_CurrentLocation
1010     
ABC
CDE
1015     
SSS
KKK
Please Anyone Can help me on this .
Thank You,
Avis

Hello Avis,
You need to store date and time in 'UpdatingTime' column in both ITEM and ITEMUPDATE tables.
If you store only date, you will not get desired results as there may be multiple records for a 'ParcelID' in the same date in ITEMUPDATE table. It is difficult to get the latest record from multiple records with same dates for a 'ParcelID' from ITEMUPDATE.
Alternatively you can create a surrogate key   , like 'ItemUpdateID', in ITEMUPDATE from which we can get the latest record for a 'ParcelID'. The surrogate key can be a integer identity column so that automatically incremented and updated for each insert
in ITEMUPDATE. Then it is easy to get latest UpdatingTime for a ParcelID from ITEMUPDATE  table.
If the table design and data is something as below:
ITEM table
ParcelID    ItemCode    CurrentLocation    Destination    UpdatingTime
1010         GLS                ABC                     XYZ              
2014-01-12
1011         MMX               ABD                     XVB  
            2014-01-12
1015        PHM                SSS                     ZZZ               
2014-01-12
ITEMUPDATE table
ItemUpdateID    ParcelID    ItemCode    CurrentLocation    Destination    UpdatingTime
1                         1010         GLS                
ABC                     XYZ          2014-01-12
2                         1015         PHM                SSS             
        ZZZ          2014-01-12
3                         1015         PHM               
KKK                      ZZZ          2014-02-12
4                         1010         GLS                
CDE                      XYZ          2014-02-12
5                         1011         MMX               
ABD                      YYY           2014-01-12
Here 'ItemUpdateID' is identity column.
The SELECT query can be written as below:
SELECT A.ParcelID, A.ItemCode,
B.CurrentLocation 'New_Location',
ISNULL((SELECT CurrentLocation FROM ItemUpdate WHERE ItemUpdateID = (SELECT MAX(D.ItemUpdateID)
FROM ItemUpdate D WHERE D.ItemUpdateID < B.ItemUpdateID AND D.ParcelID = B.ParcelID)), A.CurrentLocation) 'Old_Location'
FROM Item A
LEFT JOIN ItemUpdate B On B.ParcelID = A.ParcelID
AND B.ItemUpdateID = (SELECT MAX(ItemUpdateID) FROM ItemUpdate C WHERE C.ParcelID = B.ParcelID)
The result will be as follows:
ParcelID    ItemCode    New_Location    Old_Location
1010          GLS              CDE                   ABC
1011          MMX             ABD                    ABD
1015          PHM             KKK                     SSS

Similar Messages

  • How to find if COLUMN DEFAULT VALUE is stored as metadata?

    Hello,
    I'm using Oracle 11g enhanced ADD COLUMN Functionality. Adding new columns with DEFAULT values and NOT NULL constraint no longer requires the default value to be stored in all existing records.
    Sometimes we change DB columns from NOT NULL with DEFAULT to NULL with DEFAULT. This operation "materialize" column default.
    Is there an easy way (Dictionary view) how to find, that COLUMN default value is stored as metadata or is "materialized" ?
    Thanks. Filip
    Oracle RDBMS version : Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production

    Thanks for your suggestion, but it is not what i'm looking for :-(
    I don't need to find the default value, i need to know how is default value stored. It could be stored in 2 ways.
    1. "materialized" - prior to 11G (value is physicaly stored for every column)
    2. "as a metadata" - new 11G functionality (default is not physicaly stored and if you query the column DB transalte NULL value to defaut value)
    Now I would like to now if my column is type 1) or 2). How can I do it?
    Thank you.Filip

  • UPDATE value in column with value in SAME TABLE

    Hi all,
    Here's my issue...
    I have a table which records all incidents for a person.
    The table looks something like this:
    tbl_connect(person_Id NUMBer,
    OLD_ID VARCHAR2(24),
    CASE_NUMBER     VARCHAR2(10),
    CASE_TYPE     VARCHAR2(10),
    PERSON_ROLE     VARCHAR2(30),
    INCIDENT_TYPE     VARCHAR2(40));
    The table is populated from a source table with all fields except person_id. Person_id is a sequence number that gets generated if the person comitting the incident is a NEW PERSON to our system. OLD_Id is the unique identifier between the source table and tbl_connect to identify a person.
    The problem: If an existing person commits a new incident, a new record will be inserted into tbl_connect without PERSON_ID. Since the person already is in the database the person_id already exists in tbl_connect for that person. I now need to UPDATE person_id column with the person_id that already exists for this person. How can i achieve this.
    Ive been trying all sorts of update queries but nothing seems to work. ANy help will be appreciated. Thanks in advance.

    Frank,
    Thanks for the speedy reply. Here is a sample table.
    I know it's a bit confusing. The data that is being dumped into this table is information from a old system. The OLD_ID is in here because it is the relationship between this table and the old table. PERSON_ID is new to this system to identify a person uniquely. That is why i need them both in this table for right now.
    So say my tbl_connect got populated with new info from the old table. As you can see, old_id=567A has comitted another incident. Since he already exists in tbl_connect, now i need to update person_id with the value of 1. Does that make sense now????? If old_id did not exist, all i would do is insert a new row and set person_id to the next sequence number.
    TBL_CONNECT
    PERSON_ID OLD_ID      CASE_NUMBER CASE_TYPE          
    1     567A     12345          IR          
    1     567A     15236          MV
         567A     98547          IR<--newly inserted record of same person

  • Update column with ROW_NUMBER() value

    I have a column that I would like to update with a ROW_NUMBER() value partitioned by a specific column.
    CREATE TABLE ##tmp (id INT NULL,
    value1 VARCHAR(10),
    value2 VARCHAR(10))
    INSERT INTO ##tmp (value1,
    value2)
    VALUES ('A', 'asdfasdf'),
    ('A', 'asdf'),
    ('A', 'VC'),
    ('B', 'aasdf'),
    ('C', 'sdfgs'),
    ('C', 'xdfbhsdty'),
    ('C', '23sdgfg'),
    ('C', '234')
    -- update the ID column with the values below
    SELECT ROW_NUMBER() OVER (PARTITION BY Value1 ORDER BY Value2),
    Value1,
    Value2
    FROM ##tmp
    I have 14 million records.  Can someone explain the best way to do this?  Does the ORDER BY destroy performance for this many records?
    Thanks!

    CREATE TABLE ##tmp (id INT NULL,
    value1 VARCHAR(10),
    value2 VARCHAR(10))
    INSERT INTO ##tmp (value1,
    value2)
    VALUES ('A', 'asdfasdf'),
    ('A', 'asdf'),
    ('A', 'VC'),
    ('B', 'aasdf'),
    ('C', 'sdfgs'),
    ('C', 'xdfbhsdty'),
    ('C', '23sdgfg'),
    ('C', '234')
    -- update the ID column with the values below
    ; with cte as (
    SELECT ROW_NUMBER() OVER (PARTITION BY Value1 ORDER BY Value2) as RowNumber,
    Value1,
    Value2
    FROM ##tmp
    update cte
    set Id = RowNumber;
    Russel Loski, MCT, MCSE Data Platform/Business Intelligence. Twitter: @sqlmovers; blog: www.sqlmovers.com

  • Update column with parent child relationships

    Hi,
    i need to update a column with parent child relationship.
    Please provide the possible queries..
    Gobi..

    Hi,
    Depending of the objective and the size of the tables, personally, for maintenance purposes, I preferred duplicate the parent with the new column value and duplicate the child records with this same new value. Other alternative is disable the foreign key temporarily.
    You can take a look on this link below too:
    http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:5773459616034
    Cheers

  • Script to find report columns with XXS vulnerabilities

    Hi,
    Report columns with display as "Standard Report Column" are vulnerable to Cross Site Scripting attacks. I have written a simple script to find these report columns and described an easy way to change them all at once. Check: http://wiki.shellprompt.net/bin/view/Apex/XssExamples
    Regards Pete

    Hi Pete,
    good that you bring up that security issue!
    I was talking to Joel during ODTUG about the "Standard Report Column" default the wizard uses and he said that this might be changed in the next version to use the more secure "Display as Text (escape special characters, does not save state)"
    Patrick
    My APEX Blog: http://inside-apex.blogspot.com
    The ApexLib Framework: http://apexlib.sourceforge.net
    The APEX Builder Plugin: http://sourceforge.net/projects/apexplugin/

  • Update A Column with value of Column in the next Row

    Hi All,
    I have a table which is like this
    ID NUMBER,
    SUB_ID NUMBER,
    STARTDATE DATE.
    The Value is say for example like this:
    ID     SUB_ID     StartDate
    1     1     1-JAN-09
    1     2     20-FEB-09
    1     3     1-MAR-09
    2     1     10-JAN-09
    2     4     10-APR-09
    2     3     1-MAR-09
    2     2     2-FEB-09
    I have to add a new column END DATE in the table and populate a value which is One Day less than the value of
    start Date for next row. I have to order by ID and SubID and so that colum looks like this.
    ID     SUB_ID     StartDate          END DATE
    1     1     1-JAN-09          19-FEB-09
    1     2     20-FEB-09          28-FEB-09
    1     3     1-MAR-09          DEFAULT-END DATE
    2     1     10-JAN-09          1-FEB-09
    2     2     2-FEB-09          8-Mar-09
    2     3     9-MAR-09          9-APR-09     
    2     4     10-APR-09          DEFAULT END DATE
    Can this be achieved using a single update query or I have ot write a Procedure ...any help will be appreicaited

    or
    merge into test
    using (select rowid rid
                , id
                , sub_id
                , startdate
                , lead (startdate) over (order by id, sub_id) - 1 ed
           from test) x
    on (x.rid = test.rowid)
    when matched then
       update set end_date = x.ed
    ;

  • Dynamic update of column with values derived by formula on other table.

    i have 3 tables; table A,B and C
    table A has two columns (id and Quantity), based on the id value, the quantity value is computed using a formula on two rows (one from table B and the other from C).
    the formula is different for each id value.
    but after every month the quantity value has to be updated (because tables B & C get updated monthly).
    example. given id=1, quantity= select round((sum(h.col1)/sum(p.col1), 2) from B p, C h
    given id=2, quantity=select round((sum(col1)/sum(col3)), 2) from C
    so for every id the quantity is derived by a different formula, but the quantity value has to be recomputed every month.
    need help.

    1. update table a
    quantity = decode (id,1, <formul1>, id,2 , formula 2...)
    2. if you wanto schedule this as monthly task
    use dbms_job.submit
    send the above query to "What" parameter.

  • Update Nulls With Value from Another Row

    Good Morning,
    I word for a car manufacturer. I am pulling an excel spreadsheet into SQL 2012.The spreadsheet lists one line for each dealer, each carline. So if there are 6 carlines and 65 dealers there will be 390 lines. when the spreadsheet gets to me it looks like
    this:
    Dealer          
    Carline         District         Data         Data         Data
    East Side Craworld Model1
    1 0
    0
    Model2
    1 0 0
    Model3
    1 0
    0 0
    So the Dealer is only listed once. When I pull this into SQL the blank lines are nulls and I want them to be the Dealer name in there. How can I accomplish this with an update.
    Just one thing more. When I programmed in access, I would loop through a table and set a variaiable =Dealer name and not change the variable until the next not null row and then make that the new variable all the while making nulls=the variable. Is there
    a way to accomplish that here.

    The only issue is this file will eventually be a feed and an ftp agent will grab the feed place it somewhere and and there will be a stored package to import it.
    Can I do an update on a null field with the field from the previous record, or am I dealing witht he ordering thing again. I found this code. Will it work?
    select Date
    , GroupId
    , case
    when Comment is not null then Comment
    else
    select Comment
    from YourTable yt2
    where yt1.GroupId = yt2.GroupId
    and yt2.Comment is not null
    and yt2.Date < yt1.Date
    and not exists
    select *
    from YourTable yt3
    where yt1.GroupId = yt2.GroupId
    and yt2.Comment is not null
    and yt3.Date < yt2.Date and yt2.Date < yt1.Date
    ) end as Comment
    from YourTable yt1

  • Update column with subquery returning more than one row

    Hi Everybody,
    Please let me know how to handle this. I am writing update statement in procedure with subquery and it is returning multiple rows. Please help me, how i can handle this :
    UPDATE TABLEA A
    SET A.ERROR_MESSAGE = (Select B.XERROR_MESSAGE from TABLEB B, TABLEA A WHERE B.id = A.id)
    WHERE A.id = (Select B.id from TABLEB B, TABLEA A WHERE B.id = A.id);
    (Select B.XERROR_MESSAGE from TABLEB B, TABLEA A WHERE B.id = A.id) --- This subquery is returning more than one rows. How i can handle this in Pl/SQL?
    Please let me know. I will be very thankful to you all.
    I will really appreciate your replies and comments.
    Thank you

    Try getting rid of tablea in your subqueries. You already have it in the UPDATE statement.
    UPDATE TABLEA A
    SET    A.ERROR_MESSAGE = (Select B.XERROR_MESSAGE
                              from   TABLEB B
                              WHERE  B.id = A.id
    WHERE  A.id = (Select B.id
                   from   TABLEB B
                   WHERE  B.id = A.id);You can also try a simple MERGE:
    merge into tablea a
    using tableb b
    on    (a.id = b.id)
    when  matched then update
          set a.error_message = b.xerror_message;

  • Update a column with value of column of other table

    Hi,
    I am looking for an update statement for the following scenario
    With following 2 tables
    TABLE1 (Primary A, B)
    TABLE2 (Secondary C , D).
    C Column is not unique.
    Please provide me an update statement to update TABLE1.B with TABLE2.D where TABLE1. A = TABLE2.C
    Please note that TABLE2.C is not unique.
    Please let me know whether this satement is correct.
    UPDATE TABLE1
    SET B = (SELECT DISTINCT D FROM TABLE 2 WHERE A=C )
    Thanks
    Alla Kishore

    UPDATE TABLE1 T1
    SET T1.B = (SELECT DISTINCT D FROM TABLE2 T2 WHERE T2.C=T1.A )

  • Filling a column with value from other columns

    Hello all!
    Suppose i have the following table:
    TableA(col_one int, col_two int, result int);I want to insert value to col_one and col_two, and result will be filled with the multiplication product of col_one and col_two.
    ok firstly i know this is very redundant way of storing data and generally should not be used, but one case when this might come in handy is when we want to store a total payment amount of product price and quantity. this can be useful since product price can change overtime and we need to record the data as the transaction happens to avoid change in total payment over time (as the price drops or rises).
    now with the problem:
    I tried this in mysql and it works:
    INSERT INTO TableA (col_one, col_two, result) VALUES (30,  20, col_one * col_two);but not in oracle, which output the following error:
    >
    ora 00984 column not allowed here
    any idea on how to do it in oracle?
    thank you very much :)
    ps, thank you for the fast replies on my previous topic.

    If the insert is done inside PL/SQL you can return the primary key for the row (assuming the table has a primary key?!?) and then update based on that key, but this still introduces two statements where a single statement can suffice.
    Why are you thinking so complicated? ;-)
    If it is done in PL/SQL the you can do this:
    create procedure addRow(p_value1 number,p_value2 number) is
    p_result number;
    begin
    p_result:=p_value1*p_value2;
    insert into table1 (col_one,col_two,result) values(p_value1,p_value2,p_result);
    commit;
    end;If you want to do this automatically when someone inserts a row (without providing the result) you have to use an insert trigger.
    Dim

  • Unable to find the column pay value and monthly salary in element entries

    hi,
    iam unable to find the payvalue and monthly salary in element entries form.
    people enter and maintain> assignment > entries >entry values..
    please tell me the table and column where the payvalue and monthlly salary is going to get populate.
    Thanks,
    vijetha.c

    It seems that you're using an application to connect to an Oracle database. It's general Oracle Database Forum, so you should put your question under correct topic
    Kamran Agayev A. (10g OCP)
    http://kamranagayev.wordpress.com
    [Step by Step install Oracle on Linux and Automate the installation using Shell Script |http://kamranagayev.wordpress.com/2009/05/01/step-by-step-installing-oracle-database-10g-release-2-on-linux-centos-and-automate-the-installation-using-linux-shell-script/]

  • BO: separate column for + - sign apart from column with value

    Hello
    In Business Objects Web Intelligence I would like to create a column that shows the sign '+' or '-' when a key figure KF1 is below 0 or above. How do I do this? It has to be a separate column. I already created a calculated key figure KF2 that has as formula ABS (KF1). KF1 itself must not be shown as contains amount and sign in one field. I need to separate these 2.
    Regards
    Arjan

    You can create one more variable with the formula:
    =If([Key FIG 1]<0) Then "-" Else "+"
    Regards,
    Rohit

  • Column with values separted by commas

    I have a column which brings back a series of numbers (as text) separated by commas e.g.
    20145, 58896, 52369, 88745
    I want to search this column and identify when there is a certain combination of these 5 digit numbers e.g.
    if columncontents equal any of (12345,23445,34567) AND columncontents equal any of (88965,63258,36894) then say hi (or whatever)
    Envisioning a formula in a separate column which evaluates the contents of the column in  question and then turns RED or I insert some text.
    Thank you so much!

    The suitable formula should be like this
    =(Match([Column];"12345") or Match([Country];"23445") or Match([Country];"34567") )
    the above formula returns a TRUE for a match found. and a false other wise. then use alerters on the column to make it red or what ever.
    Thanks,
    Karthik

Maybe you are looking for

  • Formatting the write to meaurement file

    I would like to format the write to measurement file.vi  in such a way so that I get the format as shown in the attached datalogger.txt. I am currently getting lot of unnecessary information as shown in data.xls file. Any help is appreciated. Mozill

  • Changing colour of Available capacity Line Graph in DIMSPPB_BARCHART_DEMO5

    Hi all experts, I am facing a problem in changing the colour of Available capacity Line Graph in program DIMSPPB_BARCHART_DEMO5. Currently it appears as a blue line. Can someone please help me in changing the colour of this graph to any colour I want

  • After trial I don't want Photoshop CC, how do I get my old purchased version of Lightroom 5 to work?

    I previously purchased Photoshop CS6 and Lightroom 5 as stand alone software. I tried Photoshop CC for the trial month, which has passed. I am able to use my old Photoshop CS6, but when I try to use my old, purchased version of Lightroom 5 I just get

  • How t generate random numbers at everytime of exceutuion of the program

    dear friends i am facing a problem that the random numbers generated at time of each exectuion of the program are not the same, i want to generate the same random numbers every time time i run the program. i need your help. i am giving the code in c+

  • SOLAR_EVAL report variants

    Hello all, We are on EHP1, SP20 and are having an issue with report variants for SOLAR_EVAL.  I've created multiple report variants for the Change Request Management report option, and they are all listed in SOLAR_EVAL underneath "Change Request Mana