How to average a mysql query result?

Asked

Viewed 5,107 times

0

How do I get the "Count(t2.id_atend) TOTAL_ATENDIMENTO field " and calculate his PHP media?

SELECT t3.desc_serv, 
    t3.nm_serv,
    t1.nm_usu,
    count(t2.id_atend) TOTAL_ATENDIMENTO
FROM usuarios t1
INNER JOIN historico_atendimentos t2 ON t1.id_usu = t2.id_usu
INNER JOIN servicos t3 ON t2.id_serv = t3.id_serv
WHERE t3.id_serv = 9 AND t2.dt_fim LIKE '%2013-10%'
GROUP BY t3.id_serv, t1.id_usu

result print:

inserir a descrição da imagem aqui

  • No sql you put count(t2.id_atend) as total, then you just take the total like you take the other elements. To know the number of records just count the number of results obtained by the query you did.

  • I don’t understand my dear.

  • You know how to fetch the values of T3.nm_serv and t1.nm_usu, right?

  • $res = Count($results); ?

  • See this question I think it fits and you have the two ways to do it in the answers.

1 answer

2

select avg(TOTAL_ATENDIMENTO) MEDIA_TOTAL_ATENDIMENTO
from
(
SELECT t3.desc_serv, 
    t3.nm_serv,
    t1.nm_usu,
    count(t2.id_atend) TOTAL_ATENDIMENTO
FROM usuarios t1
INNER JOIN historico_atendimentos t2 ON t1.id_usu = t2.id_usu
INNER JOIN servicos t3 ON t2.id_serv = t3.id_serv
WHERE t3.id_serv = 9 AND t2.dt_fim LIKE '%2013-10%'
GROUP BY t3.id_serv, t1.id_usu
) virtual

Browser other questions tagged

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