2

我有一个查询:

select country_region, 
       country_subregion, 
       country_name, 
       calendar_year, 
       calendar_quarter_number, 
       sum(amount_sold) as amount
  from countries co join
       customers cu on co.country_id = cu.country_id join
       sales sa on cu.cust_id = sa.cust_id join
       times ti on sa.time_id = ti.time_id
 where (   co.country_region = 'Americas' 
        or co.country_region = 'Middle East'
       ) 
   and ti.calendar_year between 2000 and 2001
group by grouping sets 
(
    (country_region, country_subregion, country_name, calendar_year, calendar_quarter_number),
    (country_region, country_subregion, country_name, calendar_year),
    (country_region, country_subregion, country_name),
    (country_region, country_subregion, calendar_year, calendar_quarter_number),
    (country_region, country_subregion, calendar_year),
    (country_region, country_subregion),
    (country_region, calendar_year, calendar_quarter_number),
    (country_region, calendar_year),
    (country_region),
    (calendar_year, calendar_quarter_number),
    (calendar_year),
    ()
)
order by amount desc;

什么是返回相同输出但使用group by rollup子句的查询。我想要一个查询。

4

1 回答 1

7

使用该ROLLUP子句的等效查询是:

select country_region
     , country_subregion
     , country_name
     , calendar_year
     , calendar_quarter_number
     , sum(amount_sold) as amount
  from countries co
       join customers cu on co.country_id = cu.country_id
       join sales sa on cu.cust_id = sa.cust_id
       join times ti on sa.time_id = ti.time_id
 where (  co.country_region='Americas'
       or co.country_region='Middle East'
       )
   and ti.calendar_year between 2000 and 2001
 group by rollup (country_region, country_subregion, country_name)
     , rollup (calendar_year, calendar_quarter_number)
 order by amount desc

这是证明:

 group by rollup (country_region, country_subregion, country_name)
     , rollup (calendar_year, calendar_quarter_number)

等于

 group by grouping sets
       ( (country_region, country_subregion, country_name)
       , (country_region, country_subregion)
       , (country_region)
       , ()
       )
     , grouping sets
       ( (calendar_year, calendar_quarter_number)
       , (calendar_year)
       , ()
       )

这等于

 group by grouping sets
       ( (country_region, country_subregion, country_name, calendar_year, calendar_quarter_number)
       , (country_region, country_subregion, country_name, calendar_year)
       , (country_region, country_subregion, country_name)
       , (country_region, country_subregion, calendar_year, calendar_quarter_number)
       , (country_region, country_subregion, calendar_year)
       , (country_region, country_subregion)
       , (country_region, calendar_year, calendar_quarter_number)
       , (country_region, calendar_year)
       , (country_region)
       , (calendar_year, calendar_quarter_number)
       , (calendar_year)
       , ()
       )

这等于您的原始查询。

您可以在我去年写的这篇文章中找到有关按扩展分组的更多信息:http ://www.rwijk.nl/AboutOracle/All_About_Grouping.pdf

问候,罗布。

于 2010-12-31T09:14:22.923 回答