Sort by the result of the sum of two SUM - MYSQL

Asked

Viewed 587 times

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

Resultado dos antes da pergunda

Resultado da resposta do @Sorack

  • 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!

2 answers

1


To do summation ordering you do not need to create Queries no. Queries like this, they work well:

select ID, SUM(QUANTIDADE_A), SUM(QUANTIDADE_B), SUM(QUANTIDADE_A + QUANTIDADE_B) AS TOTAL
from pedidos_itens
GROUP BY ID
ORDER BY TOTAL DESC

There’s a syntax in your SQL that I wouldn’t do at all.

SEC_TO_TIME( SUM( TIME_TO_SEC( a.duracao ) ) + SUM( TIME_TO_SEC( t.at_duracao ) ) )

Maybe that’s why you get weird results. I’d trade it for something more semantic:

 SEC_TO_TIME( SUM(TIME_TO_SEC(a.duracao) + TIME_TO_SEC(t.at_duracao)) )

In addition to ensuring expected results, your code is cleaner and easier to understand.

1

You can reach the desired end using a suquery and referencing the position of the column in the ORDER BY:

SELECT x.atendimento_duracao + x.texto_duracao + chat_duracao AS total_duracao
  FROM (SELECT SUM(COALESCE(TIME_TO_SEC(a.duracao), 0)) atendimento_duracao,
               SUM(COALESCE(TIME_TO_SEC(at.at_duracao), 0)) texto_duracao,
               SUM(COALESCE(TIME_TO_SEC(TIMEDIFF(c.con_data_fim, DATE_FORMAT(c.con_data, '%H:%i:%s'))), 0)) chat_duracao
          FROM atendimentos a
               LEFT JOIN atendimentos_texto at ON at.atendimento = a.id
               LEFT JOIN chat_conversa cc ON cc.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
 ORDER BY 1

In this query each of the durations is being added up, and if you are NULL in the table, will be considered as zero, so the total sum will not be affected. After this will be ordered by the column of position 1 (Considering the ORDER BY 1).

  • thanks for the answer (and so fast) almost worked, for you to understand my problem, I need to bring the sum of time is in three tables (Tabela1 + table2) and Tabela3, I need to sum everything and sort by the sum of the time that are attendance in a Helpdesk.

  • @Alexandrevieira you better provide the name of the columns and contents in your question, so I can assemble a query aimed at your specification

  • I edited the question see if it helps.

  • @Alexandrevieira altered. Please check.

  • The result is not yet as expected, I attached two prints with the results before and after editing, note that what I need is the sum of the tempo_total_attendance and tempo_chat. Thank you.

  • @Alexandrevieira this your column is TIME even?

  • no, actually two are char and 1 is team

  • @Alexandrevieira then, you have to see why you can’t understand if your format is HH:mm:ss. Outside that apparently you don’t just want to sort, you want to show some extra data

  • Yeah, this bank I picked up like this, I’m just doing a report and I need to sort by that sum, but I’ll leave it at that, I’ll go to the sort by php even, maybe for sure. Thanks.

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.