我需要查询 Pass Status 之后的 ID [已关闭]

问题描述 投票:0回答:1

enter image description here

输出

enter image description here

我正在尝试,但它对我不起作用

任何人都可以帮忙吗

sql sql-server join select
1个回答
1
投票

试试这个

drop table if exists #have;

CREATE TABLE #have 
(
  ID      [INT]
, Status  [VarChar](8)
);

insert into #have
values
  (1 , 'P')
, (2 , 'F')
, (3 , 'F')
, (4 , 'P')
, (5 , 'P')
, (10, 'F')
, (22, 'F')
;

with cte as
(
select *
     , lag(Status) over (order by ID) as prev
from #have
)

select ID, Status 
from cte
where Status = 'F' and prev = 'P'
;
© www.soinside.com 2019 - 2024. All rights reserved.