I have table like follows
col date
a 2022-07-26
a 2022-07-27
c 2022-08-02
d 2022-07-28
and I would like to update value at latest date column
update table
set date = '2022-09-30'
where col='a'
and // some logic
my desired result is as follows
col date
a 2022-07-26
a 2022-09-30
c 2022-08-02
d 2022-07-28
how to select latest days and update ?
Thanks
2
Answers
We can use an update join approach here:
The subquery above aliased as
t2
will target only the latest date record for eachcol
group. We then set the date for that record, forcol
=a
.An easy and readable approach would be to simply update the record for the MAX(date).
This is assuming you only want to update for a, and for this 1 record.
If you want to update for each ‘col’ on the highest date, you for example use a CTE;