Computing Weekly Changes

Learn to compute changes for various time durations in PostgreSQL.

Let’s assume that the analytics department now wants us to add a weekly difference for each day of the result. More specifically, we want to add a column with the evolution as a percentage of the dollars column between the day of the value and the same day of the previous week.

Use case: Computing weekly changes

We’re taking the “week over week percentage difference” example because it’s a classic analytics need, though perhaps mostly in marketing circles. In our experience, rarely will the first reaction of a developer be to write a SQL query that does all the math.

Also, computing weeks is another area in which the calendar we have isn’t very helpful, but for PostgreSQL the task is as easy as spelling the word “week.”

Press the “Run” button in the following widget to see the output of the code.

Get hands-on with 1400+ tech skills courses.