Skip to content
Advertisement

Select row where column is different from previous row

I am trying to write a query to track the changes of a boolean column in a table. The table looks like this

So what i would want is row 3 where the is_active changed to false. Then after that i would want the next row where it changed to true again.

This is what i tried:

So the subquery is trying to get the previous row of the same client_id by getting the most recent timestamp before it . Then in query check if is_active does not equal the is_active from the previous row. But this is not working as planned. I expect as I trigger acttive/inactive it should be alternating in this query but it is not. Anybody got any tips?

Advertisement

Answer

Use window functions!

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