sarovarc
sarovarc

Reputation: 407

Max from Sum of Count

So I have a table which contains tickets for users.

What I need to do is find out which user has the maximum number of tickets in the system.

Theoretically I need to group the table by user_id, then find sum of rows for each user, then select max from there.

I have not a clue how to approach this. Bonus if someone can show how to write in Laravel Eloquent.

Upvotes: 0

Views: 655

Answers (1)

Abhik Chakraborty
Abhik Chakraborty

Reputation: 44844

The easiest way would be

select 
user_id,
count(*) as total
from table_name
group by user_id
order by total desc limit 1

Upvotes: 1

Related Questions