title: Mysql Select rows with same values in one column and different in another
body: <p>I'd really appreciate it if someone could validate my SQL query.</p>
  
  <p>For the following dataset:</p>
  
  <pre><code>MD5      UserPK      CategoryPK    
  ADCDE    1           7  
  ADCDE    1           4  
  ADCDE    1           7  
  dffrf    1           7  
  dffrf    2           7  
  dffrf    2           6 
  dffrf    1           1 
  </code></pre>
  
  <p>I'd like to select MD5 and CategoryPK where two or more rows exist with identical MD5 values, identical CatgegoryPK and two or more DIFFERENT UserPK values.</p>
  
  <p>In other words, I'd like to know the MD5 and categoryPK of all  records where two or more different users (UserPK) have assigned the same category (UserPK) to the same file (Md5). I'm not interested in records the same user has assigned the category to multiple times, (unless a different user has also assigned the same category to that file).</p>
  
  <p>So from the above data, I would like to be returned just:</p>
  
  <pre><code>md5    CategoryPK
  dffrf  7
  </code></pre>
  
  <p>The query I've written is:</p>
  
  <pre><code>SELECT md5, 
         count(md5), 
         count(distinct categorypk) as cntcat, 
         count(distinct userpk) as cntpk
  FROM Hash
         group by md5 having count(md5) &gt; 1 
                             and cntpk &gt; 1
                             and cntcat = 1;
  </code></pre>
  
  <p>It seems to work, but before I start using it in anger, I'd appreciate a second opinion in case I've missed something or if there is a better way of doing it.</p>
  
  <p>Thanks</p>