php - Wrong use of GROUP BY
i'm having a real problem with a query. The database entity are:
Paziente (Codice_Fiscale, Nome_Paziente, Cognome_Paziente, Data_Nascita, Luogo_Nascita)
Appuntamento (Codice_Appuntamento, Data_Appuntamento, Ora_Appuntamento, Codice_Fiscale)
Intervento (Codice_Intervento, Tipo_Intervento, Sala_Intervento, Data_Intervento, Ora_Intervento, Codice_Fiscale_Paziente, ID_Dentista)
Dentista (ID_Dentista, Nome_Dentista, Cognome_Dentista)
I need to do a query that shows a list of appointment grouped by type of intervention. It should be something like:
Tipo_Intervento COUNT(*)
Chirurgia dentale 1
Cura carie 2
Estrazione dentale 1
Pulizia dentale 3
I can do this correctly when i use this query:
SELECT intervento.Tipo_Intervento, COUNT(*)
FROM intervento
GROUP BY intervento.Tipo_Intervento
But i need to link the database "intervento" to "appuntamento" ('cause it's an exercise) and when i use this:
SELECT COUNT(DISTINCT appuntamento.Codice_Appuntamento) AS Numero_Appuntamenti, intervento.Tipo_Intervento AS Tipologia_intervento
FROM appuntamento, intervento
WHERE appuntamento.Codice_Fiscale = intervento.Codice_Fiscale_Paziente
GROUP BY intervento.Tipo_Intervento
I obtain the wrong value:
Numero_Appuntamenti Tipologia_intervento
2 Chirurgia dentale
4 Cura carie
2 Estrazione dentale
3 Pulizia dentale
The problem is in the GROUP BY intervento.Tipo_Intervento, that increase the number of appointments, in fact if i use GROUP BY appuntamento.Codice_Appuntamento i obtain:
Numero_Appuntamenti Tipologia_intervento
1 Pulizia dentale
1 Estrazione dentale
1 Pulizia dentale
1 Cura carie
1 Cura carie
1 Pulizia dentale
1 Estrazione dentale
That is right, but i need it to be grouped.
Does someone can help me? (forgive my bad english, as you can see it's not my language)
EDIT: This is the table "appuntamento":
Codice_Appuntamento Data_Appuntamento Ora_Appuntamento Codice_Fiscale
001 2020-05-05 16:00:00.000000 RSSMRA80A01H501U
002 2020-07-03 16:30:00.000000 VRDGPP85E02F205P
003 2020-06-05 16:00:00.000000 RSSMRA80A01H501U
004 2020-08-14 17:15:00.000000 BNCGNN83D03L219B
005 2020-08-27 17:45:00.000000 BNCGNN83D03L219B
006 2020-07-05 16:00:00.000000 RSSMRA80A01H501U
007 2020-09-13 17:20:00.000000 VRDGPP85E02F205P
And this the table "intervento"
Codice_Intervento Tipo_Intervento Sala_Intervento Data_Intervento Ora_Intervento Codice_Fiscale_Paziente ID_Dentista
A_001 Pulizia dentale 1 2020-05-05 16:00:00.000000 RSSMRA80A01H501U D_001
A_002 Estrazione dentale 2 2020-07-03 16:30:00.000000 VRDGPP85E02F205P D_003
A_003 Pulizia dentale 1 2020-06-05 16:00:00.000000 RSSMRA80A01H501U D_001
A_004 Cura carie 3 2020-08-14 17:15:00.000000 BNCGNN83D03L219B D_001
A_005 Chirurgia dentale 5 2020-08-27 17:45:00.000000 BNCGNN83D03L219B D_002
A_006 Pulizia dentale 1 2020-07-05 16:00:00.000000 RSSMRA80A01H501U D_001
A_007 Cura carie 3 2020-09-13 17:20:00.000000 VRDGPP85E02F205P D_002
"Codice fiscale" and "ID_Dentista" are the foreing key, thery are the primary key of "paziente" and "dentista"
Answer
Solution:
COUNT(DISTINCT field)
returns how many unique [appuntamento].[Codice_Appuntamento] are found for each [intervento].[Tipo_Intervento]. If I read your question right, you just want to remove the DISTINCT
.
That should work if you use a LEFT JOIN
. Your way of putting together tables rule out any [appuntamento] without a [intervento]
SELECT COUNT(appuntamento.Codice_Appuntamento) AS Numero_Appuntamenti, intervento.Tipo_Intervento AS Tipologia_intervento
FROM appuntamento
LEFT JOIN intervento ON appuntamento.Codice_Fiscale = intervento.Codice_Fiscale_Paziente
GROUP BY intervento.Tipo_Intervento
Answer
Solution:
This will work on mysql 5.6, not on higher versions :
https://www.db-fiddle.com/f/sDXoDdMWsPVkTheoaWkx1i/0
SELECT i.Tipo_Intervento,tmp.Numero_Appuntamenti,a.*
FROM intervento i
LEFT JOIN appuntamento a ON a.Codice_Fiscale = i.Codice_Fiscale_Paziente
LEFT JOIN
(SELECT COUNT(*) AS Numero_Appuntamenti,Tipo_Intervento
from intervento
GROUP BY Tipo_Intervento) AS tmp ON i.Tipo_Intervento=tmp.Tipo_Intervento
GROUP BY i.Tipo_Intervento
Answer
Solution:
You can use this query:
SELECT max(i.Tipo_Intervento), count(a. Codice_Appuntamento)
FROM intervento as i left join appuntamento as a on i.Codice_Fiscale_Paziente = a.Codice_Fiscale
GROUP BY i.Sala_Intervento
Source