在一个查询中聚合来自两个不同级别的信息

时间:2017-08-04 22:19:12

标签: sql hive

我想知道是否可以仅使用一个查询来汇总两个不同级别的信息? 例如,我有桌子,想要获得购买特定商品的唯一客户数量,以及每个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:     select item_id,count(distinct customer_id)as customers_count_per_item     从表     按item_id分组

我还需要总的唯一客户数量如下:    从表

中选择count(distinct customer_id)作为total_customers

所以我需要一行中的所有这些信息。这样做的唯一方法是将这3个查询(可能是子查询)组合在一起,还是有更有效的方法来实现这一目标?

1 个答案:

答案 0 :(得分:0)

窗口功能

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               |
+---------+-------------+-------------------------+--------------------------+-----------------+