目錄:
一壁查、將數(shù)據(jù)導(dǎo)入數(shù)據(jù)庫
二觉至、SQL--用戶消費(fèi)行為分析
1剔应、統(tǒng)計(jì)不同月份的下單人數(shù)
2、統(tǒng)計(jì)用戶三月份的回購率和復(fù)購率
3语御、統(tǒng)計(jì)男女的消費(fèi)頻次是否有差異
4峻贮、統(tǒng)計(jì)多次消費(fèi)的用戶,第一次和最后一次消費(fèi)時(shí)間的間隔
5应闯、統(tǒng)計(jì)不同年齡段的用戶消費(fèi)金額是否有差異
6纤控、統(tǒng)計(jì)消費(fèi)的二八法則,消費(fèi)的top20%用戶碉纺,貢獻(xiàn)了多少額度
一船万、將數(shù)據(jù)導(dǎo)入數(shù)據(jù)庫
目的:將兩份csv文件導(dǎo)入數(shù)據(jù)庫
步驟:建表刻撒、導(dǎo)入數(shù)據(jù)
建表
1、訂單明細(xì)表orderinfo:
2耿导、用戶表userinfo:
3声怔、導(dǎo)入數(shù)據(jù)
我是用Navicat中的導(dǎo)入向?qū)е苯訉?dǎo)入。兩個(gè)數(shù)據(jù)集分別是10萬和50萬條數(shù)據(jù)舱呻,導(dǎo)入時(shí)間時(shí)長較快醋火。
數(shù)據(jù)集更大時(shí)可以用cmd命令行導(dǎo)入、或者用KETTLE進(jìn)行抽取箱吕。我嘗試用CMD命令導(dǎo)入幾次芥驳,但一直報(bào) ERROR 1290 (HY000),嘗試幾次沒解決故作罷茬高。
二兆旬、用戶消費(fèi)行為分析
使用MySQL數(shù)據(jù)庫
分析問題:
1、統(tǒng)計(jì)不同月份的下單人數(shù)
2怎栽、統(tǒng)計(jì)用戶三月份的回購率和復(fù)購率
3爵憎、統(tǒng)計(jì)男女的消費(fèi)頻次是否有差異
4、統(tǒng)計(jì)多次消費(fèi)的用戶婚瓜,第一次和最后一次消費(fèi)時(shí)間的間隔
5宝鼓、統(tǒng)計(jì)不同年齡段的用戶消費(fèi)金額是否有差異
6、統(tǒng)計(jì)消費(fèi)的二八法則巴刻,消費(fèi)的top20%用戶愚铡,貢獻(xiàn)了多少額度
1、統(tǒng)計(jì)不同月份的下單人數(shù)
select month(paidTime),count(userID) from orderinfo
where isPaid = '已支付'
group by month(paidTime);
2胡陪、統(tǒng)計(jì)用戶三月份的復(fù)購率和回購率
(1)復(fù)購率是在本月消費(fèi)一次以上用戶的占比(區(qū)分消費(fèi)1次及1次以上的)
select count(ct) as 總消費(fèi)用戶,
sum(case when ct>1 then 1 else 0 end) as 復(fù)購用戶,
sum(case when ct>1 then 1 else 0 end)/count(ct) as 復(fù)購率
from(select userID,count(userID) as ct
from orderinfo
where isPaid = '已支付' and month(paidTime)=3
group by userID) t;
(2)回購率是三月份購買的人數(shù)四月份依舊購買
select t1.m as 月份,count(t1.m) as 本月消費(fèi)用戶,count(t2.m) as 本月回購用戶 from
(select userID,month(paidTime) as m
from orderinfo
where isPaid='已支付'
group by userID,month(paidTime)) t1
left join
(select userID,month(paidTime) as m
from orderinfo
where isPaid='已支付'
group by userID,month(paidTime)) t2
on t1.userID=t2.userID and t1.m= DATE_SUB(t2.m,INTERVAL 1 MONTH)
group by t1.m;
3沥寥、統(tǒng)計(jì)男女的消費(fèi)頻次是否有差異
select sex,avg(ct) from
(select o.userID,sex,count(*) as ct
from orderinfo o
inner join
(select * from userinfo where sex is not null ) t
on o.userID= t.userID group by o.userID,sex) t2;
4、統(tǒng)計(jì)多次消費(fèi)的用戶柠座,第一次和最后一次消費(fèi)時(shí)間的間隔
select userID , DATEDIFF(max(paidTime),min(paidTime)) as 消費(fèi)間隔
from orderinfo
where isPaid='已支付'
group by userID having count(*)>1;
5邑雅、統(tǒng)計(jì)不同年齡段的用戶消費(fèi)金額是否有差異
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 >'0000-00-00') t
on o.userID = t.userID
group by o.userID,age) t2
group by age;
6、統(tǒng)計(jì)消費(fèi)的二八法則妈经,消費(fèi)的top20%用戶淮野,貢獻(xiàn)了多少額度
select count(userID) ,sum(total) as 前20%用戶
from
(select userID,sum(price) as total
from orderinfo
where isPaid='已支付'
group by userID
order by total desc
limit 17000) t;
數(shù)據(jù)與解法參考:https://www.bilibili.com/video/BV1PE411P7Q9?p=15