水滴 sql
一张表name_id,product,pay_time
求购买两款商品以上的用户占比
select name_id ,count(distinct product)
from table
group by name_id
having count(distinct product)>=2
以上是列出符合的用户
要想求占比的话该怎么写呢,求大佬解惑
求购买两款商品以上的用户占比
select name_id ,count(distinct product)
from table
group by name_id
having count(distinct product)>=2
以上是列出符合的用户
要想求占比的话该怎么写呢,求大佬解惑
全部评论
你好,请问数仓会有算法题吗
SELECT COUNT(DISTINCT name_id) * 1.0 / COUNT(DISTINCT name_id, product) AS ratio
FROM table
GROUP BY name_id
HAVING COUNT(DISTINCT product) >= 2
这是用chatgpt跑出来的结果,你来试试的
按照name_id分组select购买的商品数量作为临时表,然后再求用户总数,和用case when求出购买商品数量大于2的用户数,round一下就行
相关推荐
点赞 评论 收藏
分享
点赞 评论 收藏
分享
03-20 11:46
黑龙江大学 Java 点赞 评论 收藏
分享
点赞 评论 收藏
分享