案例分析:使用sql进行用户消费行为分析

使用sql进行用户消费行为分析

所需数据:ORDER_INFO_UTF.CSV、USER_INFO_UTF.CSV

1、将数据导入mysql数据库

步骤:将两份csv文件导入数据库、建表、导入数据
前面的表分别是10万和50万条数据,如果用数据库自带的导数工具比较缓慢,像WORKBENCH导入几千行数据就需要3分钟时间,这里可以用cmd命令行导入、或者用KETTLE进行抽取。

2、建表
#1、订单明细表
CREATE TABLE ORDERINFO  ( 
    ORDERID     varchar(10) PRIMARY KEY,-- 订单ID,主键
    USERID      varchar(10) NULL,-- 用户ID,可以和用户表进行关联
    ISPAID      varchar(10) NULL,-- 是否支付
    PRINCE      varchar(10) NULL,-- 订单价格
    PAIDTIME    varchar(100) NULL -- 订单支付时间
    )
#2、用户表
CREATE TABLE USERINFO  ( 
    USERID  varchar(10) PRIMARY KEY,-- 用户ID,主键
    SEX     varchar(10) NULL,-- 性别
    BIRTH   varchar(100) NULL -- 出生日期
    )
#3、装载文件
load data infile 'C:\\ProgramData\\MySQL\\MySQL Server 5.7\\Uploads\\order_info_utf.csv'  
into table orderinfo 
fields terminated by ',' 
lines terminated by '\r\n';
3、待分析的六个问题:

1-统计不同月份的下单人数
2-统计用户三月份的回购率和复购率
3-统计男女的消费频次是否有差异
4-统计多次消费的用户,第一次和最后一次消费时间的间隔
5-统计不同年龄段的用户消费金额是否有差异
6-统计消费的二八法则,消费的top20%用户,贡献了多少额度

1、统计不同月份的下单人数
思路:查看orderinfo发现支付时间字段都是同一年份的,所以直接使用month函数提取月份,这样更加简便。

select month(paidTime) ,count(distinct userId) from orderinfo
where isPaid = '已支付'
group by month(paidTime)

2、统计用户三月份的回购率和复购率

  • 复购率指当月消费者中消费次数多于一次的人数占比
  • 回购率指本月消费者中在下月再次消费的占比

(1)复购率:

  • 思路:先统计三月份每个购买者的购买次数,作为一个子查询返回,
    外层使用count+if函数统计大于一次消费的购买者人数,将其与总人数相除,即可得到复购率。
select count(ct) ,count(if(ct>1,1,null)),count(if(ct>1,1,null))/count(ct) as ratio
from ( select userId,count(userId) as ct from orderinfo
       where isPaid = '已支付'
       and month(paidTime) = 3
       group by userId) t

(2)回购率:

  • 思路:使用自连接,对比两个月份是否相差一个月,统计本月购买并在下月继续购买的客户数量
select t1.m,count(t1.m),count(t2.m) from (
      select userId,date_format(paidTime,'%Y-%m-01') as m from orderinfo
      where isPaid = '已支付'
      group by userId,date_format(paidTime,'%Y-%m-01')) t1
left join (
      select userId,date_format(paidTime,'%Y-%m-01') as m from orderinfo
      where isPaid = '已支付'
      group by userId,date_format(paidTime,'%Y-%m-01')) t2
on t1.userId = t2.userId and t1.m = date_sub(t2.m,interval 1 month)
group by t1.m 

3、统计男女用户消费频次是否有差异

  • 思路:过滤空值,连接两个表,通过count统计单个购买者的购买次数, 根据性别分组,统计均值,得到男女平均消费频次。
select sex,avg(ct) from (
     select o.userId,sex,count(1) as ct from orderinfo o
     inner join (
         select * from userinfo
         where sex is not null) t 
     on o.userId = t.userId
     group by userId,sex) t2
group by sex

4、统计多次消费的用户,第一次和最后一次消费间隔是多少

  • 思路:提取多次消费用户,用datediff计算max和min的差值
select userId,max(paidTime),min(paidTime),
    datediff(max(paidTime),min(paidTime)) from orderinfo
where isPaid = '已支付'
group by userId having count(1) > 1 

5、统计不同年龄段用户消费频次是否有差异

  • 思路:年龄/10作为年龄段
select age,avg(ct) from (
       select o.userId,age,count(o.userId) as ct
       from orderinfo o
       inner join (
           select userId,ceil((year(now()) - year(birth)) / 10) as age
           from userinfo
           where birth > '1901-00-00') t 
       on o.userId = t.userId
       group by o.userId,age) t2
group by age 

6、统计消费的二八法则,消费的top20%用户,贡献了多少额度

  • 思路:先计算出前20%的用户数量,再算出前20%用户贡献额度总和。
select round(COUNT(distinct userid)*0.2) from orderinfo where ISPAID='已支付'
-- 17130
select count(userId),sum(total) from (
    select userId,sum(prince) as total from orderinfo o
    where isPaid = '已支付'
    group by userId
    order by total desc
    limit 17130)t

注意:mysql5.7之前没有窗口函数row_number() over(order by )
我们使用这种方式实现:

  • 变量会最后再计算,所以是先排序好之后,才会开始计算@num
-- 最终sql
select COUNT(userid),SUM(total) from 
 (select  @num := @num+1 num,userid,total from 
  (select userId,sum(prince) as total from orderinfo o
    where isPaid = '已支付'
    group by userId
    order by total desc) t,(SELECT @str := '', @num := 0)t1)t2
where t2.num < (select round(COUNT(distinct userid)*0.2) from orderinfo where ISPAID='已支付')
  • 2
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值