1

Possuo a seguinte query:

SELECT
(case when day(sysdate()) < 10 and meses.mes = month(sysdate()) then 0 else
CAST(SUM(CASE WHEN s2.name LIKE '%Servidores' THEN ( coalesce(ta.time_unit,0) * 1.5)/ 60 ELSE coalesce(ta.time_unit,0)/60 END) AS DECIMAL(12, 3)) end) AS TEMPO_CHAMADO,
meses.customer_id as empresa
FROM
(Select distinct
  m.mes,
  t2.customer_id
From (SELECT d1+d2*10 AS mes FROM 
(SELECT 0 AS d1 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 
UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 ) t1, 
(SELECT 0 AS d2 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 
UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 ) t2 
LIMIT 12 OFFSET 1 ) AS m
Cross Join otrs.ticket t2
WHERE t2.customer_id = 'Dial') as meses
LEFT JOIN otrs.ticket t on t.customer_id = meses.customer_id
                   and t.type_id = 11
LEFT JOIN otrs.time_accounting ta on ta.ticket_id = t.id 
                             and MONTH(ta.change_time) = meses.mes
                             and ta.change_time BETWEEN '2018-04-01' AND '2018-06-30'
LEFT JOIN otrs.service AS s2 ON t.service_id = s2.id
WHERE meses.mes BETWEEN MONTH('2018-04-01') AND MONTH('2018-06-30')
GROUP BY  meses.mes, meses.customer_id    
ORDER BY meses.mes

Que me retorna o seguinte conteúdo:

inserir a descrição da imagem aqui

Como poderia somar esse tempo retornado? Se eu tiro o Group by por meses.mes o resultado exibido da diferente, acabo recebendo isso:

inserir a descrição da imagem aqui

1 Resposta 1

2

Não entendi bem porque traz resultados diferentes removendo apenas o agrupamento, mas se você transformar isso em uma subconsulta deve receber o retorno correto:

SELECT customer_id, SUM(TEMPO_CHAMADO) FROM
    (SELECT
    (case when day(sysdate()) < 10 and meses.mes = month(sysdate()) then 0 else
    CAST(SUM(CASE WHEN s2.name LIKE '%Servidores' THEN ( coalesce(ta.time_unit,0) * 1.5)/ 60 ELSE coalesce(ta.time_unit,0)/60 END) AS DECIMAL(12, 3)) end) AS TEMPO_CHAMADO,
    meses.customer_id
    FROM
    (Select distinct
      m.mes,
      t2.customer_id
    From (SELECT d1+d2*10 AS mes FROM 
    (SELECT 0 AS d1 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 
    UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 ) t1, 
    (SELECT 0 AS d2 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 
    UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 ) t2 
    LIMIT 12 OFFSET 1 ) AS m
    Cross Join otrs.ticket t2
    WHERE t2.customer_id = 'Dial') as meses
    LEFT JOIN otrs.ticket t on t.customer_id = meses.customer_id
                       and t.type_id = 11
    LEFT JOIN otrs.time_accounting ta on ta.ticket_id = t.id 
                                 and MONTH(ta.change_time) = meses.mes
                                 and ta.change_time BETWEEN '2018-04-01' AND '2018-06-30'
    LEFT JOIN otrs.service AS s2 ON t.service_id = s2.id
    WHERE meses.mes BETWEEN MONTH('2018-04-01') AND MONTH('2018-06-30')
    GROUP BY  meses.mes, meses.customer_id) as tabela_tmp
GROUP BY customer_id
2
  • 1
    Perfeito, obrigado pela ajuda novamente :)
    – R.Santos
    Commented 6/06/2018 às 14:07
  • =) vamo que vamo
    – rLinhares
    Commented 6/06/2018 às 14:09

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .