+ Start a Discussion
JeepCreepJeepCreep 

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.

 

Thanks!

 

NasipuriNasipuri

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

 

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

 

http://www.salesforce.com/us/developer/docs/api/index.htm

 

 

Thanks and Regards,

Dinesh Nasipuri

dinesh.nasipuri@gmail.com

mcrosbymcrosby

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.
JeepCreepJeepCreep
I'll try the code... thanks!
magdielhfmagdielhf

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  

ajaslkfjqpr89q0ajaslkfjqpr89q0

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

 

 

http://www.ext-it.com/2010/blog/soql-group-by-grouping-records-in-salesforce-com.html