计算 table 不同部分的多个平均值?

Calculating multiple averages across different parts of the table?

我有以下 transactions table:

customer_id purchase_date   product         category        department      quantity    store_id
    1       2020-10-01      Kit Kat         Candy           Food                2       store_A
    1       2020-10-01      Snickers        Candy           Food                1       store_A
    1       2020-10-01      Snickers        Candy           Food                1       store_A
    2       2020-10-01      Snickers        Candy           Food                2       store_A
    2       2020-10-01      Baguette        Bread           Food                5       store_A
    2       2020-10-01      iPhone          Cell phones     Electronics         2       store_A
    3       2020-10-01      Sony PS5        Games           Electronics         1       store_A

我想计算购买产品的平均数量(对于 table 中的每个 product)。我还希望通过分别计算同一 categorydepartment 中的所有产品来计算每个 category 和每个 department 的平均值。应注意将 独特客户 和大于 0 的产品 quantity 分开(0 数量表示退款,不应计入)。

所以基本上,输出 table 如下所示:

...其中 store_idaverage_level_type 是分区列。

有没有一种方法可以一次性完成交易 table?还是我需要将我的方法分解为多个步骤?

谢谢!

如何使用“union all”如下 -

Select store_id, 'product' as average_level_type,product as id, sum(quantity) as total_quantity,
Count(distinct customer_id) as unique_customer_count, sum(quantity)/count(distinct customer_id) as average
from transactions
where quantity > 0
group by store_id,product
Union all
Select store_id, 'category' as average_level_type, category as id, sum(quantity) as total_quantity,
Count(distinct customer_id) as unique_customer_count, sum(quantity)/count(distinct customer_id) as average
from transactions
where quantity > 0
group by store_id,category
Union all
Select store_id, 'department' as average_level_type,department as id, sum(quantity) as total_quantity,
Count(distinct customer_id) as unique_customer_count, sum(quantity)/count(distinct customer_id) as average
from transactions
where quantity > 0
group by store_id,department;

如果你想避免在这种情况下使用 union all,你可以使用 rollup() 或 group by grouping sets() 来实现相同的目的,但查询会稍微复杂一些,以获取输出您在问题中显示的确切格式。

编辑:下面是如何使用分组集来获得相同的输出 -

Select store_id,
       case when G_ID = 3 then 'product' 
            when G_ID = 5 then 'category'
            when G_ID = 6 then 'department' end As average_level_type,
       case when G_ID = 3 then product 
            when G_ID = 5 then category
            when G_ID = 6 then department end As id,
       total_quantity,
       unique_customer_count,
       average
from            
    (select store_id, product, category, department, sum(quantity) as total_quantity, Count(distinct customer_id) as unique_customer_count, sum(quantity)/count(distinct customer_id) as average, GROUPING__ID As G_ID
    from transactions
    group by store_id,product,category,department
    grouping sets((store_id,product),(store_id,category),(store_id,department))
    ) Tab
order by 2    
;