ROLLUP

การตัดสินใจเชิงข้อมูลด้วย SQL

Bart Baesens

Professor Data Science and Analytics

ตาราง renting_extended

แถวแรกของตาราง 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      |
| .......... | ........ | ...... | ...... |
การตัดสินใจเชิงข้อมูลด้วย SQL

คิวรีด้วย ROLLUP

SELECT country, 
       genre, 
       COUNT(*)
FROM renting_extended
GROUP BY ROLLUP (country, genre);
  • ระดับของการรวมข้อมูล
    • รวมข้อมูลตามแต่ละคู่ของ country และ genre
    • รวมข้อมูลตาม country เพียงอย่างเดียว
    • รวมข้อมูลทั้งหมด
การตัดสินใจเชิงข้อมูลด้วย SQL

คิวรีด้วย 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     |
การตัดสินใจเชิงข้อมูลด้วย SQL

ลำดับใน 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    |
การตัดสินใจเชิงข้อมูลด้วย SQL

สรุป ROLLUP

  • คืนค่าการรวมข้อมูลตามลำดับชั้น เช่น ROLLUP (country, genre)
    • จำนวนการเช่าหนังแต่ละ country และแต่ละ genre
    • จำนวนการเช่าหนังแต่ละ country
    • จำนวนการเช่าหนังทั้งหมด
  • แต่ละขั้นจะลดระดับรายละเอียดลงหนึ่งระดับ
  • ลำดับของชื่อคอลัมน์มีผลต่อ ROLLUP
การตัดสินใจเชิงข้อมูลด้วย SQL

จำนวนการเช่าและการให้คะแนน

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         |
การตัดสินใจเชิงข้อมูลด้วย SQL

มาฝึกกันเถอะ!

การตัดสินใจเชิงข้อมูลด้วย SQL

Preparing Video For Download...