+ Start a Discussion

Does SOQL have a function that works like SQL's DISTINCT

I'm looking to pull a list of values that are used in a particular field in our sf.com data.  Is there any function or command that will pull only one of each value?


Also, Is there any books out there on soql?  The closest thing I have is the force.com cookbook.





Salesforce.com SOQL does not provide similar to distinct as SQL.


You can get detail abut SOQL in the Force.com API guide.





Thanks and Regards,

Dinesh Nasipuri



I'm not sure if this will help in your instance, but I used the following code to generate a list of distinct countries from Account records to populate a drop-down list.  You might be able to pick apart the code and get it to do what you need:


Map<String,SelectOption> countryMap = new Map<String,SelectOption>(); List<SelectOption> countryList = new List<SelectOption>(); countryList.add(new SelectOption('','')); for(Account a : [select BillingCountry from Account where BillingCountry <> null and BillingCountry <> '']) { countryMap.put(a.BillingCountry, new SelectOption(a.BillingCountry, a.BillingCountry)); } List<String> keys = new List<String>(countryMap.keySet()); keys.sort(); for(String key : keys) { countryList.add(countryMap.get(key)); } return countryList;


Using the "put" method on the map ensures that there are no duplicate keys. My code also sorts the results to make the list user friendly.
I'll try the code... thanks!

Hi folks,


I heard about some brand new features comming, maybe in next month, like the addition of "GROUP BY" and "HAVING" clauses, "AVG" and many others functions, I could saw something related to "DISTINCT" clausses to,


These guys are pushing forward, so keep up2date  


With Winter 2010 there is definitely Group By. See below, for some samples of Group by.