By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
create table Sampledata
(
id int
,groupno int
,Date date
)
insert into Sampledata values (
1,1,'1/2/2020'),
(2,2,'1/13/2020'),
(3,2,'1/13/2020'),
(4,2,'1/13/2020'),
(5,3,'1/24/2020')
5 rows affected
select sd.*, g.next_date
from sampledata sd join
(select groupno, lead(min(date)) over (order by groupno) as next_date
from Sampledata
group by groupno
) g
on sd.groupno = g.groupno
id | groupno | Date | next_date |
---|---|---|---|
1 | 1 | 2020-01-02 | 2020-01-13 |
2 | 2 | 2020-01-13 | 2020-01-24 |
3 | 2 | 2020-01-13 | 2020-01-24 |
4 | 2 | 2020-01-13 | 2020-01-24 |
5 | 3 | 2020-01-24 | null |
select sd.*, x.next_date
from sampledata sd outer apply
(select top (1) min(sd2.date) as next_date
from sampledata sd2
where sd2.date > sd.date
) x
id | groupno | Date | next_date |
---|---|---|---|
1 | 1 | 2020-01-02 | 2020-01-13 |
2 | 2 | 2020-01-13 | 2020-01-24 |
3 | 2 | 2020-01-13 | 2020-01-24 |
4 | 2 | 2020-01-13 | 2020-01-24 |
5 | 3 | 2020-01-24 | null |