You can use dense_rank
as follows:
Select t.* from
(Select t.*,
dense_rank() over (partition by name order by date desc) as dr
From your_table t
Where t.date < (select min(tt.date)
from your_table tt
where tt.name = t.name
and tt.flag = 'Y') ) t
Where t.dr = 1
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…