Skip to content
Advertisement

How do I sum until it meets a criteria?

I have the following data set for an user. I want to merge the False flag rows to the latest True row.

initial set:

output:

sql to recreate data:

Advertisement

Answer

This is an aggregation problem. You can use a cumulative sum to assign groups. And then aggregate. The group is the number of “trues” up to each row. So:

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