mysql分组统计及占比

面试题:

统计各个渠道的人数占比:
在这里插入图片描述

sql实现:

select 
	channel, 
	g_cnt/cnt
from(select *
	 from(select channel, count(distinct uid) g_cnt from user_profile group by channel)t1
	 join (select count(*)cnt from user_profile)t2 on 1=1
	)tt

查询结果:
在这里插入图片描述


版权声明:本文为weixin_42656010原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。