删除 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 中的每一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)