GROUP BY C.category_id, C.name처럼 GROUP BY 절에 두 개 이상의 컬럼을 지정하면 지정한 모든 컬럼의 조합을 기준으로 데이터가 그룹화 됨.GROUP BY는 데이터를 특정 기준으로 그룹화하고 해당 그룹별로 집계(Ex. COUNT, SUM 등)를 수행.Ex
| category_id | name | language_id | film_id | title |
|---|---|---|---|---|
| 1 | Action | 1 | 101 | Fast & Furious |
| 1 | Action | 2 | 102 | Velozes e Furiosos |
| 1 | Action | 1 | 106 | John Wick |
| 2 | Comedy | 1 | 103 | The Mask |
| 2 | Comedy | 1 | 104 | Dumb and Dumber |
| 2 | Comedy | 2 | 107 | O Auto da Compadecida |
| 3 | Drama | 1 | 105 | Titanic |
SELECT category_id, name, language_id, COUNT(*) AS film_count
FROM film_category
GROUP BY category_id, name, language_id
ORDER BY film_count DESC;
| category_id | name | language_id | film_count |
|---|---|---|---|
| 1 | Action | 1 | 2 |
| 1 | Action | 2 | 1 |
| 2 | Comedy | 1 | 2 |
| 2 | Comedy | 2 | 1 |
| 3 | Drama | 1 | 1 |
Ex
| category_id | name | film_id |
|---|---|---|
| 1 | Action | 101 |
| 1 | Adventure | 102 |
GROUP BY C.category_id만 사용할 경우 Action과 Adventure가 같은 그룹으로 묶이는 오류가 발생할 수 있음.GROUP BY에 2개 이상의 칼럼을 지정.Ex
SELECT year,
month,
SUM(volume) AS volume_sum
FROM tutorial.aapl_historical_stock_price
GROUP BY year, month
ORDER BY year, month
