2

I am trying to get the account which has the highest number of opportunities.

Tried below and getting all the accountids and it's related opportunity count. But how do I only get the account record with highest opportunity number count. enter image description here

2 Answers 2

1

Use ORDER BY to get this result:

SELECT COUNT(Id), AccountId 
FROM Opportunity 
GROUP BY AccountId 
ORDER BY COUNT(Id) DESC 
LIMIT 1
1
  • Thanks. Is there a way I can do a query on Account to get the id which has maximum opportunities. Please let me know.
    – joshallen
    Commented Aug 9 at 15:15
0

You need to use ORDER BY and LIMIT in your query to get an account that has the highest number of opportunities.

SELECT count(Id), AccountId FROM Opportunity GROUP BY AccountId ORDER BY AccountId LIMIT 1

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .