2
I need to order a query by the result of the sum of two sums.
ORDER BY SUM(SUM() + SUM())
I tried the following:
SELECT
SUM(campo1 + campo2) AS soma1
, SUM(campo3 + campo4) AS soma2
, SUM(soma1 + soma2) AS ordenacao
WHERE
condicoes_where
ORDER BY
ordenacao
DESC
The query is this:
SELECT
SEC_TO_TIME( SUM( TIME_TO_SEC( x.tempo_total_atendimento ) ) + SUM( TIME_TO_SEC( x.tempo_chat ) ) ) AS ordenacao
FROM (
SELECT
case when SEC_TO_TIME( SUM( TIME_TO_SEC( a.duracao ) ) + SUM( TIME_TO_SEC( t.at_duracao ) ) ) is null then
SEC_TO_TIME( SUM( TIME_TO_SEC( a.duracao ) ) )
else
SEC_TO_TIME( SUM( TIME_TO_SEC( a.duracao ) ) + SUM( TIME_TO_SEC( t.at_duracao ) ) )
end AS tempo_total_atendimento,
SEC_TO_TIME( SUM( TIME_TO_SEC( TIMEDIFF(c.con_data_fim, DATE_FORMAT(c.con_data, '%H:%i:%s')) ) ) ) AS tempo_chat
FROM atendimentos AS a
LEFT OUTER JOIN atendimentos_texto AS t ON t.atendimento = a.id
LEFT OUTER JOIN chat_conversa AS c ON c.con_file = a.chat
WHERE a.data_inicio BETWEEN '2017-01-01 00:00:00' AND '2017-09-26 23:59:59'
GROUP BY a.cliente
) x
Prints of the results
Was any of the answer helpful? Don’t forget to choose one and mark it so it can be used if someone has a similar question!
– Sorack