sql >> Base de Datos >  >> RDS >> Mysql

¿Cómo selecciono una fila con conteo máximo haciendo un grupo por

Puedes probar con el group by y having cláusulas:

select t.user_name, t.thread_id , count(*) as max_count
from tbl t
group by t.user_name, t.thread_id
having count(*) = ( select count(*) as ttl
                    from tbl
                    where thread_id = t.thread_id
                    group by user_name
                    order by ttl desc
                    limit 1 )