Aaaaand with a cover of black magic:
select a.eventName, b.eventName, AVG(DATEDIFF(MINUTE, a.[Time], b.[Time])) as Average from (select *, row_number() over (order by [time]) rn from events) a join (select *, row_number() over (order by [time]) rn from events) b on (a.rn=b.rn-1) group by a.eventName, b.eventName
This will give you lines like:
stage3 stage1 2 stage1 stage2 2 stage2 stage3 5
The first column is the start event, the second column is the end event. If there is Event 3 immediately after Event 1, this will also be indicated. Otherwise, you should indicate some criteria, by which stage it should be, at what stage, therefore the time is calculated only between them.
Added: This should work both on Transact-SQL (MSSQL, Sybase), and on PL / SQL (Oracle, PostgreSQL). However, I have not tested it, and there may still be syntax errors. This will not work on any version of MySQL.
Vilx- source share