update most recent rows only? I have a database of a garage of rented cars with a table called Cars with the following columns :
Type,First_seen_in_garage,Last_seen_in_garage
The table initially have the following rows :
Ferrari,2002-10-13 07:12:58,2002-10-13 10:12:58
Lambo,2002-10-13 08:12:58,2002-10-13 11:12:58
Renault,2002-10-13 09:12:58,2002-10-13 12:12:58
Ferrari,2002-10-13 10:12:58,2002-10-13 15:12:58

Now I have the following row from which I have to update the initial table :
*Ferrari,2002-10-14 08:12:58,2002-10-14 10:12:58*
*Renault,2002-10-13 23:12:58,2002-10-14 23:12:58*
*Lambo,2002-10-13 23:12:58,2002-10-14 23:12:58*

in order to become :
Ferrari,2002-10-13 07:12:58,2002-10-13 10:12:58
Lambo,2002-10-13 08:12:58,2002-10-14 23:12:58
Renault,2002-10-13 09:12:58,2002-10-14 23:12:58
Ferrari,2002-10-13 10:12:58,2002-10-14 10:12:58

Note that for the Ferrari rows I want to keep the first instance as it is and update only the second one
I could do it with an UPDATE .. SET .. WHERE if there was only one Ferrari instance however the previous query updates the two rows which is not what I want ..
Help Please.
As you don't tell us ´,w hat for an Update you will have,
I selected one . where the lst of a type get 5 Minutes more on his End time, this is of corse only an example for an   update, to show how it works.
The latest seen time we get from the WINDOW Function  MAX OVER PArtition
And the update only update the types with the latest time (i choose Last_seen_in_garage) because the update only updates the rows , that are joined together
In the new query i also check out the same data from the temptaböe(see that Ferrair has now 2020 date as Last_seen_in_garage and use those data to update the CARS table


CREATE TABLE Cars  (
  &quot;Type&quot; VARCHAR(7),
  &quot;First_seen_in_garage&quot; timestamp ,
  &quot;Last_seen_in_garage&quot; timestamp 
);

INSERT INTO Cars 
  (&quot;Type&quot;, &quot;First_seen_in_garage&quot;, &quot;Last_seen_in_garage&quot;)
VALUES
  ('Ferrari', '2002-10-13 07:12:58', '2002-10-13 10:12:58'),
  ('Lambo', '2002-10-13 08:12:58', '2002-10-13 11:12:58'),
  ('Renault', '2002-10-13 09:12:58', '2002-10-13 12:12:58'),
  ('Ferrari', '2002-10-13 10:12:58', '2002-10-13 15:12:58');


✓

4 rows affected




  CREATE TABLE Tmp_table  (
  &quot;Type&quot; VARCHAR(7),
  &quot;First_seen_in_garage&quot; timestamp ,
  &quot;Last_seen_in_garage&quot; timestamp 
);

INSERT INTO Tmp_table 
  (&quot;Type&quot;, &quot;First_seen_in_garage&quot;, &quot;Last_seen_in_garage&quot;)
VALUES
  ('Ferrari', '2002-10-13 07:12:58', '2002-10-13 10:12:58'),
  ('Lambo', '2002-10-13 08:12:58', '2002-10-13 11:12:58'),
  ('Renault', '2002-10-13 09:12:58', '2002-10-13 12:12:58'),
  ('Ferrari', '2020-10-13 10:12:58', '2020-10-13 15:12:58');


✓
4 rows affected




 UPDATE Cars
SET &quot;Last_seen_in_garage&quot; = maxtempcar.&quot;maxdateime&quot;
FROM (SELECT DISTINCT Cars.&quot;Type&quot;,
       MAX(&quot;Last_seen_in_garage&quot;) OVER (PARTITION BY Cars.&quot;Type&quot;) &quot;maxdateime&quot;
       FROM Cars) maxcar 
INNER JOIN (SELECT DISTINCT Tmp_table.&quot;Type&quot;,
MAX(&quot;Last_seen_in_garage&quot;) OVER (PARTITION BY Tmp_table.&quot;Type&quot;) &quot;maxdateime&quot;
FROM Tmp_table) maxtempcar ON maxtempcar.&quot;Type&quot; = maxcar.&quot;Type&quot; 
WHERE Cars.&quot;Type&quot; = maxcar.&quot;Type&quot; AND Cars.&quot;Last_seen_in_garage&quot; = maxcar.&quot;maxdateime&quot;;




3 rows affected




SELECT *
FROM Cars
;




Type    | First_seen_in_garage | Last_seen_in_garage
:------ | :------------------- | :------------------
Ferrari | 2002-10-13 07:12:58  | 2002-10-13 10:12:58
Lambo   | 2002-10-13 08:12:58  | 2002-10-13 11:12:58
Renault | 2002-10-13 09:12:58  | 2002-10-13 12:12:58
Ferrari | 2002-10-13 10:12:58  | 2020-10-13 15:12:58


db&lt;&gt;fiddle here