【问题标题】:SQL - Aggregate same column through different criteriaSQL - 通过不同的条件聚合相同的列
【发布时间】:2017-08-18 01:54:12
【问题描述】:

假设我有一个表格 Orders,看起来像这样,

|country| customer_id | order_id |
| CA    | 5           |     3    |
| CA    | 5           |     4    |
| CA    | 6           |     5    |
| CA    | 6           |     6    |
| US    | 2           |     7    |
| US    | 7           |     8    |
| US    | 7           |     9    |
| US    | 7           |    10    |
| US    | 2           |    11    |

我想写一个查询来填充一个表,

| country | customers_w_2_orders | customers_w_2_plus_orders |
| CA      | 2                    | 0                         |
| US      | 1                    | 1                         |

它按国家/地区汇总了有 2 个订单的客户数量和有 3 个订单的客户数量。

这就是我所做的,但它没有给出我想要的结果..

SELECT country, count(*) as cnt1, count(*) as cnt2 
FROM Orders 
GROUP BY country 
HAVING cnt1=2 AND cnt2>2;

【问题讨论】:

    标签: mysql sql


    【解决方案1】:
    SELECT country,
           (select count(distinct(customer_id)) from Orders o where o.country = Orders.country and (select count(*) from Orders o2 where o2.country = orders.country and o2.customer_id = o.customer_id) = 2) as customers_w_2_orders,
           (select count(distinct(customer_id)) from Orders o where o.country = Orders.country and (select count(*) from Orders o2 where o2.country = orders.country and o2.customer_id = o.customer_id) > 2) as customers_w_2_plus_orders
      FROM Orders 
     GROUP BY country;
    

    【讨论】:

      【解决方案2】:
      declare @orders table (country char(2), customer_id int, order_id int);
      insert into @orders values
      ('CA', 5, 3),
      ('CA', 5, 4),
      ('CA', 6, 5),
      ('CA', 6, 6),
      ('US', 2, 7),
      ('US', 7, 8),
      ('US', 7, 9),
      ('US', 7, 10),
      ('US', 2, 11);
      
      select country,
             sum(case when num_orders <= 2 then 1 else 0 end) as cust_w_2_orders,
             sum(case when num_orders > 2 then 1 else 0 end) as cust_2_plus_orders
      from (
            select country, customer_id, count(*) num_orders
            from   @orders
            group by country, customer_id
           ) x
      group by country;
      GO
      
      国家 | cust_w_2_orders | cust_2_plus_orders :-------- | --------------: | -----------------: 加利福尼亚州 | 2 | 0 美国 | 1 | 1

      dbfiddle here

      【讨论】:

      • 不会得到 OP 想要的格式。您将生成一个稀疏表。
      • 您提供的查询在 0.5 毫秒内完成,优于其他答案。谢谢!
      • 我很高兴能够提供帮助。
      【解决方案3】:

      首先构建一个表,其中包含每个客户以及他们在每个国家/地区拥有的订单数量,其中每一行是country, customer_id, number_of_orders

      现在您可以通过在派生表上分组来计算 number_of_orders 是 2 或大于 2 的频率

      select country, sum(num_orders = 2), sum(num_orders > 2)
      from (
          select country, customer_id, count(*) as num_orders
          from Orders
          group by country, customer_id
      ) t group by country
      

      【讨论】:

        猜你喜欢
        • 2021-04-17
        • 2020-07-09
        • 1970-01-01
        • 2020-01-14
        • 1970-01-01
        • 1970-01-01
        • 2019-04-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多