Эквивалентный запрос, использующий предложение 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
С уважением, Роб.