function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

What is the use of COUNT_DISTINCT function ?



  Unable to understand the use COUNT_DISTINCT function.Can anybody explain with an example

Laxman RaoLaxman Rao

first i will say the example :


Lead l1 = new Lead(Name = 'l1', Company = 'Salesforce');

Lead l2 = new Lead(Name = 'l1', Company = 'IBM');

Lead l3 = new Lead(Name = 'l1', Company = 'Microsoft');

Lead l4 = new Lead(Name = 'l1', Company = 'Salesforce');


Consider you have these records in salesforce lead object, now i want count of distinct company names.

I will write the query as 




This will lead output as 3


So COUNT_DISTINCT(field) returns the number of distinct non-null field values matching the query criteria.


this might be helpful