Select selecting the attendant below the average of calls

2

Good morning,

These are trying to make a select to select only the attendants who have the number of calls below the average.

It seems simple, but I'm not getting it, I've tried it in several ways ...

I would not have to count the number of calls in the WHERE to put something like "Count (*) & media;"

Thank you.

$ average = '40';

SELECT a.adminID
    from os
    JOIN cadastroCliente cc ON cc.cadastroClienteID = os.idcliente
    JOIN administrador a ON a.adminID = os.idusuario
    WHERE os.idusuario = a.adminID
    AND os.status = '1'
    AND os.respondido = '0'
ORDER BY RAND()
LIMIT 1
    
asked by anonymous 21.09.2015 / 15:43

1 answer

3

You will have to use having for this, here is an example:

SELECT count(a.adminID), a.adminID
    from os
    JOIN cadastroCliente cc ON cc.cadastroClienteID = os.idcliente
    JOIN administrador a ON a.adminID = os.idusuario
    WHERE os.idusuario = a.adminID
    AND os.status = '1'
    AND os.respondido = '0'
ORDER BY RAND()
LIMIT 1

group by adminID
having count(a.adminID) < 40
    
21.09.2015 / 15:46