By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
create table data (Status varchar(10), MyDate date);
insert into data values
('A', '6/14/2021')
,('A' ,'6/12/2021')
,('B' ,'6/10/2021')
,('A' ,'6/8/2021')
,('B' ,'6/6/2021')
,('A' ,'6/4/2021');
6 rows affected
select top(1) Mydate from
(
select *, max(case when Status = 'B' then Mydate end) over () MaxBDate from data
) t
where status = 'A'
and MyDate > MaxBDate
order by Mydate
Mydate |
---|
2021-06-12 |
Warning: Null value is eliminated by an aggregate or other SET operation.