Answers.Body: <p>Depending what you're trying to do you might be able to just use group by, like if you want a list of all the distinct last names in your database:</p>
  <pre><code>\Civi\Api4\Contact::get()
    -&gt;addSelect('last_name')
    -&gt;addGroupBy('last_name')
    -&gt;execute();
  </code></pre>
  <p>If you want to get fancy you can create a searchkit, where you set it up with distinct, and then run it via code to get the results. See <a href="https://github.com/civicrm/civicrm-core/blob/91ac1dc25c9e24422c6bc9feabca9e5c16b4856d/ext/search_kit/tests/phpunit/api/v4/SearchDisplay/SearchRunTest.php" rel="nofollow noreferrer">https://github.com/civicrm/civicrm-core/blob/91ac1dc25c9e24422c6bc9feabca9e5c16b4856d/ext/search_kit/tests/phpunit/api/v4/SearchDisplay/SearchRunTest.php</a> for some fancy examples, but at its simplest you could create the searchkit via the UI, then just run it with something like below. See api4 explorer for more params.</p>
  <pre><code>$result = SearchDisplay::Run()
        -&gt;setSavedSearch('the_saved_search_name')
        -&gt;setDisplay('the_search_display_name')
        -&gt;execute();
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-09-20T14:57:10.657Z
Answers.Text: Thanks for your help.
Answers.UserDisplayName: Learn CiviCRM
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-09-18T00:57:08.020Z
Answers.LastActivityDate: 2023-09-18T00:57:08.020Z
Answers.UserDisplayName: Demerit
Answers.Body: <p>APIv4 supports distinct when used with aggregation, but doesn't add the keyword DISTINCT to select queries.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-09-17T03:20:25.033Z
Answers.LastActivityDate: 2023-09-17T03:20:25.033Z
Answers.UserDisplayName: Coleman
Body: <p>Is it possible to get distinct using apiv4?
  For example $result = civicrm_api4('Contact', 'get'  &lt;- getdistinct?
  Thanks</p>
  
Tags: <apiv4>
Title: Can we get distinct by using API4?
ThreadText: # Can we get distinct by using API4?
  
  (Asked by: username_0 on 2023-09-16)
  
  Is it possible to get distinct using apiv4?
  For example $result = username_0\_api4('Contact', 'get' <- getdistinct?
  Thanks
  
  **Tags:** apiv4
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2023-09-18. Score: 1
  
  Depending what you're trying to do you might be able to just use group by, like if you want a list of all the distinct last names in your database:
  
  ```
  \Civi\Api4\Contact::get()
    ->addSelect('last_name')
    ->addGroupBy('last_name')
    ->execute();
  
  ```
  
  If you want to get fancy you can create a searchkit, where you set it up with distinct, and then run it via code to get the results. See <https://github.com/username_0/username_0-core/blob/91ac1dc25c9e24422c6bc9feabca9e5c16b4856d/ext/search_kit/tests/phpunit/api/v4/SearchDisplay/SearchRunTest.php> for some fancy examples, but at its simplest you could create the searchkit via the UI, then just run it with something like below. See api4 explorer for more params.
  
  ```
  $result = SearchDisplay::Run()
        ->setSavedSearch('the_saved_search_name')
        ->setDisplay('the_search_display_name')
        ->execute();
  
  ```
  
  > **Comments:**
  > **username_0**: Thanks for your help.  
  
  ---
  
  ### Answer by username_2 on 2023-09-17. Score: 0
  
  APIv4 supports distinct when used with aggregation, but doesn't add the keyword DISTINCT to select queries.
  
  
  ---
  
  