2
I have a table with the two fields Start and End of Tipo de dados Time
.
Example of records I have in the table:
- 09:30:00 10:00:00
- 10:00:00 11:00:00
- 11:00:00 12:00:00
As I show, I have full hours and half hours, as you can only have 20 minutes or 45 minutes for example.
Now I plan to make the hours difference and then add up the total hours per customer.
The problem is that when I don’t have full hours, for example I have 5 records of the same client of 30 minutes, so it’s 2:30 minutes, the query shows the result as 1:50, because it’s adding up 30 five times.
Query:
SELECT C.codigoutente,
C.nome AS Utente,
C.descricaovalencia AS Valência,
REPLACE(CAST(SUM(SEC_TO_TIME(C.Minutos))/100 AS DECIMAL (15,2)), '.', ':') AS `Horas Consumidas`,
CONVERT(CONCAT(CAST(SUM(C.Valor) AS DECIMAL (15,2)), '€'), char(8)) AS `Valor Total`
FROM
(SELECT B.codigoutente,
B.nome,
B.descricaovalencia,
B.`Data`,
B.Minutos,
IF(B.nome = 'CLASSE', (B.Minutos*10)/60, (B.Minutos*12)/60) AS Valor
FROM
(SELECT A.codigoutente,
A.nome,
A.descricaovalencia,
A.`Data`,
A.`Horas Consumidas`,
CAST(TIME_TO_SEC(A.`Horas Consumidas`)/60 AS DECIMAL(15,0)) AS Minutos
FROM
(SELECT centrodb.registoFisioterapia.id,
centrodb.utentes.codigoutente,
centrodb.utentes.nome,
centrodb.utentes.descricaovalencia,
centrodb.registoFisioterapia.`Data`,
Inicio,
Fim,
centrodb.colaboradores.Nome AS Colaborador,
TimeDiff(TIME_FORMAT(Fim,'%H:%i'), TIME_FORMAT(Inicio,'%H:%i')) AS `Horas Consumidas`
FROM centrodb.registoFisioterapia LEFT OUTER JOIN centrodb.utentes
ON centrodb.utentes.Id = centrodb.registoFisioterapia.utente
LEFT OUTER JOIN colaboradores
ON centrodb.colaboradores.codigo = centrodb.registoFisioterapia.Nome) AS A) AS B
) AS C
GROUP BY C.codigoutente, C.nome, C.descricaovalencia
The correct result then should be 2:30 minutes and not 1:50 minutes. Someone can help?
Altering:
SELECT B.codigoutente,
B.nome,
B.descricaovalencia AS Valência,
SUM(B.Minutos) AS `Horas Consumidas`,
CONVERT(CONCAT(CAST(SUM(B.Valor) AS DECIMAL (15,2)), '€'), char(8)) AS `Valor Total`
FROM
(SELECT A.codigoutente,
A.nome,
A.descricaovalencia,
A.`Data`,
A.`Minutos`,
IF(A.nome = 'CLASSE', (A.`Minutos`*10), (A.`Minutos`*12)) AS Valor
FROM
(SELECT centrodb.registoFisioterapia.id,
centrodb.utentes.codigoutente,
centrodb.utentes.nome,
centrodb.utentes.descricaovalencia,
centrodb.registoFisioterapia.`Data`,
Inicio,
Fim,
centrodb.colaboradores.Nome AS Colaborador,
CAST(TIME_TO_SEC(TiMEDiff(TIME_FORMAT(Fim,'%H:%i'), TIME_FORMAT(Inicio,'%H:%i')))/60/60 AS DECIMAL(15,2)) AS `Minutos`
FROM centrodb.registoFisioterapia LEFT OUTER JOIN centrodb.utentes
ON centrodb.utentes.Id = centrodb.registoFisioterapia.utente
LEFT OUTER JOIN colaboradores
ON centrodb.colaboradores.codigo = centrodb.registoFisioterapia.Nome) AS A) AS B
GROUP BY B.codigoutente, B.nome, B.descricaovalencia
There’s only one thing left, now instead of showing up at 1:50, it shows up at 2:50, but it should show up at 2:30, you can help?
– Bruno
What time start and end ?
– rbz
I put the change I made in the question. I have Start 9:30 and End 10:00, as I start 10:00 and End 10:30, it depends on the time
– Bruno
Yes, but then to give a total adding up more than one record, it would be another case, using the
GROUP BY
and making aSUM
in the hours field. For example, run this query, see that it is bringing value in hours correctly:SELECT (TIME_TO_SEC(TIME_FORMAT('10:00','%H:%i')) - TIME_TO_SEC(TIME_FORMAT('09:30','%H:%i')))/60/60;
– rbz
Put in http://sqlfiddle.com/, I do GROUP BY, if it is not working, because in my view, it is
– rbz
You play the script to create and insert the data on the side, click on "Build Schema"... giving ok, copy the link and send here...
– rbz
I think your problem is in SUM, because it must be adding wrong... instead of converting to hours (/60/60) per record, convert to SUM... SUM(time)/60/60, so it will sum everything in seconds, and do the split only later in total, in the final select, understood ?
– rbz
Let’s go continue this discussion in chat.
– rbz