怎么用mysql来统计消费金额限制_用MySQL分析网络销售案例

本文利用MySQL分析网络销售数据,包括不同月份的下单人数、用户回购与复购率、男女消费频次、消费间隔及年龄层消费差异,并探讨消费的二八法则。
摘要由CSDN通过智能技术生成

用MySQL分析网络销售案例

数据来源于某网站销售统计

网络订单数据

用户信息

点击获取数据 提取码:3k6i

分析步骤

0、数据导入

1、不同月份的下单人数

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

3、统计男女用户的消费频次

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

5、统计不同年龄段用户的消费金额差异

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

0 数据导入

首先需要先创建对应的数据库和相应的表

创建orderinfo 表

b7d1832c81ae6ff86fbff7d457b65e15.png

创建userinfo表

a110124767a386e3fd099c6cd6f76d63.png

登录mysql导入相应的数据

load data local infile "file" into table dbname.tablename ...

# 登录

mysql --local-infile -uroot -p

# 导入数据orderinfo

load data local infile 'F:\BaiduNetdiskDownload\SQL\order_info_utf.csv' into table data.orderinfo fields terminated by ',';

# 导入数据userinfo

load data local infile 'F:\BaiduNetdiskDownload\SQL\user_info_utf.csv' into table data.userinfo fields terminated by ',';

观察数据,对时间进行处理 ; 更新字符串为日期格式

update orderinfo set paidtime=replace(paidtime,'/','-') where paidtime is not null

update orderinfo set paidtime=str_to_date(paidtime,'%Y-%m-%d %H:%i') where paidtime is not null

5查看数据

bee62c099d8b54337ef3a07b97c27949.png

1 不同月份的下单人数

思路 :按月份进行分组,对用户进行去重统计

select month(paidTime) as dtmonth,

count(distinct userId) as count_users

from orderinfo

where isPaid = '已支付'

group by month(paidTime)

133559ff2326395176851cae758c6245.png

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

复购率 : 自然月内,购买多次的用户占比

首先先找出已支付中3月份的用户id和对应次数,按用户分组

然后再嵌套一层,复购率:购买次数大于1/ 总购买次数

select count(ct),count(if(ct>1,1,null)) from(

select userID,Count(userId) as ct from orderinfo

where isPaid = '已支付'

and month(paidTime) = 3

group by userId

order by userId) t

8e8e7198213af89212887901b4b11883.png

复购率: 16916 / 54799 = 0.308

回购率: 曾经购买过的用户在某一时期内再次购买的占比

首先先查询已支付userId ,和 支付月份的统计

select userId, date_format(paidTime, '%Y-%m-01') as m from orderinfo

where isPaid = '已支付'

group by userId , date_format(paidTime,'%Y-%m-01')

然后使用date_sub函数,将表关联,筛选出本月的消费的userID,和下月的回购userID,即可计算出回购率

select t1.m,count(t1.m) as 消费总数,

count(t2.m) as 复购率,

count(t2.m)/ count(t1.m) as 回购率 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

8e9d5c41f36c66712aa7338e14d016dd.png

3 统计男女用户的消费频次

userinfo因为性别有空值,需要筛选出t orderinfo 再和表t连接 统计出用户男女消费次数

select o.userId,sex,count(o.userId)as ct from orderinfo o

inner join

(select * from userinfo

where sex != '') t

on o.userId = t.userId

group by userId,sex

order by userId

eda1ca0608ca52589993245431104131.png

根据上表,在进行子查询,统计出男性消费频次

select sex,avg(ct) from(

select o.userId,sex,count(o.userId)as ct from orderinfo o

inner join

(select * from userinfo

where sex != '') t

on o.userId = t.userId

group by userId,sex

order by userId)t2

group by sex

d0fc1a54ed040129c35ff8eda8a1fbab.png

4 统计多次消费用户,分析第一次和最后一次的消费间隔

首先把多次消费的用户,和相应第一次最后一次消费时间提取出来

然后使用datediff 计算时间间隔,以天为单位

select userId,max(paidTime),min(paidTime),datediff(max(paidTime),min(paidTime)) from data.orderinfo

where isPaid = '已支付'

group by userId having count(1) > 1

order by userId

531ada7bbdfde12003e4685d8d8cade4.png

5 统计不同年龄段用户的消费金额差异

通过表联结,给用户划分不同的年龄段,以10年为基准,过滤出生日期为1900-00-00的异常值,筛选出用户消费频次和消费金额

select o.userId,age,price,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

where isPaid = '已支付'

group by userId

order by userId

bcca980d8178d9884eccbefc08b3d4ee.png

统计出年龄段的消费频次和消费金额

select t2.age,avg(ct),avg(price) from (

select o.userId,age,price,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

where ispaid = '已支付'

group by userId, age) t2

group by age

order by age

e424f56e384f56ff910e7ed232e0e65e.png

ceil : 向上取整

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

按照用户消费总额排序

select userId,sum(price) as total from orderinfo o

where isPaid = '已支付'

group by userId

order by total desc

c99a81cebefe847c5ede376f1aa503c0.png

查看总用户数和总金额

select count(userId),sum(total) from (

select userId,sum(price) as total from orderinfo o

where isPaid = '已支付'

group by userId

order by total desc) as t

cd089813dc9c590f43fd5f35cbf18399.png

查看前20%的用户数量有多少

select count(userId)*0.2,sum(total) from (

select userId,sum(price) as total from orderinfo o

where isPaid = '已支付'

group by userId

order by total desc)as t

6585bebb1910c6d6b01f702f817eec35.png

limit限制前17000用户

select count(userId),sum(total) from (

select userId,sum(price) as total from orderinfo o

where isPaid = '已支付'

group by userId

order by total desc

limit 17129) t

f8bf526d3c8554ec8acaaf255d0f59d2.png

top20%用户的消费总额占比情况:top20%用户的消费总额/所有用户的消费总额=73.93%

top20%的用户贡献了73.93%消费额度。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值