Thursday, May 9, 2013

Implement SELECT DISTINCT in Salesforce SOQL

As far as I know, Salesforce SOQL does not support SELECT DISTINCT. In stead of implementing distinct function in your own application, here is a tip to do SELECT DISTINCT in SOQL.

Suppose we have a Customized salesforce object called Dummy__c, which have attributes Attr1__c, Attr2__c, Attr3__c etc, We want to get distinct values of Attr1__c. To achieve this, we can easily get distinct values of Attr1__c by using GROUP BY with COUNT_DISTINCT function:


SELECT Attr1__c, COUNT_DISTINCT(Attr__c) FROM Dummy__c GROUP BY Attr1__c

That's all. We do not need to load large number of records into our own application's memory and count unique values.

No comments:

Post a Comment