将 ratio_to_report 与 3 个或更多分组一起使用 Oracle SQL

Using ratio_to_report with 3 or more groupings Oracle SQL

我正在尝试设置一个查询,以根据商店数量、客户群和品牌分组汇总总销售额百分比。理想情况下,我希望得到如下结果:

 Store --------Segment-------Brand------Sales--------pct_total
    1              A            X         50            66.6
    1              A            Y         25            33.3
    1              B            X         25            25.0
    1              B            Y         25            25.0
    1              B            Z         50            50.0

这是我目前从 table 获得的代码,其中包含执行此操作所需的数据。

 select Store,Segment,Brand, to_char(100 * ratio_to_report(total_sales) over (partition by store, segment, brand),'990.00L','NLS_CURRENCY=%') as pct_total
 from ( 
         select 
           Store,
           Segment,
           Brand,
           sum(sales) as total_sales
           from customer_data 
           group by grouping sets ((Store),(store,Segment,brand)))

由于分组,它只是给我一个错误

不需要分组集,这将return预期结果:

select Store,Segment,Brand, total_sales,
   to_char(100 * ratio_to_report(total_sales) over (partition by store, segment),'990.00L','NLS_CURRENCY=%') as pct_total
from ( 
        select 
          Store,
          Segment,
          Brand,
          sum(sales) as total_sales
          from customer_data 
          group by store,Segment,brand)