Meet the Improved Distinct Function

Retrieving the list of unique data elements is always helpful when manipulating data. In some cases, retrieving the number of occurrences of each value is essential. The distinct function of the Collection and Entity selection classes allows retrieving a list of unique values. With 4D v20, you can also retrieve the number of occurrences of each distinct value.

And that’s not all!

An object in the database can be used to save custom data entered by the user. So on the code side, you need to know all the possible paths to the object. The distinctPaths function has been added to the Entity selection class.

Here is everything you need to know.

New option for distinct

The syntax is identical for both classes, Collection and Entity selection. You must pass the ck count values, or dk count values constant, to the second parameter named option to get a collection of attribute pairs: value and count.

If you don’t pass the option, the behavior remains the same; you get a collection of unique values.

example with Collection

For example, I want the list of unique values of a category collection and the number of occurrences of each category:

var $col; $category : Collection
$category:=New Collection("Family";"Friend";"Work";"Family";"Personal")
$col:=$category.distinct(ck count values)

The result is:


Example with Entity Selection

For example, I want to get the list of countries of my contacts and the number of contacts per country:

var $countries : Collection
$countries:=ds.Contact.all().distinct("country";dk count values)

The result is: 


New distinct paths function

The distinctPaths function of the Entity selection class returns the list of distinct paths found in your indexed object field. You must pass the name of the indexed object field as a parameter. This function is equivalent to the DISTINCT ATTRIBUTE PATHS command. 


For example, I want to retrieve all possible paths of the “address” object field:

var $paths : Collection

The result is: 



Feel free to share your feedback and ideas on the 4D forum to help us continue improving the product.

Vanessa Talbot
• Product Owner •Vanessa Talbot joined 4D Program team in June, 2014. As a Product Owner, she is in charge of writing the user stories then translating it to functional specifications. Her role is also to make sure that the feature implementation delivered is meeting the customer need.Since her arrival, she has worked to define key features in 4D. She has worked on most of preemptive multi-threading new features and also on a very complex subject: the new architecture for engined application. Vanessa has a degree from Telecom Saint-Etienne. She began her career at the Criminal Research Institute as a developer for the audiovisual department. She has also worked in media and medical fields as expert in technical support, production as well as documenting new features.