繁体   English   中英

SQL:在具有多个group by语句的两列上进行计算

[英]SQL: calculation on two columns with multiple group by statements

我有一个包含以下各列的表:

  • user_id-包括重复项

  • product_id-包括重复项

  • 购买-给定product_id的购买次数

我的桌子看起来像这样:

   user_id  date  product_id  purchases
0        1     1           1          4
1        1     2           1          0
2        1     3           2          0
3        1     4           2          0
4        2     1           1          1
5        2     2           1          0
6        2     3           1          1
7        3     1           2          0
8        3     2           3          0
9        4     1           5          1

我的目标是计算以下指标:

按用户分组的至少购买一次产品的百分比

例如:用户1有2种产品,其中一种至少购买一次,另一种根本没有购买。 因此,指标将是至少购买一次的产品数量/每位用户的所有产品数量:1/2 * 100 = 50%

我几乎没有SQL经验,所以我没有任何可以纠正的合法代码。

我想要的输出将是这样的:

   user_id  total_products  products_with_purchases  metric
0        1               2                        1     50%
1        2               1                        1    100%
2        3               2                        0      0%
3        4               1                        1    100%

我很高兴看到一个很好的解决方案来解决这个问题。 非常感谢!

select
    user_id,
    count(distinct product_id) as total_products,
    count(distinct case when purchases > 0 then product_id end) as products_with_purchases,
    100.00 * count(distinct case when purchases > 0 then product_id end)
        / count(distinct product_id) as metric
from T as t
group by user_id

https://rextester.com/EDSY39439

您可以在一个查询中完成所有这些操作,但这是通过子查询更容易理解的一种情况-sql Optimizer应该使其速度更快。

select 
  user_id, 
  total_products,
  products_with_purchase,
  (products_with_purchase / total_products) * 100 as metric
from (
  select  -- group by user to get totals
    user_id, 
    count(product_id) as total_products,
    sum(case when purchases > 0 then 1 else 0 end) as products_with_purchase
  from ( -- group by user and product and get purchase items
    SELECT user_id, product_id, sum(purchases) as purchases
    FROM table
    GROUP BY user_id, product_id
  ) X
  group by user_id
) X2

我是Mohit Sahni,您可以使用以下SQL代码解决上述问题:

select 
user_id,
count(distinct product_id) as total_products,
sum(case when purchases = 0 then 0 else 1 end) as products_with_purchases,
((sum(case when purchases = 0 then 0 else 1 end))/count(distinct product_id))*100 as metric
from 
table
group by 
user_id

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM