-1

I have two tables, one with units and mileage (unit field is unique: one row per unit):

Table A (unit master)

unit mileage
1 0
2 0
3 0

And another table with units and mileage (multiple rows per unit):

Table B (unit history)

unit mileage
1 100
1 140
2 102
2 150
3 107
3 129

I am trying to construct a sql to populate the A.mileage field with the MAX B.mileage for each unit.

update 
A
join B
on A.unit = B.unit
join ( 
  select max(mileage) as max_mil, unit
  from B
  group by unit
)maxTable 
on maxTable.max_mil = B.mileage
and maxTable.unit = B.unit
set A.mileage = B.mileage;

Edit: To clarify, I got a 104 error (originally I made a typo and said 107). A 104 is just an 'invalid token' it looks like it had an issue with the first join and was expecting a 'set' clause after the update line. Another comment asked me to define my question a little better. Unfortunately, I realize it is a little vague. My question is the following: how to I modify/rewrite the above sql statement so I can accomplish my goal as described above.

Sarah
  • 1
  • 3
  • Please ask 1 specific researched non-duplicate question. Please either ask re 1 bad query/function with obligatory [mre], including why you think it should return something else or are unsure at the 1st subexpression that it doesn't give what you expect or are stuck, justified by reference to authoritative documentation, or ask about your overall goal giving working parts you can do with justification & ideally a [mre]. Then misunderstood code doesn't belong. But please ask about unexpected behaviour 1st because misconceptions get in the way of your goal. [ask] [Help] Basic questions are faqs. – philipxy Mar 21 '23 at 12:53
  • 1
    What does a 107 error mean? – jarlh Mar 21 '23 at 12:58
  • Please before publishing look at the formatted version of your post. (But an editor bug allows tables without a blank line before.) Please read the edit help & advanced help re block formats for code & quotes, etc. A [mre] includes cut & paste & runnable code including initialization; desired & actual output (including verbatim error messages); tags & versions; clear specification & explanation. For SQL include DDL & tabular initialization code. For debug that includes the least code you can give that is code that you show is OK extended by code that you show is not OK. [ask] [Help] – philipxy Mar 21 '23 at 12:58
  • Please before considering posting: Pin down code issues via [mre]. Read manuals/references & google error messages & many clear, concise & precise phrasings of your question/problem/goal, with & without your particular names/strings/numbers, 'site:stackoverflow.com' & tags; read many answers. SO/SE search is poor & literal & unusual, read the help. Google re googling/searching, including Q&A at [meta] & [meta.se]. [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/q/261592/3404097) [ask] [Help] Reflect research in posts. – philipxy Mar 21 '23 at 13:25
  • Please don't insert "EDIT"s/"UPDATE"s, just make your post the best presentation as of edit time. Please don't leave earlier things in that are not necessary for the current version. Please avoid social & meta commentary in posts. Eg don't say that something is your question, just ask the question. Please act on the earlier feedback. PS Finding the max per group & updating one column from another are both faqs. Where are you stuck? PS If that code doesn't return what you expect, why aren't you debugging your expectations of the language or reasoning before giving up on your goal? – philipxy Mar 21 '23 at 13:27
  • [Fetch the row which has the Max value for a column](https://stackoverflow.com/q/121387/3404097) – philipxy Mar 21 '23 at 13:28
  • Does DB2 support the non-standard UPDATE FROM syntax? – jarlh Mar 21 '23 at 13:33
  • @jarlh DB2 LUW does, DB2 for IBMi and DB2 for z/OS don't – nfgl Mar 21 '23 at 14:39

2 Answers2

1

Try with a merge statement :

merge into a using (
  select unit, max(mileage) max_mileage from b group by unit
) as maxb on a.unit = maxb.unit
when matched and a.mileage is distinct from maxb.max_mileage
  then update set a.mileage = maxb.max_mileage
nfgl
  • 2,812
  • 6
  • 16
1

Db2 for LUW supports the "UPDATE FROM SELECT" syntax.

CREATE TABLE A (unit, mileage) AS
(
VALUES
  (1, 0)
, (2, 0)
, (3, 0)
) WITH DATA
CREATE TABLE B (unit, mileage) AS
(
VALUES
  (1, 100)
, (1, 140)
, (2, 102)
, (2, 150)
, (3, 107)
, (3, 129)
) WITH DATA
;

UPDATE A
SET MILEAGE = B.MILEAGE
FROM
(
  SELECT UNIT, MAX (MILEAGE) AS MILEAGE
  FROM B
  GROUP BY UNIT
) B
WHERE A.UNIT = B.UNIT
;

SELECT * FROM A;
UNIT MILEAGE
1 140
2 150
3 129

fiddle

Mark Barinstein
  • 11,456
  • 2
  • 8
  • 16