OLAP-Operationen: GROUPING SETS

Datengestützte Entscheidungsfindung in SQL

Irene Ortner

Data Scientist at Applied Statistics

Überblick über OLAP-Operatoren in SQL

Erweiterungen in SQL, die OLAP-Operationen vereinfachen

  • GROUP BY CUBE
  • GROUP BY ROLLUP
  • GROUP BY GROUPING SETS
Datengestützte Entscheidungsfindung in SQL

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

GROUP BY GROUPING SETS

Beispiel für eine Abfrage mit dem Operator GROUPING SETS:

SELECT country, 
       genre, 
       COUNT(*)
FROM rentings_extended
GROUP BY GROUPING SETS ((country, genre), (country), (genre), ());
  • Spaltennamen in Klammern zeigen Aggregationsebene an
  • GROUP BY GROUPING SETS gibt UNION über mehrere GROUP BY-Abfragen zurück
Datengestützte Entscheidungsfindung in SQL

GROUPING SETS und GROUP-BY-Abfragen

SELECT country, 
       genre, 
       COUNT(*)
FROM renting_extended
GROUP BY GROUPING SETS (country, genre);
  • Filmausleihen für jede einzelne Kombination aus Land und Genre zählen
  • Ausdruck in GROUPING SETS: (country, genre)
SELECT country, 
       genre, 
       COUNT(*)
FROM renting_extended
GROUP BY country, genre;

| country | genre | count | |---------|--------|--------| | Austria | Comedy | 2 | | Belgium | Drama | 15 | | Austria | Drama | 4 | | Belgium | Comedy | 1 |
Datengestützte Entscheidungsfindung in SQL

GROUPING SETS und GROUP-BY-Abfragen

SELECT country, COUNT(*)
FROM renting_extended
GROUP BY GROUPING SETS (country);
  • Filmausleihen für jedes Land zählen
  • Ausdruck in GROUPING SETS: (country)
SELECT country, COUNT(*)
FROM renting_extended
GROUP BY country;
| country | count |
|---------|-------|
| Austria | 16    |
| Belgium | 6     |
Datengestützte Entscheidungsfindung in SQL

GROUPING SETS und GROUP-BY-Abfragen

SELECT genre, COUNT(*)
FROM renting_extended
GROUP BY GROUPING SETS (genre);
  • Filmausleihen für jedes Genre zählen
  • Ausdruck in GROUPING SETS: (genre)
SELECT genre, COUNT(*)
FROM renting_extended
GROUP BY genre;
| country | count |
|---------|-------|
| Comedy  | 3     |
| Drama   | 19    |
Datengestützte Entscheidungsfindung in SQL

GROUPING SETS und GROUP-BY-Abfragen

SELECT COUNT(*)
FROM renting_extended
GROUP BY GROUPING SETS ();
  • Gesamtaggregation – alle Filmausleihen zählen
  • Ausdruck in GROUPING SETS: ()
SELECT COUNT(*)
FROM renting_extended;
| count |
|-------|
| 22    |
Datengestützte Entscheidungsfindung in SQL

Notation für GROUP BY GROUPING SETS

  • GROUP BY GROUPING SETS (...)
    SELECT country, genre, COUNT(*)
    FROM renting_extended
    GROUP BY GROUPING SETS ((country, genre), (country), (genre), ());
    
  • UNION über 4 vorherige Abfragen
  • Alle Infos einer Pivot-Tabelle in einer Abfrage kombinieren
  • Abfrage identisch mit GROUP BY CUBE (country, genre)
Datengestützte Entscheidungsfindung in SQL

Ergebnis mit GROUPING SETS

SELECT country, genre, COUNT(*)
FROM renting_extended
GROUP BY GROUPING SETS ((country, genre), (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   |
| NULL    | Comedy | 3   |
| NULL    | Drama  | 19  |
Datengestützte Entscheidungsfindung in SQL

Ausleihen und Bewertung berechnen

  • Nur ausgewählte Aggregationen kombinieren:
    • Land und Genre
    • Genre
  • Anzahl der ausgeliehenen Filme und Durchschnittsbewertungen für Aggregation verwenden
    SELECT country, 
         genre, 
         COUNT(*), 
         AVG(rating) AS avg_rating
    FROM renting_extended
    GROUP BY GROUPING SETS ((country, genre), (genre));
    
Datengestützte Entscheidungsfindung in SQL

Ausleihen und Bewertung berechnen

SELECT country, genre, COUNT(*), AVG(rating) AS avg_rating
FROM renting_extended
GROUP BY GROUPING SETS ((country, genre), (genre));
| country | genre  | count| avg_rating |
|---------|--------|------|------------|
| Austria | Comedy | 2    | 8.00       |
| Belgium | Drama  | 15   | 9.17       |
| Austria | Drama  | 4    | 6.00       |
| Belgium | Comedy | 1    | NULL       |
| NULL    | Comedy | 3    | 8.00       |
| NULL    | Drama  | 19   | 8.38       |
Datengestützte Entscheidungsfindung in SQL

Lass uns üben!

Datengestützte Entscheidungsfindung in SQL

Preparing Video For Download...