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, dest int, emp int); insert into t select 1,893106,0 union all select 2,717205,1 union all select 3,888305,0 union all select 4,312301,1 union all select 5,645100,0 union all select 6,222001,0 union all select 7,761104,1; commit; with main_data as ( select *,case when emp=0 and lag(emp) over(order by id)=1 then 1 else 0 end as grp_val from t ) select *,sum(grp_val) over(order by id) as grp from main_data;

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

Copy Clear