I have the table that is shown down below and have to retrieve the last updated code of the num, which I know by the date, but I don't really know how to retrieve that..
num | code | created |
---|---|---|
778951 | 1112233 | 2021-04-13 |
123446 | 2354654 | 2021-04-15 |
235487 | 1232546 | 2021-05-03 |
778951 | 1112234 | 2021-05-13 |
123446 | 2354655 | 2021-04-27 |
123446 | 2354656 | 2021-05-26 |
One method uses window functions:
select t.*
from (select t.*,
row_number() over (partition by num order by created desc) as seqnum
from t
) t
where seqnum = 1;
An alternative method that is often a bit faster with the right indexing is:
select t.*
from t
where t.created = (select max(t2.created) from t t2 where t2.num = t.num);
The right indexing is (num, created)
.