MySQL workbench max of a count -
as sql goofball, flummoxed trying 2 column count max.
i have database of transactions , want highest frequency recipient each user.
data is:
user recipient b c b f d d c d d
desired result is
user recipient count b 2 d 3
i manage count can't seem max recipient out.
i've tried using inner join, , statement, failed work.
in case of ties, doesn't matter value shown.
getting maximum in mysql bit of pain. here 1 method:
select user, recipient, count(*) t group user, recipient having count(*) = (select count(*) t t2 t2.user = t.user group user, recipient order count(*) desc limit 1 );
note: version return duplicates when there ties. if don't want duplicates, can instead do:
select user, recipient, count(*) t group user, recipient having recipient = (select recipient t t2 t2.user = t.user group user, recipient order count(*) desc limit 1 );
Comments
Post a Comment