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:
[ {"value":"Family","count":2}, {"value":"Friend","count":1}, {"value":"Personal","count":1}, {"value":"Work","count":1} ]
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:
[ {"value":"France","count":2}, {"value":"Morocco","count":10}, {"value":"Germany","count":3} ]
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.
Example
For example, I want to retrieve all possible paths of the “address” object field:
var $paths : Collection
$paths:=ds.Contact.all().distinctPaths("address")
The result is:
[ "Number", "StreetAddress", "ExtendedAddress", "City", "ZipCode", "Country" ]
Next…
Feel free to share your feedback and ideas on the 4D forum to help us continue improving the product.