Skip to content
Advertisement

How to add a row to result if a condition based on two adjacent rows is true

Given a table like this

Is it possible to get this?

Right now, I’m post processing the table with a for loop checking if system_time_start is not equal to the previous row’s system_time_end, then insert another row to the result with a null price, but I’m wondering if there’s a way to do this in SQL. I was thinking to use the lag function, which can do the check that I want, but inserting a row in between is what I can’t figure out how to do.

Advertisement

Answer

Hmmm . . . You can use not exists:

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