0

我想在Status1每个序列的列中获取相同值的第一个和最后一个值!

这是我的表的一个例子:

time                     Status1   Eficiencia   Lote
----------------------------------------------------
2020 06 14 18:03:48.457    70         80         95
2020 06 14 18:04:47.693    70         80         95
2020 06 14 18:06:58.203    55         80         95
2020 06 14 18:08:19.900    55         80         95
2020 06 14 18:09:45.537    55         80         95
2020 06 14 18:10:06.670   100         80         13
2020 06 14 18:10:27.297   100         80         13
2020 06 14 18:10:31.810   100         80         13
2020 06 14 18:10:43.187   100         80         13
2020 06 14 18:11:30.303    55         80         14
2020 06 14 18:12:07.563    55         80         14
2020 06 14 18:18:54.997    55         80         14

我试过这个,但使用这个我没有得到每个序列,而是在洞表中!

;with cte as
(
    select
        *,
        RnAsc = row_number() over (partition by [Status1] order by time),
        RnDesc = row_number() over (partition by [Status1] order by time desc) 
    from 
        [VALORES_DB].[dbo].[OEE_TESTE]
)
select time, [Status1], Eficiencia, Lote, Status1 
from cte
where RnAsc = 1 or RnDesc = 1

我想得到以下信息:

time                      Status1   Eficiencia   Lote
------------------------------------------------------
2020 06 14 18:03:48.457     70          80        95
2020 06 14 18:04:47.693     70          80        95
2020 06 14 18:06:58.203     55          80        95
2020 06 14 18:09:45.537     55          80        95
2020 06 14 18:10:06.670    100          80        13
2020 06 14 18:10:43.187    100          80        13
2020 06 14 18:11:30.303     55          80        14
2020 06 14 18:18:54.997     55          80        14
4

1 回答 1

1

我建议lag()并且lead()

select t.*
from (select t.*,
             lag(status1) over (order by time) as prev_status1,
             lead(status1) over (order by time) as next_status1
      from t
     ) t
where (prev_status1 is null or prev_status1 <> status1) or
      (next_status1 is null or next_status1 <> status1);

这些比较确定了值的变化位置——这正是您所要求的。

于 2020-06-21T17:18:14.713 回答