Paresh
Paresh

Reputation: 3749

Want the data of Upline and Downline for particular ID

here is the table and data like:

id name
1  test1
2  test2
3  test3
4  test4
5  test5
6  test6

From above data i want the data like if i pass the id as parameter and return the data from from up and gown by order

Example if i pass the id as parameter = 4 then it should be return upline 2 row and downline 2 row for particular id, and it should be like this

id name

2  test2
3  test3
4  test4
5  test5
6  test6

and same for the id = 3

id name
1  test1
2  test2
3  test3
4  test4
5  test5

Upvotes: 0

Views: 1152

Answers (1)

IordanTanev
IordanTanev

Reputation: 6240

SELECT TOP 3 id, name FROM table WHERE id =< @id ORDER BY id DESC

UNION 

SELECT TOP 2 id, name
FROM table
WHERE id > @id 
ORDER BY id ACS

Upvotes: 1

Related Questions