Skip to content
Advertisement

Oracle SQL: LAG over a range of values

How do we use analytic function LAG over the range of values. It should return null if there is no record in the partition with an earlier in_date otherwise, year and month of the previous in_date

Example:

Expected Output,

My current query using LAG returns below

Current Result

Looks like, LAG does not allow us to use partition over range Is there an alternate approach for this

Advertisement

Answer

You don’t want LAG() if you want the previous month before this one. I would suggest:

Here is a db<>fiddle.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement