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

Distinct in soql

I would like to know is there any ways to get distinct records in soql
I want to extract the product family using a query I get all the records having family but I want only distinct records how do I do that ?
Thanks in advance
Sorry no distinct in SOQL today..
Suchitra -

Adam is, of course, right - SOQL is not SQL, and it does not support the key word DISTINCT.

But that doesn't mean you can't achieve the same result.  SOQL naturally uses the concept of relationships, such as a lookup relationship, between objects.  You might be able to design your custom objects to store the possible values for a product family in one table, and have all the records in another custom object have a lookup relation to a single value.

In other words, instead of having data that looks like this
Product Name   Product Family
Product One          Product Family 1
Product Two          Product Family 1
Product Three       Product Family 1
Product Four          Product Family 2
Product Five          Product Family 2

You would have one custom object called Product Name, with the names, and another called Product Family, with two records for Product Family 1 and Product Family 2.  You would then have a SOQL query that selected on the distinct product family value and also retrieved the related product names.

Of course, creating separate objects like this has implications in the design and implementation of other parts of your application, which may be helpful or hurtful.

Finally, you can always retrieve all the records into an SObject and then remove duplicates through code.

Hope this helps.

- Rick Greenwald Evangelist

Thanks a lot Rick I will try this out.:smileyhappy:

Thanks & Regards

P Suchitra



Hi Rick/Adam,
The distinct values that I wanted to get is a picklist so used the describeSObject to retrieve the values.