2010-12-30 6 views
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; 

同じ出力を返しますが、ロールアップ句によってグループを使用するクエリであるもの。 私は単一のクエリをしたいです。

答えて

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) 
     ,() 
     ) 

に等しいです。

これは元のクエリと同じです。

あなたは、私が昨年書いたこの記事で拡張することにより、グループの詳細情報を見つけることができます:http://www.rwijk.nl/AboutOracle/All_About_Grouping.pdf

よろしく、 ロブ。

+0

ありがとう、 キューブのグループ化を使用するクエリの同等のユニオンを作ることは可能でしょうか? – ZdenekSmetana

+2

いいえ、余分なHAVING句がないとできません。グループ化セットを計算して後で削除することは非効率です。 –

+0

あなたのpdfのグループ化の概要はとても役に立ちました。ロールアップ/キューブの前にグループ化セットを説明することは明らかにはっきりしています。利用可能にしてくれてありがとう。 – alan

関連する問題