Sunday, 25 February 2018

CTE example in SQL Server_To get Previous n Next value

WITH CTE AS (
SELECT
rownum = ROW_NUMBER() OVER (ORDER BY p.jobpost_id),
p.jobpost_id
FROM tblJobPostDetails p
)
SELECT
prev.jobpost_id PreviousValue,
CTE.jobpost_id,
nex.jobpost_id NextValue
FROM CTE
LEFT JOIN CTE prev ON prev.rownum = CTE.rownum - 1
LEFT JOIN CTE nex ON nex.rownum = CTE.rownum + 1

No comments:

Post a Comment