簡體   English   中英

在一個查詢中匯總來自兩個不同級別的信息

[英]aggregating information from two different levels in one query

我想知道是否可以僅使用一個查詢在兩個不同級別上匯總信息? 例如,我有一張桌子,想要獲取購買某項商品的唯一客戶數,以及每位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

我還可以按如下方式獲取單個列的customer_count_per_item列:從表組中按item_id選擇item_id,count(distinct customer_id)作為customer_count_per_item

我還需要如下所示的唯一客戶總數:從表中選擇count(distinct customer_id)作為total_customers

所以我需要所有這些信息。 唯一的方法是組合這3個查詢(可能作為子查詢),或者是否有更有效的方法來執行此操作?

視窗功能

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM