I am trying to write a sql statement that will allow me to check each row in a sql server table sequentially. If the values for the fields are more than one apart I would like to return the value of that field and the field that is more than 1 away. For instance, my fields look like this:
ValueField
V100819493
V100819492
V100819491
V100819232
In this case the only values that are more than 1 apart are:
V100819232
V100819491
Any idea on how I can do this in a select statement?
You can’t do this directly in a select statement. Your best bet would be to select the values using an ORDER BY statement, and then loop through them. Check each value against its predecessor and see if the difference > 1.
You could in theory do this all in SQL by writing a function to insert each value which passes the check into a secondary (possibly temporary) table, but if you can, implement the check in C#, Visual Basic, what have you for obvious performance reasons.