SQL query to get historical data from an entity with state start and end timestamps in the same row/result

Hey all,

I would like to create a mariadb view to get state start and end timestamps for a selection of entities but it’s a bit more complicated with the HA db structure. Something like the history graph but directly with sql. This view will server for future sql sensors later on.

Any one managed to do something like this ?

This might help?

I try to explain what the query is doing in the post.

If you need more help, be more specific in what output you’re looking to achieve.