SQLize Online / PHPize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
CREATE TABLE urls ( id serial, "name" varchar(255), created_at timestamp, updated_at timestamp ); CREATE TABLE urls_checks ( id serial, url_id integer, status integer, created_at timestamp, updated_at timestamp ); insert into urls values (1, 'example.com', now(), now()); insert into urls_checks values (1, 1, 1, now(), now() - interval '3 hour'), (1, 1, 2, now(), now() - interval '2 hour'), (1, 1, 3, now(), now() - interval '1 hour'), (1, 1, 2, now(), now()) ; select * from urls_checks; SELECT * FROM ( SELECT url_id, status, updated_at, ROW_NUMBER() OVER (PARTITION BY url_id ORDER BY updated_at DESC) rn FROM urls_checks ORDER BY urls_checks DESC ) checks JOIN urls on urls.id = checks.url_id WHERE rn = 1;
Stuck with a problem? Got Error? Ask ChatGPT!
Copy Clear