简体   繁体   English

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

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

I have a table which has the following columns: 我有一个包含以下各列的表:

  • user_id - includes duplicates user_id-包括重复项

  • product_id - includes duplicates product_id-包括重复项

  • purchases - number of purchases of given product_id 购买-给定product_id的购买次数

My table looks somewhat like this: 我的桌子看起来像这样:

   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

My goal is to calculate the following metric: 我的目标是计算以下指标:

% of products that were purchased at least once, grouped by user 按用户分组的至少购买一次产品的百分比

For example: user 1 had 2 products, one of them got purchased at least once, the other one did not get purchased at all. 例如:用户1有2种产品,其中一种至少购买一次,另一种根本没有购买。 So the metric would be the number of products that got purchased at least once / number of all products per user: 1/2 * 100 = 50% 因此,指标将是至少购买一次的产品数量/每位用户的所有产品数量:1/2 * 100 = 50%

I have little SQL experience so I do not have any legitimate code that could be corrected. 我几乎没有SQL经验,所以我没有任何可以纠正的合法代码。

My desired output would be like this: 我想要的输出将是这样的:

   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%

I would appreciate seeing a good practice solution to this problem. 我很高兴看到一个很好的解决方案来解决这个问题。 Many thanks! 非常感谢!

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 https://rextester.com/EDSY39439

You can do this all in one query but this is the type of situation where it is easier to understand with sub-queries -- sql optimizer should make it fast. 您可以在一个查询中完成所有这些操作,但这是通过子查询更容易理解的一种情况-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

I Am Mohit Sahni you can solve the above problem with the below SQL Code: 我是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