我有购买活动的表'transactions'
:
customer_id, company, purchaseamount
我正在寻找一个sql命令来输出"市场渗透率"对于某个公司:
例如,我想计算:
(从公司购买的唯一customer_id的数量== 12)/(唯一customer_id的总数)
答案 0 :(得分:1)
select count(distinct customer_id) * 100 / (select count(distinct customer_id) from transactions)
from transactions
where customer_id = 12
答案 1 :(得分:1)
您可以使用条件聚合执行此操作:
select count(distinct case when company = 12 then customer_id end) / count(distinct customer_id)
from transactions;
答案 2 :(得分:0)
customer_id
的唯一编号是一行,因此您可以cross join
使用每个公司的不同计数
SELECT company, COUNT (DISTINCT customer_id) / cnt AS penetration_ratio
FROM my_table
CROSS JOIN (SELECT COUNT (DISTINCT customer_id) AS cnt
FROM my_table) t