hmm if I understand correctly the main problem would be "every sum to know which period was the last one." this would need a variable. And that's usually a stored procedure :)
Unless you could work with groupings and havings since you could use
sum(if yes +1 if no -1) as sumvalue group by common-identifier having sum(field) == 2
in pseudo code because if you look at it mathematically you just need a sequence of two in a row. since you can add and subtract one in any order in the end there has to be a two.
But the way I read it, that's likely not the case