OLAP: CUBE operator

Ra quyết định dựa trên dữ liệu với 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
Ra quyết định dựa trên dữ liệu với 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      |
| .......... | ....... | .....  | ...... |
Ra quyết định dựa trên dữ liệu với SQL

Pivot table - number of movie rentals

 

 

Ra quyết định dựa trên dữ liệu với SQL

Pivot table and SQL output

Ra quyết định dựa trên dữ liệu với 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    |
Ra quyết định dựa trên dữ liệu với 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     |
Ra quyết định dựa trên dữ liệu với SQL

Now it's your turn to GROUP BY CUBE!

Ra quyết định dựa trên dữ liệu với SQL

Preparing Video For Download...