How to calculate duration in aggregation?

Toughest question to answer on an event store is something like "what is the average duration of web sessions on my website?". This looks like that sort of question.

Why it is hard and how to solve it is covered in the "entity centric indexing" talk here: https://www.youtube.com/watch?v=yBf7oeJKH2Y

It includes example scripts you can download.