怎么用mysql来统计消费金额限制_mysql——用户消费行为分析
分析导览:分析目的
1-统计不同性别的消费频次、消费金额
2-统计不同年龄段用户的消费金额
3-统计不同月份的消费金额
4-统计多次消费的用户,第一次和最后一次消费时间的间隔
5-统计不同年龄段的用户消费金额
6-统计消费的二八法则,消费top20%的用户,贡献了多少额度分析过程
(一)文件导入
文件导入具体方法见如何在mySQL中文件导入CSV文件,这里就不在赘述。
主要步骤分为建表、导入文件,为了避免遇到很多坑,可以间我前一篇文章,这里的数据量有10w条,用命令行导入很方便。
创建的两张表的字段:
user:userid, sex,birth
orderinfo: orderid,userid,ispaid,price,paidtime
(二)用户消费者行为分析
1、统计不同月份的下单人数
select CONCAT_WS('-',left(paidtime,4),mid(paidtime,6,1)) as month_,count(distinct userid) as paidorder from test.orderinfo
where ispaid='已支付'
group by left(paidtime,4),mid(paidtime,6,1);
注:这里用到字符串拼接和选取字符串的函数,concat_ws,left,mid,还可以用substring函数。
从结果中看出,三四月份的下单人数最多,5月份竟然只有6个人。
2、统计男女的消费频次
思路是先将两个表联结,求出每个id的下单次数,再在该表基础上对男女进行分组,统计。这里有个需要注意的,我们可以统计下单的男女频次,也可以统计已支付和未支付的频次。
select a.userid,b.sex,count(1)as cs from test.orderinfo a,test.user b where a.userid=b.userid and ISPAID="已支付" and b.sex is not null
group by a.userid;
select c.sex ,round(avg(c.cs),2) as 平均次数 from (select a.userid,b.sex,count(1)as cs from test.orderinfo a,test.user b where a.userid=b.userid and ISPAID="已支付" and b.sex is not null
group by a.userid,b.sex) c group by c.sex ;
以上是一年男女平均频次,以下是以月为单位男女的平均频次
select t.month,t.sex,round(avg(t.ct),2) as 频次 from (
select left(a.paidtime,6) as month,a.userid,count(a.userid) as ct,b.sex
from test.orderinfo a,test.user b where a.ISPAID='已支付' and a.userid=b.userid
and b.sex is not null and left(a.paidtime,6) !='2016/5'
group by left(a.paidtime,6),a.userid) t
group by t.month,t.sex ;
3、统计不同年龄段的消费频次
先给不同年纪用户划分年龄段,这里用到一个新函数timestampdiff。返回时间序列的最大、最小时间差,由于我的时间是字符串类型,我用现在的时间-年份字段得到年龄。
CEIL(X) 返回不小于X的最小整数值。(天花板)
FLOOR(X) 返回不大于X的最大整数值。(地板)
这里由于我的时间是字符串,所以我用case when多条件查询,
select userid,year(now())-left(birth,6) as age,
case when year(now())-left(birth,6)<=12 then '0-12岁'
when year(now())-left(birth,6)<=25 then '13-25岁'
when year(now())-left(birth,6)<=30 then '26-30岁'
when year(now())-left(birth,6)<=50 then '30-50岁'
when year(now())-left(birth,6)<=70 then '51-70岁'
else 'over70' end as al
from test.user ;
先求出每个age对应的年龄段,再求出每个年龄段的次数。
select t.al,count(t.ct) as 次数 from
(select a.userid,count(a.userid) as ct,year(now())-left(b.birth,6) as age,
case when year(now())-left(b.birth,6)<=12 then '0-12岁'
when year(now())-left(b.birth,6)<=25 then '13-25岁'
when year(now())-left(b.birth,6)<=30 then '26-30岁'
when year(now())-left(b.birth,6)<=50 then '30-50岁'
when year(now())-left(b.birth,6)<=70 then '51-70岁'
else 'over70' end as al
from test.orderinfo a,test.user b
where a.userid= b.userid and a.ispaid='已支付'
and year(now())-left(b.birth,6) !='2020'
group by a.userid) t
group by t.al;
select t.al,avg(t.ct) as 频次 from
(select a.userid,count(a.userid) as ct,year(now())-left(b.birth,6) as age,
case when year(now())-left(b.birth,6)<=12 then '0-12岁'
when year(now())-left(b.birth,6)<=25 then '13-25岁'
when year(now())-left(b.birth,6)<=30 then '26-30岁'
when year(now())-left(b.birth,6)<=50 then '30-50岁'
when year(now())-left(b.birth,6)<=70 then '51-70岁'
else 'over70' end as al
from test.orderinfo a,test.user b
where a.userid= b.userid and a.ispaid='已支付'
and year(now())-left(b.birth,6) !='2020'
group by a.userid) t
group by t.al
order by avg(t.ct) desc;
4、消费top20%的用户贡献了多少消费额度
计算每个用户消费的总额,
select userid,sum(price) as sum from test.orderinfo
where ispaid ="已支付" group by userid order by sum desc;
top20用户的数量和消费额度
select floor(count(1)*0.2) from (select userid,sum(price) as sum
from test.orderinfo where ispaid ="已支付"
group by userid order by sum desc) t;
select sum(t.sum) as top20 from
(select userid,sum(price) as sum from test.orderinfo where ispaid ="已支付"
group by userid order by sum desc limit 17129 ) t;
计算全部的消费额
用top20的消费额/消费总额=2700/3683 约为73.9%。
select a.userid,a.sum,round(a.sum/(select sum(price) as total from test.orderinfo) ,4) as 贡献率 from
(select userid,sum(price) as sum
from test.orderinfo where ispaid ="已支付"
group by userid order by sum desc) a
order by a.sum desc;
5、统计多次消费用户,第一次和最后一次的消费间隔
select t.userid,timestampdiff(day,min(t.time),max(t.time)) as timediif from
(select userid,str_to_date(concat(left(paidtime,4),'-0',mid(paidtime,6,1),'-0',mid(paidtime,8,1)),'%Y-%m-%d') as time
from test.orderinfo) t
where t.time is not null
group by t.userid
having timestampdiff(day,min(t.time),max(t.time))!= 0;
这里用到了字符串拼接函数concat(),时间间隔函数timestampdiff(),以天为单位,消费多次的用户的时间间隔
6、统计三月份消费的复购率和回购率
复购率:本月消费一次以上用户的占比
思路:对用户进行分组,用总的用户数>1的数量/总数
select count(if (t.ct>1,1,null))/count(1) as 复购率
from (select userid,count(userid) as ct from test.orderinfo where ispaid="已支付" and mid(paidtime,6,1)="3"
group by userid) t
每个月购买用户的复购率
select s.time,s.复购人数, round(s.复购人数/(select count(distinct userid) as zs from test.orderinfo
where ispaid='已支付'),2) as 复购率
from (select a.userid,a.time,count(if(a.ct>1,1,0)) as 复购人数 from
(select userid,count(userid) as ct,left(paidtime,6) as time from
test.orderinfo where ispaid='已支付'
group by left(paidtime,6),userid) a
group by a.time) s
回购率:三月购买的人,4月依旧购买。
思路:先算出每月购买的用户,分成两个表,将两个表左联结。以时间为计数求出本月购买人数和下月回购人数。
select a.t1,count(a.t1) as 本月购买人数,count(b.t2) as 下月回购人数, round(count(b.t2)/count(a.t1),3) as 回购率
from
(select userid,left(paidtime,6) as t1 from test.orderinfo
where ispaid='已支付'
group by userid,left(paidtime,6)) a
left join (
select userid,left(paidtime,6) as t2 from test.orderinfo
where ispaid='已支付'
group by userid,left(paidtime,6)) b
on a.userid =b.userid and a.t1 = concat(left(b.t2,5),mid(b.t2,6,1)-1)
group by a.t1;
总结从每个月的订单数来看,3、4月份的订单数较多,5月份只有几个。
从每个月以及全年的男女消费频次来看,男女消费频次是差不多的,差别不大。
从各年龄段的消费频次来看,70岁以上的频次最高,其次是30-50岁用户,而30-50岁用户消费的总次数最高。
根据二八法则,分析得出top20%消费的用户消费总额占总额的73%。
用户第一次消费和最后一次消费的时间间隔多数为1天,说明用户生命周期较短,回头客较少。
3、4月份的用户复购率较大,,约为50-60%,,3月购买用户在4月的回购率较高,而4月回购率就非常低了。
利用sql和现有数据得出以上分析结论,可以从结论中推导为什么4月用户回购率变少了,用户流失较大,i由于数据有限,因此还需要作进一步分析。
本次用到的新韩淑:
concat()
str_to_date() 函数,(str1,str2....,'%Y%m%d')
case when then
…… end
窗口函数rank()和dense_rank() 两者的区别
更新表
update table tablename set
怎么用mysql来统计消费金额限制_mysql——用户消费行为分析相关推荐
- mysql设置user权限允许远程_mysql 用户及权限管理 允许远程连接
mysq,功能强大的关系型数据库,它的用户管理在开发过程中当然也尤其重要,接下来就看看mysql的用户管理 1.登录数据库 mysql -uroot -p 回车 输入密码... 回车 2.登录成功后, ...
- mysql 报表统计sql使用实例_mysql 案例~mysql元数据的sql统计
一 简介:今天我们来收集下提取元数据的sql 二 前沿: information_schema 引擎 memory 元数据收集表 三 sql语句: 1#没有使用索引的表统计 SELECT t.TAB ...
- mysql弱口令是什么意思_MYSQL用户root密码为弱口令又一攻击方法
转自论坛作者未知. 请与我们联系. 前天,我发了一张关于MYSQL用户ROOT密码为空的贴,在网上我也找到一些利用此漏洞的方法,一般就是写一个ASP或PHP的后门,不仅很麻烦,而且还要猜解网站的目录, ...
- mysql 计算近30天总金额_MySQL数分实战:咖啡店精细化运营
前期,我们已经对MySQL的增删改查.以及多种查询方式和窗口.视图进行了解学习.本次我们利用前期所积累的知识进行数据分析实战. 本次实战数据源来自: Supersetsuperset.workeri ...
- mysql求回购率_用户行为分析——回购率、复购率(SQL、Python求解)
有一个多月没有用Python了,有些生疏o(╥﹏╥)o.通过秦路老师的一道题目,分别使用sql和python求解,顺便复习下python点,重点关注[复购率].[回购率]的解法 ☞秦路老师视频讲解(使 ...
- 淘宝用户行为分析MySQL
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 淘宝用户行为分析MySQL 一.数据清洗 二.总体情况 1.UV.PV.人均浏览次数 2.复购率.跳失率 三.用户行为分析 1.行为转 ...
- mysql查每个月的收入_mysql 查询每个人、每个月的消费金额及每年的消费总额
最近按需求实现会员年报表, 假设现有会员表member.订单表i_order, member表字段(id, member_name, xxxx), i_order表字段(id,member_id,ha ...
- Mysql查询统计按年月日(年、季、月、周、日)
Mysql查询统计:按年.季.月.周.日 按年统计交易金额. SELECTsum(goods_fee),YEAR (FROM_UNIXTIME(create_time, '%Y-%m-%d')) FR ...
- 餐馆(餐馆有n张桌子,每张桌子有一个参数a 可容纳的最大人数; 有m批客人,每批客人有两个参数:b人数,c预计消费金额。 不允许拼桌的情况下,选择其中一部分客人,使得总预计消费金额最大)
餐馆 某餐馆有n张桌子,每张桌子有一个参数:a 可容纳的最大人数: 有m批客人,每批客人有两个参数:b人数,c预计消费金额. 在不允许拼桌的情况下,请实现一个算法选择其中一部分客人,使得总预计消费金额 ...
最新文章
- mongodb中简单的根据时间过滤进行查询
- 进程注入的研究与实现
- CAShapeLayer + UIBezierPath
- 017_Upload上传
- 最大公约数gcd和Win32版本实现
- 对话阿里云李飞飞:下一代企业级数据库6大技术方向
- excel可编码java,Java关于前后台传输中文乱码以及Excel下载乱码解决
- 从安全和不安全两个角度,教你如何发布对象(含各种单例代码)
- PHP Warning: date(): It is not safe to rely on the system’s timezone settings
- Emacs + gdb单步调试汇编代码
- unity 多选枚举
- 2016.05.17开通自己的博客
- SpringMVC在返回JSON数据时出现406错误解决方案
- Python爬虫BeautifulSoup4小记
- HALCON灰度值变换
- 计算机桌面锁屏设置,怎么设置电脑锁屏
- 通过“单键锁配置法”实现访问控制
- Jquery hiden() 与 .css('visibility','visible') 区别
- Python复习的知识点
- 冯唐:老聃的金字塔原则