OLAP:CUBE 演算子

SQLで進めるデータドリブンな意思決定

Irene Ortner

Data Scientist at Applied Statistics

OLAP 入門

  • OLAP:オンライ ン分析処理
  • 集計で全体像を把握
    • 顧客ごとのレンタル件数をカウント
    • 国・ジャンルごとの映画の平均評価
  • 集計結果をピボットテーブルで提示
SQLで進めるデータドリブンな意思決定

rentings_extended テーブル

| renting_id | country | genre  | rating |
|------------|---------|--------|--------|
| 2          | Belgium | Drama  | 10     |
| 32         | Belgium | Drama  | 10     |
| 203        | Austria | Drama  | 6      |
| 292        | Austria | Comedy | 8      |
| 363        | Belgium | Drama  | 7      |
| .......... | ....... | .....  | ...... |
SQLで進めるデータドリブンな意思決定

ピボットテーブル:映画のレンタル件数

 

 

SQLで進めるデータドリブンな意思決定

ピボットテーブルとSQLの出力

SQLで進めるデータドリブンな意思決定

GROUP BY CUBE

SELECT country, 
       genre, 
       COUNT(*)
FROM renting_extended
GROUP BY CUBE (country, genre);
| country  | genre  | count |
|----------|--------|-------|
| Austria  | Comedy | 2     |
| Belgium  | Drama  | 15    |
| Austria  | Drama  | 4     |
| Belgium  | Comedy | 1     |
| Belgium  | null   | 16    |
| Austria  | null   | 6     |
| null     | Comedy | 3     |
| null     | Drama  | 19    |
| null     | null   | 22    |
SQLで進めるデータドリブンな意思決定

評価数の集計

SELECT country, 
       genre, 
       COUNT(rating)
FROM renting_extended
GROUP BY CUBE (country, genre);
| country  | genre  | count |
|----------|--------|-------|
| Austria  | Comedy | 1     |
| Belgium  | Drama  | 6     |
| Austria  | Drama  | 2     |
| Belgium  | Comedy | 0     |
| Belgium  | null   | 6     |
| Austria  | null   | 3     |
| null     | Comedy | 1     |
| null     | Drama  | 8     |
| null     | null   | 9     |
SQLで進めるデータドリブンな意思決定

GROUP BY CUBE を実行してみましょう

SQLで進めるデータドリブンな意思決定

Preparing Video For Download...