TimescaleDB and on change events

Hi TimescaleForum members,
I need your help! I have a table were I collect events with a deviceId, a timestamp, a category name, a specific code.
One or more events with the same category, code and deviceId but a greater timestamp can exists and indicates that the same event is still on going.
I would like to create a continuously aggregated view where, for each event with same category, code and device, I add a start timestamp (the first timestamp occurring) and an end timestamp which is equals to the start of the next event with different code or category (same deviceId). It is an “On change” logic.

Can you suggest me the best and performant way of doing this with timescale?
Thanks in advance, best regards.