Think of this as a product, that, when it's goes in a state of back order, is added to this table with the timestamp from today and the date it should be available in the future. In the example above, we can see that the product 02 was made re-available on the 04-03 after a back order that started on the 04-01, which is why there are no new entries for the 02 product after the 04-02.
The flag symbolizes the start of a new back order sequence.
Now, if I were to run the script on the 04-04. Product 01 should count 4 days in back order and 0 changes on the availability date. Product 02 should count 0 days in back order and 0 changes as it isn't currently in back order.
Now, let's say, on the next day, the table looks like this:
Two things happened, in the first place, the product 02 is in a new back order, which is flagged. The product 01 availability date changed. In this scenario, I would like to get, for product 01, 5 days in back order and 1 change of the availability date. For product 02, 1 day in back order and no changes of the availability date since it's the first date in this new sequence of back order.
The results of these calculations (the numbers of days in back order and the number of changes of the availability date in this sequence) would be stored in another table like so: