title: CountDistinct based on a condition in spark scala
body: <p>I have the below dataframe with me .</p>
  <pre><code> +-------+---+----+
  |Company|EMP|Flag|
  +-------+---+----+
  |      M| c1|   Y|
  |      M| c1|   Y|
  |      M| c2|   N|
  |      M| c2|   N|
  |      M| c3|   Y|
  |      M| c3|   Y|
  |      M| c4|   N|
  |      M| c4|   N|
  |      M| c5|   Y|
  |      M| c5|   Y|
  |      M| c6|   Y|
  +-------+---+----+
  </code></pre>
  <p>Created by -</p>
  <pre><code>val df1=Seq(
  (&quot;M&quot;,&quot;c1&quot;,&quot;Y&quot;),
  (&quot;M&quot;,&quot;c1&quot;,&quot;Y&quot;),
  (&quot;M&quot;,&quot;c2&quot;,&quot;N&quot;),
  (&quot;M&quot;,&quot;c2&quot;,&quot;N&quot;),
  (&quot;M&quot;,&quot;c3&quot;,&quot;Y&quot;),
  (&quot;M&quot;,&quot;c3&quot;,&quot;Y&quot;),
  (&quot;M&quot;,&quot;c4&quot;,&quot;N&quot;),
  (&quot;M&quot;,&quot;c4&quot;,&quot;N&quot;),
  (&quot;M&quot;,&quot;c5&quot;,&quot;Y&quot;),
  (&quot;M&quot;,&quot;c5&quot;,&quot;Y&quot;),
  (&quot;M&quot;,&quot;c6&quot;,&quot;Y&quot;)
  )toDF(&quot;Company&quot;,&quot;EMP&quot;,&quot;Flag&quot;)
  </code></pre>
  <p>How can I take the distinct count EMP when FLAG =Y and FLAG = N. Once, an EMP has got a flag, it wont change again.I can achieve this with distinct. But is there any way to achive this without distinct(this is to avoid an extra join in the code)</p>
  <p>Expected Output :</p>
  <pre><code>+---+---+---+---------+----------+
  |  M|  Y|  N|Total_ROWs|Unique_Emp|
  +---+---+---+---------+----------+
  |  M|  4|  2|       11|         6|
  +---+---+---+---------+----------+
  </code></pre>