Sum return of select

Asked

Viewed 91 times

1

I have the following 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

That returns to me the following content:

inserir a descrição da imagem aqui

How could I add up that time returned? If I take the Group by for meses.mes the displayed result of different, I end up receiving this:

inserir a descrição da imagem aqui

1 answer

2


I don’t quite understand why it brings different results by just removing the grouping, but if you turn it into a subconsultation must receive the correct return:

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

    Perfect, thanks for the help again :)

  • =) let’s go

Browser other questions tagged

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