删除 SQL 中的每一行

Delete Every Alternate Row in SQL(删除 SQL 中的每一行)

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

问题描述

我需要清理一个包含三列的表,ID (uniqueidentifier)、Observation (nvarchar) 和 Timestamp (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 中的每一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:删除 SQL 中的每一行

基础教程推荐