Regroupement des données

SQL Intermédiaire

Jasmin Ludolf

Data Science Content Developer, DataCamp

Regroupement des données

Illustration montrant un entonnoir plein avec différentes formes réparties en trois catégories : carrés, cercles et triangles

SQL Intermédiaire

Regrouper par champs individuels avec GROUP

SELECT certification, COUNT(title) AS title_count
FROM films
GROUP BY certification;
|certification|title_count|
|-------------|-----------|
|Unrated      |62         |
|M            |5          |
|G            |112        |
|NC-17        |7          |
...
SQL Intermédiaire

Gestion des erreurs

SELECT certification, title
FROM films
GROUP BY certification;
column "films.title" must appear in the
GROUP BY clause or be used in an 
aggregate function
LINE 1: SELECT certification, title
                              ^
SELECT 
   certification, 
   COUNT(title) AS count_title
FROM films
GROUP BY certification;
|certification|count_title|
|-------------|-----------|
|Unrated      |62         |
|M            |5          |
|G            |112        |
...
SQL Intermédiaire

Regrouper sur plusieurs champs avec GROUP

SELECT certification, language, COUNT(title) AS title_count
FROM films
GROUP BY certification, language;
|certification|language |title_count|
|-------------|---------|-----------|
|null         |null     |5          |
|Unrated      |Japanese |2          |
|R            |Norwegian|2          |
...
SQL Intermédiaire

GROUP avec BY

SELECT 
       certification, 
       COUNT(title) AS title_count
FROM films
GROUP BY certification;
|certification|title_count|
|-------------|-----------|
|Unrated      |62         |
|M            |5          |
|G            |112        |
...
SELECT 
       certification, 
       COUNT(title) AS title_count
FROM films
GROUP BY certification
ORDER BY title_count DESC;
|certification|title_count|
|-------------|-----------|
|R            |2118       |
|PG-13        |1462       |
...
SQL Intermédiaire

Ordre d'exécution

-- Written code:
SELECT 
       certification, 
       COUNT(title) AS title_count
FROM films
GROUP BY certification
ORDER BY title_count DESC
LIMIT 3;
-- Order of execution:

SELECT certification, COUNT(title) AS title_count
FROM films
GROUP BY certification
ORDER BY title_count DESC
LIMIT 3;
SQL Intermédiaire

Passons à la pratique !

SQL Intermédiaire

Preparing Video For Download...