tsql - How to calculate average date occurrence frequency in SQL -
i'm trying produce query on following table (relevant portion only):
create table [order] ( orderid int not null identity(1,1), creationdate datetime not null, customerid int not null )
i see list of customerids each customer's average number of days between orders. i'm curious if can done pure set based solution or if cursor/temp table solution necessary.
;with base ( select customerid, row_number() on (partition customerid order creationdate, orderid) rn [order] ) select b1.customerid, avg(datediff(day,b1.creationdate, b2.creationdate) ) base b1 join base b2 on b1.customerid=b2.customerid , b2.rn =b1.rn+1 group b1.customerid
Comments
Post a Comment