mysql计算用户平均下单周期

            用户表结构如下:

   计算用户的平均的下单周期,怎么计算的,开始看时,我也没有思路,怎么简单的计算。今天上午也进行尝试了下,

 以下是我的计算的sql,仅供参考,如果有错误或有更好的方法,欢迎一起交流,

 select 
 c.id,avg(gap)
 from(         
 select a.id,a.date as d1,min(b.date) as d2,TIMESTAMPDIFF(day,a.date,min(b.date)) as gap
 from user_order as  a
 left join  user_order as b on a.id = b.id and a.date < b.date
 where b.date is not null
 group by a.id,a.date
 )as c
 group by c.id;

  

 

posted on 2017-06-24 17:28  xjz1842  阅读(1301)  评论(0编辑  收藏  举报

导航