[SalesForce] Soql to count How many each pick list values for picklist field in Object

I have a picklist field 3 values Like Red,Blue,White. There are 50 records in the object with different picklist values. Now I need a query – How many records with Picklist value – Red and AND how many records with Picklist value – Blue and same for the third value – White?

It should show like Picklist – Red-15 ,Picklist – Blue-10, Picklist – White-25. If it is not Possible then need a query to show all records picklist value wise?

Best Answer

This is a type of "aggregate result" query, and it looks like this:

SELECT SomePicklist__c value, COUNT(Id) recordCount
FROM SomeObject__c
GROUP BY SomePicklist__c

The output would look like this:

value, recordCount
Red, 15
Blue, 10
White, 25

You can summarize up to 2,000 values this way, which is more than enough for a single picklist.

You can read more about it in GROUP BY | SOQL and SOSL Reference.