SQLize Online / PHPize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
create table t (id int, uid int, utm varchar(16)); insert into t values (1, 10, 'test1'), (2, 10, 'test2'), (3, 10, 'test3'), (4, 11, 'test4'), (5, 11, 'test5'); -- using window functions select distinct uid, first_value(utm) over (partition by uid order by id desc) last_utm from t; -- using join select t.uid, t.utm last_utm from t join ( select max(id) max_id from t group by uid ) max_ids on max_id = id; -- using sub-query select t.uid, t.utm last_utm from t where id = (select max(id) max_id from t group by uid having max(id) = t.id);
Stuck with a problem? Got Error? Ask ChatGPT!
Copy Clear