Hi! Could we please enable some services and cookies to improve your experience and our website?

SQLize | PHPize | SQLtest

Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code

A A A
Login    Share code      Blog   FAQ
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 AI support!

Copy Clear