Try the new MAX_BY function:
select
id
, name
, max_by(package, pkg_date) as latest_pkg_code
from test_table
group by 1,2;
Result:
| ID |
NAME |
LATEST_PKG_CODE |
| 11 |
Ann |
R |
| 22 |
Lima |
A |
Here is a sample script:
-- create table
create or replace transient table test_table (
id int
, name varchar(50)
, pkg_date date
, package varchar(1)
);
-- insert data
insert into test_table
values
(11,'Ann','2022-02-05','R')
,(11,'Ann','2022-01-01','A')
,(11,'Ann','2021-11-01','U')
,(22,'Lima','2021-07-08','B')
,(22,'Lima','2022-02-08','A');
-- test table
select * from test_table;
-- get package code for max date for each name/id
select
id
, name
, max_by(package, pkg_date) as latest_pkg_code
from test_table
group by 1,2;