Thursday 16 February 2017

Select an OrderID always before the current passing OrderID:

select an OrderID before the OrderID you are passing. check the below scenario for more clarification.


 ;WITH CTE AS 
(
  SELECT
    rownum = ROW_NUMBER() OVER (ORDER BY orderID),orderID FROM Orders
SELECT 
  cur.orderID as CurrentID , prev.orderID as PrevID
 FROM CTE cur
INNER JOIN CTE prev on prev.rownum = cur.rownum - 1
where cur.orderID=27 or cur.orderID=29
OUTPUT:


No comments:

Post a Comment