10

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]

and then delete every alternate row.

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.

A sample of the data would look like:

enter image description here

1
  • is the timestamp exactely equal for those duplicate rows?
    – juergen d
    Aug 13, 2012 at 9:02

4 Answers 4

19

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
2
  • Yes I'm on SQL Server 2008 R2. Is it possible to add a WHERE clause for the SELECT? Such as delete T from ( select row_number() over(order by [DateTime]) as rn from [Records] WHERE [Timestamp] < '2012/6/30 12:00am' ) T where T.rn % 2 = 0
    – Jim
    Aug 13, 2012 at 9:13
  • @Jim Sure. You do it in the inner query. I have added a sample where clause to the query. Aug 13, 2012 at 9:14
5

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
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
1
  • FYI CTEs are only available if you're using SQL Server 2005 or later - you didn't specify the version in your question.
    – Bridge
    Aug 13, 2012 at 9:06
1

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

2
  • Don't loop if you can avoid it. There are more efficient ways to do things in SQL. May 1, 2018 at 15:12
  • @loophole there was no need for the down vote! this is 6 years old and your comment is meaningless. show a better way rather than down voting an actual solution regardless if you like it or no it is going to still be a solution
    – Moe
    May 7, 2018 at 2:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.