MySQL业务分析实例

* 统计不同月份的下单人数

select date_format(paidTime, '%Y-%m') as m, count(distinct userId) as num from data.orderinfo
where isPaid = '已支付'
group by m;

* 统计用户三月份的回购率和复购率 (回购率:三月份购买后,四月次依旧购买的客户占比    复购率:三月份购买次数在1次以上客户数占总客户数的比例)

***********复购率************

select count(if(t.ct>1, 1, null)), count(1), count(if(t.ct >1, 1, null))/count(1) as percent from (
	select userId, count(1) as ct from data.orderinfo
	where isPaid = '已支付'
	group by userId) as t;
    
***********回购率************

select count(t1.userId) as m1, count(t2.userId) as m2, count(t2.userId)/count(t1.userId) as percent from (
	select userId, date_format(paidTime, '%Y-%m-01') as m from data.orderinfo
	where isPaid = '已支付'
	group by userId, m) as t1
left join (
	select userId, date_format(paidTime, '%Y-%m-01') as m from data.orderinfo
	where isPaid = '已支付'
	group by userId, m) as t2
on t1.userId = t2.userId and t1.m = date_sub(t2.m, interval 1 month)
group by t1.m;


* 统计男女用户的消费频次是否有差异

select t.sex, count(t.userId), count(distinct t.userId) from (
	select o.userId, u.sex from data.orderinfo as o
	inner join data.userinfo as u
	on o.userId = u.userId
	where isPaid = '已支付' and u.sex <> '') as t
group by t.sex;

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

select userId, datediff(m2, m1) from (
	select userId, count(userId) as num, max(paidTime) as m2, min(paidTime) as m1 from data.orderinfo
	where isPaid = '已支付'
	group by userId) as t
where num > 1;


* 统计不同年龄段,用户的消费金额是否有差异

select t.ageClass, sum(o.price) as totalPrice, count(distinct o.userId) as ClientNum, sum(o.price)/count(distinct o.userId) as AverageExpense from data.orderinfo as o
inner join (
	select
    userId,
    (2017-year(birth)) as age,
    case
		when (2017-year(birth)) <10 then '0~10'
		when (2017-year(birth)) between 10 and 20 then '10~20'
		when (2017-year(birth)) between 20 and 30 then '20~30'
		when (2017-year(birth)) between 30 and 40 then '30~40'
		when (2017-year(birth)) between 40 and 50 then '40~50'
		when (2017-year(birth)) between 50 and 60 then '50~60'
		when (2017-year(birth)) between 60 and 70 then '60~70'
		when (2017-year(birth)) between 70 and 80 then '70~80'
		else 'over 80'
	end as ageClass
    from data.userinfo
	where year(birth) > 1900) as t
on o.userId = t.userId
where isPaid = '已支付'
group by ageClass


* 统计消费的二八法则,消费的top 20%用户,贡献了多少额度

select count(userId), sum(total) from (
	select userId, sum(price) as total from data.orderinfo
	where isPaid = '已支付'
	group by userId
    order by total desc) as t

select count(userId), sum(total) from (
	select userId, sum(price) as total from data.orderinfo
	where isPaid = '已支付'
	group by userId
    order by total desc
    limit 17000) as t
    

猜你喜欢

转载自blog.csdn.net/weixin_40362097/article/details/80609020