Count() not working as expected in SQL
<p>A coworker put together a table for me that I need for mapping data, and table consists of two fields,  OP_ID and BillType.  In theory, there should be a one-to-one correspondence between the two:  every Op_ID should have one and only one bill type. </p>

<p>When I started using it, however, I noticed that there were duplicate OP_IDs with different BillTypes.  E.g:</p>

<pre><code>OP_Id               BillType 
007a000v9GWkAAM BillReady 
007a000v9GWkAAM RateReady
</code></pre>

<p>So first I built a query to group the data into unique combinations of IDs and BillTypes:</p>

<pre><code>SELECT OP_ID, BillType
FROM MappingTable
GROUP BY OP_ID, BillType
</code></pre>

<p>Simple enough.  This set should and does include the above two records.  Then, I wrapped another query around this to count OP_Ids within the aggregated dataset.  Theoretically, any OP_ID with more than one BillType should occur twice, and so it should return a count > 1.  Right?</p>

<pre><code>SELECT OP_ID, BillType, Count(OP_ID)
FROM 
    (
    SELECT OP_ID, BillType
    FROM MappingTable
    GROUP BY OP_ID, BillType
    ) Base
GROUP BY  OP_ID, BillType
HAVING Count(OP_ID) &gt; 1
</code></pre>

<p>But this query returns nothing whatsoever.  Here's what even more baffling:  when I remove the HAVING clause and limit the query to pull only the above OP_ID, since I already know it's a dupe, where's what I get:</p>

<pre><code>OP_ID          BillType  CountOfOP_IDs
007a000v9GWkAAM BillReady   1
007a000v9GWkAAM RateReady   1
</code></pre>

<p>So there are clearly two records for OP_ID 007a000v9GWkAAM, but SQL is counting only one!  </p>

<p>This seems so simple, and I'm sure I'm just missing something basic about how COUNT() works. For reference, I'm working on SQL Server 2014 and both columns are nvarchar.  I've also confirmed that SQL evaluates the OP_IDs in both records as identical.  Anyone know why this is happening?</p>