Duplicazione nei report

Reportistica in SQL

Tyler Pernes

Learning & Development Consultant

Cosa causa la duplicazione?

Reportistica in SQL

Cosa causa la duplicazione?

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;
Reportistica in SQL

Cosa causa la duplicazione?

+-----+--------+--------------+
| id  | points | matches_won  |
|-----|--------|--------------|
| 1   | 156    | 10           |
+-----+--------+--------------+
1 produrrà un valore di points triplo rispetto al dovuto, in questo caso 156.
Reportistica in SQL

Cosa causa la duplicazione?

Tabella intermedia
+-----+------+--------------+---------+ 
| id  | year | matches_won  | points  |
|-----|------|--------------|---------|    
| 1   | 2016 | 5            | 52      |
| 1   | 2017 | 2            | 52      |  
| 1   | 2018 | 3            | 52      |
+-----+------+--------------+---------+
Reportistica in SQL

Cosa causa la duplicazione?

Tabella intermedia
+-----+------+--------------+---------+ 
| id  | year | matches_won  | points  |
|-----|------|--------------|---------|    
| 1   | 2016 | 5            | 52      | <--
| 1   | 2017 | 2            | 52      | <-- SUM(points) = 52 x 3 = 156
| 1   | 2018 | 3            | 52      | <-- 
+-----+------+--------------+---------+    
Reportistica in SQL

Modi per correggere la duplicazione

1. Rimuovi le aggregazioni

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           |
+-----+--------+--------------+
Reportistica in SQL

Modi per correggere la duplicazione

Reportistica in SQL

Modi per correggere la duplicazione

2. Aggiungi un campo alla clausola 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;
Reportistica in SQL

Modi per correggere la duplicazione

2. Aggiungi un campo alla clausola 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;
Reportistica in SQL

Modi per correggere la duplicazione

SELECT id, SUM(matches_won)
FROM matches
GROUP BY id;
+-----+--------------+
| id  | matches_won  |
|-----|--------------|
| 1   | 10           |
| 2   | 7            |
+-----+--------------+
Reportistica in SQL

Modi per correggere la duplicazione

3. Riepiloga con una sottoquery

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;
Reportistica in SQL

Modi per correggere la duplicazione

  1. Rimuovi le aggregazioni
  2. Aggiungi un campo alla clausola JOIN
  3. Riepiloga con una sottoquery
Reportistica in SQL

Individuare la duplicazione

Valore nella tabella originale:

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

Valore nella query:

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
Reportistica in SQL

Obiettivo del capitolo

Reportistica in SQL

Esercitiamoci!

Reportistica in SQL

Preparing Video For Download...