I have a working time table, which has the data_inicio
, data_fim
, usuario_id
and extra
that indicates whether it is overtime or not.
I already have a query that brings the sum in seconds worked per year, month, user of the last 6 months.
But now I also need to bring the sum of second regular and overtime work (% with% = 1).
SELECT
us.nome as usuario,
MONTH(tb.data_inicio) AS mes,
YEAR(tb.data_inicio) AS ano,
SUM(TIMESTAMPDIFF(SECOND, tb.data_inicio, tb.data_fim)) AS total_segundos,
COUNT(DISTINCT DAY(tb.data_inicio)) AS total_dias
FROM 'horario_ponto' tb
INNER JOIN 'usuario' us ON us.id = tb.usuario_id
WHERE tb.data_inicio > (LAST_DAY(NOW() - INTERVAL 6 MONTH) + INTERVAL 1 DAY)
GROUP BY usuario, ano, mes
ORDER BY ano DESC, mes DESC, usuario ASC
I have tried in many ways already, there seems to be no solution without the query having to be rewritten inside a subquery.
Is there any way to:
extra
receive some parameter that discriminates SUM(TIMESTAMPDIFF(SECOND, tb.data_inicio, tb.data_fim)) AS total_segundos_regulares,
and in another:
tb.extra = 0
receive SUM(TIMESTAMPDIFF(SECOND, tb.data_inicio, tb.data_fim)) AS total_segundos_extra
?