Skip to content
Advertisement

PostgreSQL Percent Change using Row Number

I’m trying to find the percent change using row number with PostgreSQL but I’m running into an error where my “percent_change” column shows 0.

Here is what I have as my code.

Here is my SQL table in case it’s needed. Thank you in advance, I greatly appreciate it.

Advertisement

Answer

You can use LAG() for your requirement:

or you can try with WITH clause

DEMO

EDIT as per requirement in comment

DEMO

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