ROLLUP

Pengambilan Keputusan Berbasis Data dengan SQL

Bart Baesens

Professor Data Science and Analytics

Tabel renting_extended

Beberapa baris awal tabel renting_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      |
| .......... | ........ | ...... | ...... |
Pengambilan Keputusan Berbasis Data dengan SQL

Kueri dengan ROLLUP

SELECT country, 
       genre, 
       COUNT(*)
FROM renting_extended
GROUP BY ROLLUP (country, genre);
  • Tingkat agregasi
    • Agregasi tiap kombinasi country dan genre
    • Agregasi per country saja
    • Agregasi total
Pengambilan Keputusan Berbasis Data dengan SQL

Kueri dengan ROLLUP

SELECT country, 
       genre, 
       COUNT(*)
FROM renting_extended
GROUP BY ROLLUP (country, genre);
| country | genre  | count |
|---------|--------|-------|
| null    | null   | 22    |
| Austria | Comedy | 2     |
| Belgium | Drama  | 15    |
| Austria | Drama  | 4     |
| Belgium | Comedy | 1     |
| Belgium | null   | 16    |
| Austria | null   | 6     |
Pengambilan Keputusan Berbasis Data dengan SQL

Urutan dalam ROLLUP

SELECT country, 
       genre, 
       COUNT(*)
FROM renting_extended
GROUP BY ROLLUP (genre, country);
| country | genre  | count |
|---------|--------|-------|
| null    | null   | 22    |
| Austria | Comedy | 2     |
| Belgium | Drama  | 15    |
| Austria | Drama  | 4     |
| Belgium | Comedy | 1     |
| null    | Comedy | 3     |
| null    | Drama  | 19    |
Pengambilan Keputusan Berbasis Data dengan SQL

Ringkasan ROLLUP

  • Mengembalikan agregat untuk hierarki nilai, mis. ROLLUP (country, genre)
    • Sewa film per negara dan per genre
    • Sewa film per negara
    • Total sewa film
  • Tiap langkah menghapus satu tingkat detail
  • Urutan nama kolom penting untuk ROLLUP
Pengambilan Keputusan Berbasis Data dengan SQL

Jumlah sewa dan rating

SELECT country, 
       genre, 
       COUNT(*) AS n_rentals,
       COUNT(rating) AS n_ratings
FROM renting_extended
GROUP BY ROLLUP (genre, country);
| country  | genre  | n_rentals | n_ratings |
|----------|--------|-----------|-----------|
| null     | null   | 22        | 9         |
| Belgium  | Drama  | 15        | 6         |
| Austria  | Comedy | 2         | 1         |
| Belgium  | Comedy | 1         | 0         |
| Austria  | Drama  | 4         | 2         |
| null     | Comedy | 3         | 1         |
| null     | Drama  | 19        | 8         |
Pengambilan Keputusan Berbasis Data dengan SQL

Ayo berlatih!

Pengambilan Keputusan Berbasis Data dengan SQL

Preparing Video For Download...