4D Blog

Home Product Meet the Improved Distinct Function

Meet the Improved Distinct Function

April 26, 2023

Product

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.

Discuss

Tags Class, Collections, Distinct, Entity selection, ORDA, Programming, v20

Latest related posts

  • June 16, 2025

    Generate, share and use web sessions One-Time Passcodes (OTP)

  • May 20, 2025

    Automatic Refresh of OEM Build Licenses

  • May 16, 2025

    New class to handle incoming TCP connections

Vanessa Talbot
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.
  • Deutsch
  • Français
  • English
  • Português
  • Čeština
  • Español
  • Italiano
  • 日本語

Categories

Browse categories

  • 4D View Pro
  • 4D Write Pro
  • 4D for Mobile
  • Email
  • Development Mode
  • 4D Language
  • ORDA
  • User Interface / GUI
  • Qodly Studio
  • Server
  • Maintenance
  • Deployment
  • 4D Tutorials
  • Generic
  • 4D Summit sessions and other online videos

Tags

4D-Analyzer 4D for Android 4D for iOS 4D NetKit 4D Qodly Pro 4D View Pro 4D Write Pro 20 R8 20 R9 Administration Authentication Build application CI/CD Class Client/Server Code editor Collections Compatibility settings Formula Listbox Logs Mail Microsoft 365 Network Objects ORDA PDF Pictures Preemptive Programming Qodly Studio REST Scalability Security Session Source control Speed Spreadsheet Tutorial UI User Experience v20 vscode Web Word processor

Tags

4D-Analyzer 4D for Android 4D for iOS 4D NetKit 4D Qodly Pro 4D View Pro 4D Write Pro 20 R8 20 R9 Administration Authentication Build application CI/CD Class Client/Server Code editor Collections Compatibility settings Formula Listbox Logs Mail Microsoft 365 Network Objects ORDA PDF Pictures Preemptive Programming Qodly Studio REST Scalability Security Session Source control Speed Spreadsheet Tutorial UI User Experience v20 vscode Web Word processor
Subscribe to 4D Newsletter

© 2025 4D SAS - All rights reserved
Terms & Conditions | Legal Notices | Data Policy | Cookie Policy | Contact us | Write for us


Subscribe to 4D Newsletter

* Your privacy is very important to us. Please click here to view our Policy

Contact us

Got a question, suggestion or just want to get in touch with the 4D bloggers? Drop us a line!

* Your privacy is very important to us. Please click here to view our Policy