Skip to content
Advertisement

SQL – Get the antepenultimate (before previous group/phase)

I have the following table and I’d like to get the antepenultimate or the value before the previous value.

I already have group, value and prev_value, dateint… I’m trying to derive prev_prev_value

This is the table with test data (as a CTE)

Any ideas on how to derive prev_prev_value I’d like to use window functions and avoid joins.

I’ve tried LAG but I have not been successful.

Advertisement

Answer

It appears that a nested FIRST(LAG.. did the job:

The logic behind that was to get the previous (lag) prev_value and get the FIRST value of that group

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