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 数据库 Apache Camel 2021-01-01
- MySQL 中的类型:BigInt(20) 与 Int(20) 2021-01-01
- 如何在 SQL 中将 Float 转换为 Varchar 2021-01-01
- 什么是 orradiag_<user>文件夹? 2022-01-01
- oracle区分大小写的原因? 2021-01-01
- mysql选择动态行值作为列名,另一列作为值 2021-01-01
- 在 MySQL 中:如何将表名作为存储过程和/或函数参数传递? 2021-01-01
- 表 './mysql/proc' 被标记为崩溃,应该修复 2022-01-01
- 在多列上分布任意行 2021-01-01
- 如何根据该 XML 中的值更新 SQL 中的 XML 2021-01-01