ROLLUP

Datengestützte Entscheidungsfindung in SQL

Bart Baesens

Professor Data Science and Analytics

Tabelle renting_extended

Die ersten Zeilen der Tabelle 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      |
| .......... | ........ | ...... | ...... |
Datengestützte Entscheidungsfindung in SQL

Abfrage mit ROLLUP

SELECT country, 
       genre, 
       COUNT(*)
FROM renting_extended
GROUP BY ROLLUP (country, genre);
  • Aggregationsebenen
    • Aggregation jeder Kombination aus Land und Genre
    • Aggregation eines einzelnen Landes
    • Gesamtaggregation
Datengestützte Entscheidungsfindung in SQL

Abfrage mit 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     |
Datengestützte Entscheidungsfindung in SQL

Reihenfolge in 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    |
Datengestützte Entscheidungsfindung in SQL

Zusammenfassung ROLLUP

  • Gibt Aggregationen für Werte-Hierarchie zurück, z. B. ROLLUP (country, genre)
    • Filmausleihen für jedes Land und jedes Genre
    • Filmausleihen für jedes Land
    • Gesamtzahl der ausgeliehenen Filme
  • Bei jedem Schritt wird eine Detailebene weggelassen
  • Reihenfolge der Spaltennamen ist für ROLLUP wichtig
Datengestützte Entscheidungsfindung in SQL

Anzahl der Ausleihen und Bewertungen

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         |
Datengestützte Entscheidungsfindung in SQL

Lass uns üben!

Datengestützte Entscheidungsfindung in SQL

Preparing Video For Download...