计算客户订单的频率

时间:2018-04-17 16:06:42

标签: sql postgresql

我有一个Orders表,其中包含以下(简化)列:

  1. id(整数)
  2. user_id(整数)
  3. status(枚举的整数)
  4. 我正在尝试制作频率分布图。

    问:如何翻译上面的架构以获取以下列?

    1. 订单数量
    2. 用户数
    3. 示例结果将是:

      | number of orders | number of users |
      |------------------|-----------------|
      | 0                | 2000            |
      | 1                | 25              |
      | 2                | 3               |
      | 3                | 2               |
      | 4                | 1               |
      

      当然,如果有人放置" 5"订单,我想要"订单数量"列将在未来相应增加。

      到目前为止,我只能计算出多个订单的用户:

      SELECT COUNT(*) as "Repeat Customers"
      FROM (
          SELECT  o.user_id
          FROM    Orders o
          WHERE  o.status >= 4
          GROUP   BY o.user_id
          HAVING  COUNT(DISTINCT o.user_id) = 1 AND COUNT(*) > 1
          ORDER BY o.user_id
      

      更新:对不起,伙计们。我似乎犯了一个错误。由于我们只查看订单表,因此我们无法计算0订单的用户数,因为这样做的逻辑方式是all_user_ids - user_ids_in_orders_table

      正确的答案架构是:

      | number of orders | number of users |
      |------------------|-----------------|
      | 1                | 25              |
      | 2                | 3               |
      | 3                | 2               |
      | 4                | 1               |
      

2 个答案:

答案 0 :(得分:1)

这是对问题原始版本的正确回答。

如果要获得0的计数,则使用条件聚合:

select orders_per_user, count(*)
from (select user_id, sum( (status >= 4)::int ) as orders_per_user 
      from orders 
      group by user_id
     ) o
group by orders_per_user
order by orders_per_user

答案 1 :(得分:0)

我认为这个SQL可以帮到你:

SELECT 
    nOrders AS "number of orders",
    COUNT(nUsers) AS "number of users"
FROM
    (SELECT 
        COUNT(o.id) AS nOrders, o.user_id AS nUsers
    FROM
        Orders o
    WHERE
        o.status >= 4
    GROUP BY o.user_id) AS a
GROUP BY nOrders