Total Pageviews

2020/09/10

[Oracle] [Function] LEAD and LAG

I have a Product table:












If I would like to know PRODUCT_ID = 1000, and find its previous order date and next order date. We can make use of LEAD and LAG function to fulfill this requirement:
SELECT ORDER_DATE, PRODUCT_ID,
       LAG(ORDER_DATE) over (ORDER BY ORDER_DATE) AS PREVIOUS_RECORD,
       LEAD(ORDER_DATE) over (ORDER BY ORDER_DATE) AS NEXT_RECORD
FROM PRODUCT
WHERE PRODUCT_ID = 1000;


No comments: