OLAP: CUBE operator

Data-Driven Decision Making in SQL

Irene Ortner

Data Scientist at Applied Statistics

Introduction to OLAP

  • OLAP: on-line analytical processing
  • Aggregate data for a better overview
    • Count number of rentings for each customer.
    • Average rating of movies for each genre and each country.
  • Produce pivot tables to present aggregation results
Data-Driven Decision Making in SQL

Table 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      |
| .......... | ....... | .....  | ...... |
Data-Driven Decision Making in SQL

Pivot table - number of movie rentals

 

 

Data-Driven Decision Making in SQL

Pivot table and SQL output

Data-Driven Decision Making in 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    |
Data-Driven Decision Making in SQL

Number of ratings

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     |
Data-Driven Decision Making in SQL

Now it's your turn to GROUP BY CUBE!

Data-Driven Decision Making in SQL

Preparing Video For Download...