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;

同じ出力を返すが、 groupbyロールアップ句を使用するクエリはどうなるでしょうか。単一のクエリが必要です。

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 に答える