I have an event table, each row has a StartDateTime column. I need to query a subset of events (e.g. userID) and determine the average number of days between consecutive events.
In general, the table is as follows.
TransactionID TransactionStartDateTime ---------------------------------------- 277 2011-11-19 11:00:00.000 278 2011-11-19 11:00:00.000 279 2012-03-20 15:19:46.160 288 2012-03-20 19:23:06.507 289 2012-03-20 19:43:41.980 291 2012-03-20 19:55:17.523
I tried to adapt the following query specified in this Question :
select a.TransactionID, b.TransactionID, avg(b.TransactionStartDateTime-a.TransactionStartDateTime) from (select *, row_number() over (order by TransactionStartDateTime) rn from Transactions) a join (select *, row_number() over (order by TransactionStartDateTime) rn from Transactions) b on (a.rn=b.rn-1) group by a.TransactionID, b.TransactionID
But I was not lucky as the original request did not expect DateTimes
My expected result is one figure representing average days (which I now understand differently than the above query would say)
Any ideas?
source share