2017-08-04 67 views
0

我想知道是否可以使用一個查詢在兩個不同級別上彙總信息? 例如,我有表格,並希望獲得購買特定商品的客戶的唯一數量,以及每個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,從列表 中逐項計數(distinct customer_id)爲customers_count_per_item ID

我還需要總獨特的客戶數如下: SELECT COUNT(不同CUSTOMER_ID)從表

total_customers所以我需要所有這些信息在一排。要做到這一點的唯一方法是將這3個查詢(可能作爲子查詢)結合起來,還是有更有效的方式去做到這一點?

回答

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

是否在蜂巢這項工作? – vkaul11

+0

您在這裏看到的是從執行的代碼複製粘貼,所以答案是 - 「是」。無論如何,你應該在你自己的系統上用你自己的Hive版本進行測試。 –

相關問題