Delete Every Alternate Row in SQL

安稳与你 提交于 2019-12-05 03:39:56

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

Instead of deleting alternate records you might use safer variation - delete only duplicate Observations, so that in case of error in data you don't fall out of sync:

; with cte as (
  select *,
         row_number() over (partition by Observation 
                            order by [Timestamp]) rn
    from [Records]
)
delete cte
 where rn > 1

If I'm not mistaken you could use a CTE, with row_number() to generate effectively a temp numeric id column. Then the same idea could be applied - delete where a row number mod 2 is 0:

;WITH temp
     AS (SELECT *,
                Row_number() OVER (ORDER BY [Timestamp]) rn
         FROM   [Records]
         ORDER  BY [Timestamp])
DELETE FROM temp
WHERE  rn % 2 = 0

you can do so using the MOD operator which divide two numbers and return the remainder

try

DELETE FROM [Records] WHERE ([ID] Mod 2)=0)

put it inside a loop to delete all alternate rows

so for example

ID = 0;
rows = get_number_of_rows_in_the_table;
i = 0; // counter
while(i<rows) {
ID = get the ID of row 0
if(ID MOD 2 = 0) {
//perform delete operation
DELETE FROM [Records] WHERE ID=the ID you just got;
} else { 
increment the counter 
i++
}

this is one solution but not a correct syntax for access hope it helps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!