Adding previous or next row values based on condition

any inputs on this? Concurrency issue, Stored Procedure, Table Valued Function