Combiner des tables

Création de rapports en SQL

Tyler Pernes

Learning & Development Consultant

Rapport d'objectifs

Médailles d'or par groupe démographique
(Pays d'Europe de l'Ouest seulement)
+----------+--------------------+-------+
| season   |  demographic_group | golds |
|----------+--------------------+-------|
| Winter   | Male Age 26+       | 13    |
| Winter   | Female Age 26+     | 8     |
| Summer   | Male Age 13-25     | 7     |
| Summer   | Female Age 13-25   | 6     |
| Winter   | Male Age 13-25     | 4     |
| Summer   | Male Age 26+       | 4     |
| Winter   | Female Age 13-25   | 4     |
| Summer   | Female Age 26+     | 2     |
+----------+--------------------+-------+
Création de rapports en SQL

Tables pertinentes

Création de rapports en SQL

Tables pertinentes

Création de rapports en SQL

Option A : JOIN d'abord, puis UNION

Création de rapports en SQL

Option A : JOIN d'abord, puis UNION

Étape 1 : Configurer la requête du haut avec JOIN

SELECT 
    athlete_id, 
    gender, 
    age, 
    gold  
FROM summer_games AS sg
JOIN athletes AS a 
ON sg.athlete_id = a.id;
Requête exécutée avec succès !
Création de rapports en SQL

Option A : JOIN d'abord, puis UNION

Étape 2 : Configurer la requête du bas et UNION des deux

SELECT 
    athlete_id, 
    gender, 
    age, 
    gold  
FROM summer_games AS sg
JOIN athletes AS a 
ON sg.athlete_id = a.id
UNION ALL
SELECT 
    athlete_id, 
    gender, 
    age, 
    gold  
FROM winter_games AS wg
JOIN athletes AS a 
ON wg.athlete_id = a.id;
Création de rapports en SQL

Option B : UNION d'abord, puis JOIN

Création de rapports en SQL

Option B : UNION d'abord, puis JOIN

Étape 1 : Créer le UNION initial

SELECT 
    athlete_id, 
    gold  
FROM summer_games AS sg
UNION
SELECT 
    athlete_id, 
    gold  
FROM winter_games AS wg;
Création de rapports en SQL

Option B : UNION d'abord, puis JOIN

Étape 2 : Transformer en sous‑requête + JOIN

SELECT 
    athlete_id, 
    gender, 
    age, 
    gold  
FROM
    (SELECT 
         athlete_id, 
         gold  
    FROM summer_games AS sg
    UNION ALL
    SELECT athlete_id, gold  
    FROM winter_games AS wg) AS g
JOIN athletes AS a 
ON g.athlete_id = a.id;
Création de rapports en SQL

Comparaison

Option A

SELECT 
    athlete_id, 
    gender, 
    age, 
    gold  
FROM summer_games AS sg
JOIN athletes AS a 
ON sg.athlete_id = a.id
UNION ALL
SELECT 
    athlete_id, 
    gender, 
    age, 
    gold  
FROM winter_games AS wg
JOIN athletes AS a 
ON wg.athlete_id = a.id;

Option B

SELECT 
    athlete_id, 
    gender, 
    age, 
    gold  
FROM
    (SELECT 
         athlete_id, 
         gold  
    FROM summer_games AS sg
    UNION ALL
    SELECT athlete_id, gold  
    FROM winter_games AS wg) AS g
JOIN athletes AS a 
ON g.athlete_id = a.id;
Création de rapports en SQL

Points clés

  • Plusieurs façons d'obtenir le même rapport
  • Étapes graduelles = dépannage plus facile
Création de rapports en SQL

À vos requêtes !

Création de rapports en SQL

Preparing Video For Download...