删除 SQL 中的每个交替行 [英] Delete Every Alternate Row in SQL

查看:33
本文介绍了删除 SQL 中的每个交替行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要清理一个包含三列的表,ID(唯一标识符)、观察(nvarchar)和时间戳(datetimeoffset).内容由两个传感器以 1 小时间隔生成,两个传感器的 Observation 值相同.我的想法是做一个 SELECT 查询,例如

I need to clean up a table which has three columns, ID (uniqueidentifier), Observation (nvarchar), and Timestamp (datetimeoffset). The content is generated by two sensors at 1 hour interval, and the value of Observation is the same from the two sensors. My idea is to do a SELECT query such as

SELECT * FROM [Records] ORDER BY [Timestamp]

然后删除每一行.

我在 SO 如何删除每个备用行上找到了这个在访问表中,但在这里并不真正适用,因为 ID 不是 Int 而是 UID.

I found this on SO how to delete every alternate row in access table, but doesn't really applies here as the ID is not Int but UID.

数据示例如下:

推荐答案

如果您使用的是 SQL Server 2005 或更高版本,您可以执行类似的操作.

If you are on SQL Server 2005 or later you can do something like this.

delete T
from (
       select row_number() over(order by [Timestamp]) as rn
       from YourTable
       where SomeColumn = @SomeValue
     ) T
where T.rn % 2 = 0

这篇关于删除 SQL 中的每个交替行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆