如何清理没有改变的记录

时间:2017-06-27 21:30:32

标签: sql sql-server sql-server-2012

dataset

我想要清理我的数据集。左侧是原始数据的样子。它的温度读数基于时间。我基本上想要删除该行,如果它上面的行具有相同的温度

2 个答案:

答案 0 :(得分:0)

如果您只想进行更改,请使用大多数数据库支持的ANSI标准lag()函数:

select zone, temperature, time
from (select t.*,
             lag(temperature) over (partition by zone order by time) as prev_temperature
      from t
     ) t
where prev_temperature is null or prev_temperature <> temperature;

答案 1 :(得分:0)

您可以使用lead获取下一行的值,并在下一行的值相同时删除行。

with cte as (select t.*
             ,lead(temperature) over(partition by zone order by time) as next_temp
             from tbl t
            )
delete from cte
where next_temp is not null and next_temp=temperature