title: GroupBy with condition on aggregate Spark/Scala
body: <p>I have a dataframe like this :</p>
  
  <p></p>
  
  <pre><code>|   ID_VISITE_CALCULE|       TAG_TS_TO_TS|EXTERNAL_PERSON_ID|EXTERNAL_ORGANISATION_ID| RK|
  +--------------------+-------------------+------------------+------------------------+---+
  |GA1.2.1023040287....|2019-04-23 11:24:19|            dupont|                    null|  1|
  |GA1.2.1023040287....|2019-04-23 11:24:19|            durand|                    null|  2|
  |GA1.2.105243141.1...|2019-04-23 11:21:01|              null|                    null|  1|
  |GA1.2.1061963529....|2019-04-23 11:12:19|              null|                    null|  1|
  |GA1.2.1065635192....|2019-04-23 11:07:14|            antoni|                    null|  1|
  |GA1.2.1074357108....|2019-04-23 11:11:34|              lang|                    null|  1|
  |GA1.2.1074357108....|2019-04-23 11:12:37|              lang|                    null|  2|
  |GA1.2.1075803022....|2019-04-23 11:28:38|            cavail|                    null|  1|
  |GA1.2.1080137035....|2019-04-23 11:20:00|              null|                    null|  1|
  |GA1.2.1081805479....|2019-04-23 11:10:49|              null|                    null|  1|
  |GA1.2.1081805479....|2019-04-23 11:10:49|            linare|                    null|  2|
  |GA1.2.1111218536....|2019-04-23 11:28:43|              null|                    null|  1|
  |GA1.2.1111218536....|2019-04-23 11:32:26|              null|                    null|  2|
  |GA1.2.1111570355....|2019-04-23 11:07:00|              null|                    null|  1|
  +--------------------+-------------------+------------------+------------------------+---+
  </code></pre>
  
  <p>I'm trying to apply rules to aggregate by ID_VISITE_CALCULE and keep only one row for an ID.</p>
  
  <p>For an ID (a group), I wish:</p>
  
  <ul>
  <li><p>get the first timestamp of the group and store it in a START column</p></li>
  <li><p>get the last timestamp of the group and store it in an END column</p></li>
  <li><p>test if EXTERNAL_PERSON_ID is the same for the whole group. 
  If this is the case and it is NULL then I write NULL, if it is and it is a name then I write the name. Finally if there are different values in the group then I register UNDEFINED</p></li>
  <li>apply exactly the same rules to the column EXTERNAL_ORGANIZATION_ID</li>
  </ul>
  
  <pre><code>RESULT :
  +--------------------+------------------+------------------------+-------------------+-------------------+
  |   ID_VISITE_CALCULE|EXTERNAL_PERSON_ID|EXTERNAL_ORGANISATION_ID|              START|                END|
  +--------------------+------------------+------------------------+-------------------+-------------------+
  |GA1.2.1023040287....|         undefined|                    null|2019-04-23 11:24:19|2019-04-23 11:24:19|
  |GA1.2.105243141.1...|              null|                    null|2019-04-23 11:21:01|2019-04-23 11:21:01|
  |GA1.2.1061963529....|              null|                    null|2019-04-23 11:12:19|2019-04-23 11:12:19|
  |GA1.2.1065635192....|            antoni|                    null|2019-04-23 11:07:14|2019-04-23 11:07:14|
  |GA1.2.1074357108....|              lang|                    null|2019-04-23 11:11:34|2019-04-23 11:12:37|
  |GA1.2.1075803022....|            cavail|                    null|2019-04-23 11:28:38|2019-04-23 11:28:38|
  |GA1.2.1080137035....|              null|                    null|2019-04-23 11:20:00|2019-04-23 11:20:00|
  |GA1.2.1081805479....|         undefined|                    null|2019-04-23 11:10:49|2019-04-23 11:10:49|
  |GA1.2.1111218536....|              null|                    null|2019-04-23 11:28:43|2019-04-23 11:32:26|
  |GA1.2.1111570355....|              null|                    null|2019-04-23 11:07:00|2019-04-23 11:07:00|
  +--------------------+------------------+------------------------+-------------------+-------------------+
  </code></pre>
  
  <p>In my example, I only have 2 lines for a group at most, but in the real dataset I can have several hundred lines in a group.</p>
  
  <p>Thank you for your kind assistance.</p>