By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
create table your_table (
[date] date,
phase varchar(30),
user_id int
)
insert into your_table
([date], phase, user_id) values
('2020.1.1', 'a', 10)
, ('2020.1.2', 'a', 10)
, ('2020.1.3', 'b', 10)
, ('2020.1.4', 'a', 10)
, ('2020.1.5', 'a', 10)
, ('2020.1.6', 'b', 10)
6 rows affected
select user_id
, min([date]) as start_date
, max([date]) as end_date
from
(
select t1.user_id, t1.[date], t1.phase
, count(t2.phase) as grp
from your_table t1
left join your_table t2
on t2.user_id = t1.user_id
and t2.phase = 'b'
and t2.[date] >= t1.[date]
group by t1.user_id, t1.[date], t1.phase
) q
group by user_id, grp
order by user_id, start_date
user_id | start_date | end_date |
---|---|---|
10 | 2020-01-01 | 2020-01-03 |
10 | 2020-01-04 | 2020-01-06 |