title: Sql code for distinct fields
body: <p>I was wondering if anyone can help me with this query. </p>
  
  <p>I have two tables that I join together (<code>DDS2ENVR.QBO AND KCA0001.ORTS</code>) </p>
  
  <p>THE <code>QBO</code> Table has a field labeled NIIN AND RIC. THE <code>KCA0001.ORTS</code> table has a field named SERVICE and OWN_RIC.</p>
  
  <p>I Join the tables by <code>QBO.RIC</code> and <code>ORTS.OWN_RIC</code>. My dilemma is that under the NIIN field multiple rows can be identical but have different values for RIC. </p>
  
  <p>Example:</p>
  
  <pre><code>NIIN         RIC
  123455        A
  122222        B
  123456        C
  122222        A
  </code></pre>
  
  <p>I want to query a distinct count for <code>NIINS</code> that separates by the different service where it does not overlap. So example <code>NIIN</code> should only find distinct values only associated with A where the same <code>NIIN</code> is not found in B,C,D etc. </p>
  
  <pre><code>SELECT D.SERVICE, COUNT(C.NIIN)
  FROM DDS2ENVR.QBO C 
  JOIN KCA0001.ORTS D ON D.OWN_RIC = C.RIC
  WHERE C.SITE_ID = ('HEAA')
  GROUP BY D.SERVICE
  HAVING COUNT(DISTINCT C.NIIN) &gt; 1 
  </code></pre>
  
  <p>Please ask questions if this does not make any sense. </p>