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);
  • 집계 단계
    • 국가·장르 조합별 집계
    • 국가 단위 집계
    • 전체 집계
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)
    • 국가·장르별 대여 수
    • 국가별 대여 수
    • 전체 대여 수
  • 각 단계에서 한 수준의 상세가 제거됩니다
  • 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...