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 tbl ( org_id int, patient_id int, date date ); insert into tbl values (313, 455259, '2018-01-07'), (313, 455259, '2018-04-09'), (679, 455259, '2018-07-08'), (780, 455259, '2018-09-06'); with data as ( select patient_id, org_id, date, lag(org_id) over (partition by patient_id order by date) prev_org_id from tbl ) select * from data where org_id != prev_org_id and prev_org_id is not null;

Stuck with a problem? Got Error? Ask AI support!

Copy Clear