电影分组

用 SQL 进行数据驱动决策

Bart Baesens

Professor Data Science and Analytics

GROUP BY 的应用

  • 按国家或性别的客户偏好。

  • 按类型或上映年份的电影热度。

  • 按类型的电影平均价格。

用 SQL 进行数据驱动决策

表:movies_selected

用 SQL 进行数据驱动决策

GROUP BY

SELECT genre
FROM movies_selected
GROUP BY genre;
| genre       |
|-------------|
| Drama       |
| Fantasy     |
| Sci-Fiction |
| Animation   |
| Romance     |
用 SQL 进行数据驱动决策

平均租价

SELECT genre, 
       AVG(renting_price) AS avg_price
FROM movies_selected
GROUP BY genre;
| genre       | avg_price   |
|-------------|-------------|
| Drama       | 2.865       |
| Fantasy     | 2.69        |
| Sci-Fiction | 2.87        |
| Animation   | 2.923333333 |
| Romance     | 2.99        |
用 SQL 进行数据驱动决策

movies_selected 表

用 SQL 进行数据驱动决策

movies_selected 表拆分

用 SQL 进行数据驱动决策

平均租价与影片数

SELECT genre, 
       AVG(renting_price) AS avg_price, 
       COUNT(*) AS number_movies
FROM movies_selected
GROUP BY genre
| genre       | avg_price     | number_movies |
|-------------|---------------|---------------|
| Drama       | 2.865         | 4             |
| Fantasy     | 2.69          | 3             |
| Sci-Fiction | 2.87          | 2             |
| Animation   | 2.923333333   | 3             |
| Romance     | 2.99          | 1             |
用 SQL 进行数据驱动决策

HAVING 子句

SELECT genre, 
       AVG(renting_price) avg_price, 
       COUNT(*) number_movies
FROM movies
GROUP BY genre
HAVING COUNT(*) > 2;
| genre     | avg_price     | number_movies |
|-----------|---------------|---------------|
| Drama     | 2.865         | 4             |
| Fantasy   | 2.69          | 3             |
| Animation | 2.923333333   | 3             |
用 SQL 进行数据驱动决策

Passons à la pratique !

用 SQL 进行数据驱动决策

Preparing Video For Download...