Doublons dans les rapports

Création de rapports en SQL

Tyler Pernes

Learning & Development Consultant

Qu'est-ce qui cause les doublons ?

Création de rapports en SQL

Qu'est-ce qui cause les doublons ?

SELECT p.id, SUM(points) AS points, SUM(matches_win) AS matches_won
FROM points AS p
JOIN matches AS m ON p.id = m.id
GROUP BY p.id;
Création de rapports en SQL

Qu'est-ce qui cause les doublons ?

+-----+--------+--------------+
| id  | points | matches_won  |
|-----|--------|--------------|
| 1   | 156    | 10           |
+-----+--------+--------------+
1 donnera une valeur de points trois fois trop élevée, ici 156.
Création de rapports en SQL

Qu'est-ce qui cause les doublons ?

Table intermédiaire
+-----+------+--------------+---------+ 
| id  | year | matches_won  | points  |
|-----+------+--------------+---------+    
| 1   | 2016 | 5            | 52      |
| 1   | 2017 | 2            | 52      |  
| 1   | 2018 | 3            | 52      |
+-----+------+--------------+---------+
Création de rapports en SQL

Qu'est-ce qui cause les doublons ?

Table intermédiaire
+-----+------+--------------+---------+ 
| id  | year | matches_won  | points  |
|-----+------+--------------+---------+    
| 1   | 2016 | 5            | 52      | <--
| 1   | 2017 | 2            | 52      | <-- SUM(points) = 52 x 3 = 156
| 1   | 2018 | 3            | 52      | <-- 
+-----+------+--------------+---------+    
Création de rapports en SQL

Façons de corriger les doublons

1. Supprimer les agrégations

SELECT p.id, points, SUM(matches_won) AS matches_won
FROM points AS p
JOIN matches AS m ON p.id = m.id
GROUP BY p.id, points;
+-----+--------+--------------+
| id  | points | matches_won  |
|-----+--------+--------------|
| 1   | 52     | 10           |
+-----+--------+--------------+
Création de rapports en SQL

Façons de corriger les doublons

Création de rapports en SQL

Façons de corriger les doublons

2. Ajouter un champ à l'instruction JOIN

SELECT p.id, SUM(points) AS points, SUM(matches_win) AS matches_won
FROM points AS p
JOIN matches AS m ON p.id = m.id AND p.year = m.year
GROUP BY p.id;
Création de rapports en SQL

Façons de corriger les doublons

2. Ajouter un champ à l'instruction JOIN

SELECT p.id, SUM(points) AS points, SUM(matches_win) AS matches_won
FROM points AS p
JOIN matches AS m ON p.id = m.id AND p.year = m.year
GROUP BY p.id;
Création de rapports en SQL

Façons de corriger les doublons

SELECT id, SUM(matches_won)
FROM matches
GROUP BY id;
+-----+--------------+
| id  | matches_won  |
|-----+--------------|
| 1   | 10           |
| 2   | 7            |
+-----+--------------+
Création de rapports en SQL

Façons de corriger les doublons

3. Regrouper avec une sous-requête

SELECT p.id, points, matches_won
FROM points AS p
JOIN 
    (SELECT id, SUM(matches_won) AS matches_won
    FROM matches
    GROUP BY id) AS m 
ON p.id = m.id;
Création de rapports en SQL

Façons de corriger les doublons

  1. Supprimer les agrégations
  2. Ajouter un champ à l'instruction JOIN
  3. Regrouper avec une sous-requête
Création de rapports en SQL

Repérer les doublons

Valeur dans la table d'origine :

SELECT SUM(points) AS total_points
FROM points;
total_points = 52

Valeur dans la requête :

SELECT SUM(points) AS total_points
FROM   
    (SELECT p.id, SUM(points) AS points
    FROM points AS p
    JOIN matches AS m ON p.id = m.id
    GROUP BY p.id) AS subquery;
total_points = 156
Création de rapports en SQL

Objectif du chapitre

Création de rapports en SQL

Passons à la pratique !

Création de rapports en SQL

Preparing Video For Download...