Different result between select, after applying SUM() and Grop By

Asked

Viewed 354 times

0

I’m making a call report from elastix and I have a problem here in the outcome. Theoretically, if I don’t touch Where, the result would have to be the same.

select distinct
(case left( devices.id ,2)
when '10' then 'SAC'
when '40' then 'SAC'
when '41' then 'SAC'
when '80' then 'OUTROS'
when '70' then 'OUTROS'
when '90' then 'RECEPCAO'
when '91' then 'ADMINISTRATIVO'
when '92' then 'COMERCIAL'
when '93' then 'T.I.'
when '95' then 'SAC'
when '98' then 'PROJETOS'
when '99' then 'DIRETORIA DE T.I.'
else ' ' end) AS  grupo ,
round(sum(cdr.duration)/60)   AS  "duracao

from ((cdr  join  devices  on(( devices.dial  = substring_index(cdr.channel ,'-',1))))
join  trunks  on((substring_index(cdr.lastdata ,'/',2) = substring_index( trunks.channelid ,'/',2))))

where outcid = '12121212' and
!(left(cdr.dst,4) = 0800) and
(char_length(cdr.dst) > 4) and
cdr.duration > 60 and calldate >= '2014-02-01' and calldate <=  '2014-02-20'
group by grupo with rollup;

inserir a descrição da imagem aqui

  • what is the question?

  • You did not change Where but you changed the whole result set by using an Aggregate, in this case the SUM. Yes the result should be different. In addition to edit her question she was not very clear. From what I understand you use a select to return the grid and another, with the sum of durations per department. But it is not clear what its goal is. PS:. in print its aggregate total is twice as much as shown in the detailed.

  • PS2: You don’t need the distinct, the group by will already return only one Row per group.

1 answer

3


When you define group by grupo You ask mysql to display your grouped results, that is, there will be only one return for each group type.

And the SUM() sum the "duration" values of each of the items that make up each group.

Instead of using the distinct, review the use you are making of the joins, in order to avoid duplicated items.

Other than that, I couldn’t identify exactly what your question would be.

  • Thank you, William for trying to help. I have a Select and it brings me the result of the first grid and when I apply sum() and group by, the result is another as the print. Can you help me? Thank you.

  • the two selects are exactly the same except for the group by and the sum()? Have you ever tried a test without using these Juoin’s? A simplified query would help us understand your case since we don’t know the structure of your bank...

  • anyway I suggest you update your question, to make it clearer what you need, thus avoiding your question to be closed.

  • Hello William. I remade the two select’s and now, is giving a difference of 4 min. I wonder how I do to solve this. I will post both select’s. Thank you.

  • Follow Where from the report: FROM cdr a, Devices b Where b.dial = substring_index(a.Channel ,'-',1) and ! (left(a.dst,4) = 0800) and (char_length(a.dst) > 7) and (char_length(a.clid) > 7) and a. clid = '22164000' and a. Duration > 60 and !(b.id = '9501') and a. calldate between '2014-02-01' and '2014-02-21' ;

  • Where do Minutes totaliser by sector: FROM cdr a, Devices b Where b.dial = substring_index(a.Channel ,'-',1) and ! (left(a.dst,4) = 0800) and (char_length(a.dst) > 7) and (char_length(a.clid) > 7) and a. clid = operator and a. Duration > 60 and !(b.id = '9501') and a. calldate between datainitial and datafinal group by group;

  • I did not put the full Select due to the limited number of characters. Thanks.

  • Thank you. It’s working. I used letter to name the tables (FROM cdr a, Devices b Where) and removed distinct.

  • Dude, give an update on your own question, so the querys are visible to everyone, and if you have found the solution, give an UPDATE mentioning this.

  • Guilherme, I put in the previous post that was working. Thanks to all.

Show 5 more comments

Browser other questions tagged

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