SQLTeam.com | Weblogs | Forums

Select query get one id less or one row above based on primary key int value tskid

I need to get the prev or next available row based on the value i pass backend table, the value i pass is the primary key value taskid.

if i pass 116 to get prev value, i should get result as 114
if i pass 114 to get next value then i should my result as 116

the query to run to run just one single table tab_tasks based on taskid, could you please kindly tell me what is the best way to get prev or next ids.

112
113
114
116
119

Thanks, a lot for the helpful info.

You can use LEAD/LAG windowing functions to get previous/next values.