【问题标题】:Percentage of total purchase for qualifying customers per date period每个日期期间合格客户的总购买百分比
【发布时间】:2020-07-09 08:58:45
【问题描述】:

我需要找出每个月购买巧克力的顾客的巧克力销量百分比。 以下面的示例表为例,它只有一个 Month 条目:

Cust_Id     Customer    Quantity    Item        Amount in $     Date         Month and year
1           Maria       2           chocolate   10                 01/01/2020   1-2020
1           Maria       1           banana      5                  01/01/2020   1-2020
1           Maria       3           cookies     3                  01/01/2020   1-2020
2           Carlos      5           cookies     10                 01/01/2020   1-2020
2           Carlos      3           banana      3                  01/01/2020   1-2020
3           Jose        1           banana      5                  01/01/2020   1-2020
3           Jose        1           chocolate   3                  01/01/2020   1-2020
4           Anne        10          chocolate   20                 01/01/2020   1-2020
4           Anne        1           banana      5                  01/01/2020   1-2020
4           Anne        10          cookies     15                 01/01/2020   1-2020

在 2020 年 1 月购买了巧克力的客户(Maria、Jose 和 Anne,但不是 Carlos)中,巧克力的总购买量(百分比)有多少?

通过excel中的数据透视表解决:

Month   1 2020
Customer Name   SUM of Amount in $  Total amount of purchase    Overall %
Anne            20                  40  
Jose            3                   8   
Maria           10                  18  
Grand Total     33                  66                         50.00%

日期“1-2020”的解是50%

如何确定每个日期期间仅通过 SQL 购买巧克力的客户在每个日期期间售出的巧克力百分比?

mysql> select * from orders where date = '1-2020';
+----------+-------------+---------------+----------+-----------+--------+--------+
| order_id | customer_id | customer_name | quantity | item      | amount | date   |
+----------+-------------+---------------+----------+-----------+--------+--------+
|        1 |           1 | Maria         |        2 | chocolate |     10 | 1-2020 |
|        2 |           1 | Maria         |        1 | banana    |      5 | 1-2020 |
|        3 |           1 | Maria         |        3 | cookies   |      3 | 1-2020 |
|        4 |           2 | Carlos        |        5 | cookies   |     10 | 1-2020 |
|        5 |           2 | Carlos        |        3 | banana    |      3 | 1-2020 |
|        6 |           3 | Jose          |        1 | banana    |      5 | 1-2020 |
|        7 |           3 | Jose          |        1 | chocolate |      3 | 1-2020 |
|        8 |           4 | Anne          |       10 | chocolate |     20 | 1-2020 |
|        9 |           4 | Anne          |        1 | banana    |      5 | 1-2020 |
|       10 |           4 | Anne          |       10 | cookies   |     15 | 1-2020 |
+----------+-------------+---------------+----------+-----------+--------+--------+
10 rows in set (0.00 sec)

下面的查询几乎是正确的,只是它显示一月份的总额为 79 美元,而正确的金额是 66 美元(卡洛斯一月份没有买巧克力)

mysql> select date,     sum(case when item = 'chocolate' then amount end) as chocolate_amount,     sum(amount) as total_amount,     (sum(case when item = 'chocolate' then amount end) / sum(amount)) as percentage from orders where date = '1-2020' group by date having sum(case when item = 'chocolate' then 1 else 0 end) > 0;
+--------+------------------+--------------+------------+
| date   | chocolate_amount | total_amount | percentage |
+--------+------------------+--------------+------------+
| 1-2020 |               33 |           79 |     0.4177 |
+--------+------------------+--------------+------------+
1 row in set (0.00 sec)

同样,这里的正确计算应该是 33 / 66 = 50% 谢谢

【问题讨论】:

    标签: sql netsuite domo


    【解决方案1】:

    您可以使用条件聚合:

    select customer, 
           sum(case when item = 'chocolate' then amount end) as chocolate_amount,
           sum(amount) as total_amount,
           (sum(case when item = 'chocolate' then amount end) /
            sum(amount)
           ) as chocolate_ratio
    from t
    where date >= '2020-01-01' and date < '2020-02-01'
    group by customer
    having sum(case when item = 'chocolate' then 1 else 0 end) > 0
    

    【讨论】:

    • 谢谢,当表格有很多很多财政季度时,这会起作用吗?
    • 另外,我需要找到总体百分比,即 1 月份的 50%
    • 谢谢@Gordon Linoff,这个查询几乎给了我我想要的,除了这里的total_amount 包括每个时期没有购买巧克力的客户。我已经编辑了帖子并添加了一个示例。请问能再看看吗?谢谢
    • @mmenschig 。 . .我不明白你的问题。如果客户没有购买巧克力,则应通过having 子句过滤掉该客户。
    • 我在下面添加了一个基于您提供的答案的答案。谢谢,很有帮助
    【解决方案2】:

    感谢 Gordon Linoff,我能够修改他的查询以提供所需的结果。虽然不是 100% 正确,但他的询问让我走上了正确的道路。

    对于任何想知道查询和结果是什么的人:

    查询

    select o.date,
        sum(case when item = 'chocolate' then amount end) as chocolate_amount,
        t2.total_purchases,
        (sum(case when item = 'chocolate' then amount end) / t2.total_purchases) as percentage
    from orders o
    join (
        select date, sum(amount) as total_purchases
            from orders o1
            join (
                select distinct date, customer_id 
                from orders o2
                where item = 'chocolate'
            ) as chocolate_customers
    using (date)
    where o1.customer_id in (chocolate_customers.customer_id)
    group by 1
    ) as t2 using (date)
    group by date
    having sum(case when item = 'chocolate' then 1 else 0 end) > 0;
    

    结果

    +--------+------------------+-----------------+------------+
    | date   | chocolate_amount | total_purchases | percentage |
    +--------+------------------+-----------------+------------+
    | 1-2020 |               33 |              66 |     0.5000 |
    | 2-2020 |               10 |              15 |     0.6667 |
    | 3-2020 |               55 |              62 |     0.8871 |
    +--------+------------------+-----------------+------------+
    3 rows in set (0.00 sec)
    

    再次感谢

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-09
      • 1970-01-01
      • 1970-01-01
      • 2021-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多