计算表格中不同部分的多个平均值?

问题描述

我有以下transactions表:

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

我想计算购买的平均产品数量(对于表中的每个product)。我还希望通过分别考虑同一categorydepartment中的所有产品来计算每个category和每个department的平均值。应注意划分唯一客户,并且产品quantity大于0(数量为0表示退款,因此不予考虑)。

基本上,输出表如下:

enter image description here

...其中store_idaverage_level_type是分区列。

有没有一种方法可以一次通过交易表来实现这一目标?还是需要将我的方法分解为多个步骤?

谢谢!

解决方法

如何按以下方式使用“全部联盟”-

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,category
Union all
Select store_id,'department' as average_level_type,department as id,department;

如果要避免在这种情况下使用全部并集,可以使用诸如rollup()或通过对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)/count(distinct customer_id) as average,GROUPING__ID As G_ID
    from transactions
    group by store_id,department
    grouping sets((store_id,product),(store_id,category),department))
    ) Tab
order by 2    
;