OLAP işlemleri: GROUPING SETS

SQL ile Veri Odaklı Karar Verme

Irene Ortner

Data Scientist at Applied Statistics

SQL’de OLAP operatörlerine genel bakış

OLAP işlemlerini kolaylaştıran SQL uzantıları

  • GROUP BY CUBE
  • GROUP BY ROLLUP
  • GROUP BY GROUPING SETS
SQL ile Veri Odaklı Karar Verme

Tablo renting_extended

renting_extended tablosunun ilk satırları:

| 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      |
| .......... | ........ | ...... | ...... |
SQL ile Veri Odaklı Karar Verme

GROUP BY GROUPING SETS

GROUPING SETS operatörüyle bir sorgu örneği:

SELECT country, 
       genre, 
       COUNT(*)
FROM rentings_extended
GROUP BY GROUPING SETS ((country, genre), (country), (genre), ());
  • Parantez içindeki sütun adları bir toplama düzeyini temsil eder.
  • GROUP BY GROUPING SETS, birden çok GROUP BY sorgusunun UNION hâlini döndürür.
SQL ile Veri Odaklı Karar Verme

GROUPING SETS ve GROUP BY sorguları

SELECT country, 
       genre, 
       COUNT(*)
FROM renting_extended
GROUP BY GROUPING SETS (country, genre);
  • Her benzersiz ülke–tür kombinasyonu için kiralama sayısını hesaplar.
  • GROUPING SETS ifadesi: (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      |
SQL ile Veri Odaklı Karar Verme

GROUPING SETS ve GROUP BY sorguları

SELECT country, COUNT(*)
FROM renting_extended
GROUP BY GROUPING SETS (country);
  • Her ülke için film kiralama sayısını hesaplar.
  • GROUPING SETS ifadesi: (country)
SELECT country, COUNT(*)
FROM renting_extended
GROUP BY country;
| country | count |
|---------|-------|
| Austria | 16    |
| Belgium | 6     |
SQL ile Veri Odaklı Karar Verme

GROUPING SETS ve GROUP BY sorguları

SELECT genre, COUNT(*)
FROM renting_extended
GROUP BY GROUPING SETS (genre);
  • Her tür için film kiralama sayısını hesaplar.
  • GROUPING SETS ifadesi: (genre)
SELECT genre, COUNT(*)
FROM renting_extended
GROUP BY genre;
| country | count |
|---------|-------|
| Comedy  | 3     |
| Drama   | 19    |
SQL ile Veri Odaklı Karar Verme

GROUPING SETS ve GROUP BY sorguları

SELECT COUNT(*)
FROM renting_extended
GROUP BY GROUPING SETS ();
  • Toplam toplama: tüm kiralamaları sayar.
  • GROUPING SETS ifadesi: ()
SELECT COUNT(*)
FROM renting_extended;
| count |
|-------|
| 22    |
SQL ile Veri Odaklı Karar Verme

GROUP BY GROUPING SETS için gösterim

  • GROUP BY GROUPING SETS (...)
    SELECT country, genre, COUNT(*)
    FROM renting_extended
    GROUP BY GROUPING SETS ((country, genre), (country), (genre), ());
    
  • Önceki 4 sorgu üzerinde UNION.
  • Pivot tablodaki tüm bilgileri tek sorguda birleştirir.
  • Bu sorgu GROUP BY CUBE (country, genre) ile aynıdır.
SQL ile Veri Odaklı Karar Verme

GROUPING SETS operatörü ile sonuç

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  |
SQL ile Veri Odaklı Karar Verme

Kiralama sayısı ve ortalama puanı hesaplayın

  • Sadece seçili toplamaları birleştirin:
    • ülke ve tür
    • tür
  • Toplama için kiralama sayısı ve ortalama puanı kullanın.
    SELECT country, 
         genre, 
         COUNT(*), 
         AVG(rating) AS avg_rating
    FROM renting_extended
    GROUP BY GROUPING SETS ((country, genre), (genre));
    
SQL ile Veri Odaklı Karar Verme

Kiralama sayısı ve ortalama puanı hesaplayın

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       |
SQL ile Veri Odaklı Karar Verme

Ayo berlatih!

SQL ile Veri Odaklı Karar Verme

Preparing Video For Download...