Delete Every Alternate Row in 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 中的每一行
基础教程推荐
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- 在 SQL 中连接多个表 2021-01-01
- 无法解决整理冲突 2021-01-01
- 是否可以执行按位分组功能? 2021-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
