I have a "Master" table that I am trying to apply a table of changes to. The "Master" is a table is from a point in time from the past. Changes that have been made to the "Master" since that time are listed in "Changes", and I am given the record number, the name of the field that needs to be updated, the new value of the field, and the date/time it was changed. If I cannot take the latest change, that is not an issue, as I can just change the table so that it only has the latest update. My issue is getting a query to update a field in a record, where I am getting the field name to update from a different table.
Any ideas? I started my code off hard coding equations but wonder if there is a dynamic way to do this.
UPDATE MASTER
INNER JOIN MASTER b
ON a.Id = b.Id
SET b.description = a.New_Value
WHERE a.change_field = "Description"
......(then I need it to do the same for any instance of one of the other 3 fields)
;
Table "MASTER":
Id | Description | Brand | Cost | Mapping
--------------------------------------------------
123 | A Desc | Any | 0.01 | Candy
456 | B Desc | Old | 0.02 | Baking
789 | C Desc | Brand | 0.03 | Season
Table "CHANGES":
Id | Change Field | New Value | Change Time
-----------------------------------------------------------
123 | Description | AA Desc | 01:15:00
123 | Description | AAA Desc | 12:15:00
456 | Brand | Good | 01:15:00
456 | Brand | Best | 12:15:00
789 | Cost | 0.44 | 01:15:00
789 | Mapping | Salt | 01:15:00
789 | Mapping | Sugar | 12:15:00
Table "MASTER", updated with "CHANGES":
Item Code | Description | Brand | Cost | Mapping
--------------------------------------------------
123 | AAA Desc | Any | 0.01 | Candy
456 | B Desc | Best | 0.02 | Baking
789 | C Desc | Brand | 0.44 | Sugar
Many thanks!
What I would do if there is no enough time, Copy/export all rows from CHANGES
table to excel sheet. Then build query there on excel using CONCATENATE
formula, Check snap below.
Once your formula is perfect, just drag it up-to all rows below.
Then just copy paste the whole bunch from the Excel column to your query browser and execute it.
One more thing : if you want to update with latest value by time (in your case there are 2 rows for the description
, branch
and mapping
fields with different Change Time
) then you need to copy only those records which needs to be processed. eg While selecting the records from the Changes
table you need to use group by Change Time
and Max
You also need to handle other data type then string with slight change when you generate formula - i mentioned on the snap.
Let me know if it does not help or you need more help.
Mark it as answer if it helps.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.