zoukankan      html  css  js  c++  java
  • MYSQL系列(5) 电商常用指标查询

    表:order ,字段:orderid, userid, isPaid, price, paidTime

    表:user,字段: userid, sex, birth

    1.统计不同月份下单人数

    select month(date_format(paidtime, '%Y-%m-%d')), count(distinct userid)
    from order
    where isPaid = '已支付'
    group by month(date_format(paidtime, '%Y-%m-%d'));
    
    

    2.统计3月份回购率和复购率

    2.1 3月份复购率

    复购率:3月份中购买两次以上的人数占3月份总购买人数的比例

    ​ 3月份购买人数

    select userid, count(userid) as count_userid
    from order
    where ispaid = '已支付'
    and month(date_format(paidtime, '%Y-%m-%d')) = 3
    group by userid;
    

    ​ 复购率 =3月份购买两次以上的人数/ 对3月份复购人数统计去重

    select count(t.userid) as 购买人数, count(if(t.count_userid>1, 1, null)) as '复购人数',
    	   count(if(t.count_userid>1, 1, null)) / count(t.userid) as '复购率'
    from (select userid, count(userid) as count_userid
         from order
         where ispaid = '已支付'
         and month(date_format(paidtime, '%Y-%m-%d')) = 3
         group by userid
          ) t
    

    2.2 用户回购率

    ​ 运用子查询,筛选出第二个月有购买记录的情况,然后进行count

    select t1.m, count(t1.userid), count(t2.userid)
    from	(select userid), date_format(paidtime, '%Y-%m-01') as m
    		from order 
    		where ispaid = '已支付'
    		group by userid, date_format(paidtime, '%Y-%m-01')
    		) as t1
    left join
    		(select userid, date_format(paidtime, '%Y-%m-01') as m
            from order
            where ispaid = '已支付'
            group by userid,  date_format(paidtime, '%Y-%m-01')
            ) as t2
    on 		t1.userid = t2.userid and t1.m = date_sub(t2.m, INTERVAL 1 month)
    group by t1.m
    
    1. 统计第一次和最后一次消费间隔多久

      select userid, datediff(max(paidtime), min(paidtime))
      from order
      where ispaid = '已支付'
      group by userid
      having count(1) > 1
      
  • 相关阅读:
    018_STM32程序移植之_串口接收中文
    003_软件安装之_Visual Studio 2012
    001_C#我的第一个串口上位机软件
    017_STM32程序移植之_AS608指纹模块
    016_STM32程序移植之_舵机
    015_STM32程序移植之_NRF24L01模块
    014_STM32程序移植之_L298N电机驱动模块
    002_89C52_Proteus_DAC0832_输出50HZ,正弦波,三角波,矩形波,锯齿波
    001_89C52之_Proteus_ADC0809采集电压
    001_电子工程师招聘笔试题及详细解析
  • 原文地址:https://www.cnblogs.com/yangjiez/p/12105693.html
Copyright © 2011-2022 走看看