【问题标题】:aggregating information from two different levels in one query在一个查询中聚合来自两个不同级别的信息
【发布时间】:2018-01-12 22:47:02
【问题描述】:

我想知道是否可以仅使用一个查询来聚合两个不同级别的信息? 例如,我有一张表,想要获得购买某个商品的唯一客户数量以及每个 customer_id 购买的某个 item_id 的数量除以客户总数。

Table
customer_id   item_id    bought_date
   abc           12        2017-01-01
   def           23        2017-01-08
   abc           12        2017-01-02
   abc           13        2017-01-02
   ghi           23        2017-01-02

我想要输出

item_id   customer_id   item_count_per_customer customers_probability_per_item total_customers
 12         abc               2                      1        3
 13         abc               1                      1        3
 23         def               1                      2        3
 23         ghi               1                      2.

我可以按如下方式获取单个列 item_count_per_customer:

select item_id, customer_id, count(1) as item_count_per_customer
from table
group by item_id, customer_id

我还可以获取单个列customers_count_per_item,如下所示: 选择 item_id,count(distinct customer_id) 作为 customers_count_per_item 从表 按 item_id 分组

我还需要如下的唯一客户总数: 从表中选择 count(distinct customer_id) 作为 total_customers

所以我需要将所有这些信息集中在一起。做到这一点的唯一方法是将这 3 个查询(可能作为子查询)结合起来,还是有更有效的方法来做到这一点?

【问题讨论】:

    标签: sql hive


    【解决方案1】:

    窗口函数

    select      item_id
               ,customer_id
               ,count(*)                                                as item_count_per_customer 
               ,count(distinct customer_id) over (partition by item_id) as customers_count_per_item
               ,count(distinct customer_id) over()                      as total_customers
    
    from        mytable
    
    group by    item_id
               ,customer_id
    ;
    

    +---------+-------------+-------------------------+--------------------------+-----------------+
    | item_id | customer_id | item_count_per_customer | customers_count_per_item | total_customers |
    +---------+-------------+-------------------------+--------------------------+-----------------+
    | 23      | ghi         | 1                       | 2                        | 3               |
    +---------+-------------+-------------------------+--------------------------+-----------------+
    | 23      | def         | 1                       | 2                        | 3               |
    +---------+-------------+-------------------------+--------------------------+-----------------+
    | 13      | abc         | 1                       | 1                        | 3               |
    +---------+-------------+-------------------------+--------------------------+-----------------+
    | 12      | abc         | 2                       | 1                        | 3               |
    +---------+-------------+-------------------------+--------------------------+-----------------+
    

    【讨论】:

    • 您在此处看到的是已执行代码的复制粘贴,因此答案是 - “是”。在任何情况下,您都应该在您自己的系统上使用您自己的 Hive 版本对其进行测试。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-19
    • 2011-06-02
    • 2021-07-30
    • 2013-11-22
    • 2012-02-18
    • 2015-01-13
    • 2019-08-07
    相关资源
    最近更新 更多