title: T-SQL 2012- How can I pair down query and check the returned values
body: <p>I have a table which captures various events and returns data as below</p>
  
  <pre><code>+----+-----------+--------------+-----------------+
  | ID | EventType | SystemUserID | OrganisationID  |
  +----+-----------+--------------+-----------------+
  | 23 |         8 |           11 |               1 |
  | 44 |         7 |          456 |             304 |
  | 80 |         1 |           48 |            4042 |
  | 89 |         2 |         5673 |             183 |
  +----+-----------+--------------+-----------------+
  </code></pre>
  
  <p>EventType 8 is captured each time a user visits an organisation. </p>
  
  <p>I have another table named OrganisationHasContact as such </p>
  
  <pre><code>+----+----------------+--------------+
  | ID | OrganisationID | SystemUserID |
  +----+----------------+--------------+
  | 23 |              1 |           11 |
  | 44 |            304 |          456 |
  | 80 |           4042 |           48 |
  | 89 |            183 |         5673 |
  +----+----------------+--------------+
  </code></pre>
  
  <p>I have wrote a query in order to find the total number of visits each organisation has had but excluding SystemUsers who are associated to that organisation</p>
  
  <pre><code>SELECT 
  OrganisationID AS [OrganisationID], 
  ORG.Name AS [Compayny Name],
  Count(OrganisationID) AS [Visits]
  FROM [Audit].[EventData]
  LEFT OUTER JOIN Employed.Organisation AS ORG ON [EventData].OrganisationID = ORG.ID
  Where Not Exists(
          Select * From [Audit].[EventData] AS A2
          Inner join Employed.OrganisationHasContact AS OHC On [EventData].OrganisationIDID = OHC.OrganisationID
          Where OHC.SystemUserID = [EventData].SystemUserID 
        )
  AND
  EventTypeID = 8
  Group BY [EventData].OrganisationID,
           ORG.Name
  Order By Visits Desc
  </code></pre>
  
  <p>To test the above query was returning correct values (449 visits) I did the following </p>
  
  <p>First I ran this query to get all SystemUserId's associated to Org 1741 </p>
  
  <pre><code>Select * from Employed.OrganisationHasContact 
  Where OrganisationID = 1741
  </code></pre>
  
  <p>Then with the returned SystemUserID's I ran this query</p>
  
  <pre><code>SELECT Distinct
  OrganisationID AS [OrganisationID], 
  ORG.Name AS [Compayny Name],
  Count(OrganisationID) AS [Visits]
  FROM [Audit].[EventData]
  LEFT OUTER JOIN Employed.Organisation AS ORG ON [EventData].OrganisationID = ORG.ID
  Where
  EventTypeID = 8
  AND SystemUserID NOT IN (35, 4602, 48, 4603, 7704)
  Group BY OrganisationID,
           ORG.Name
  Order By Visits Desc
  </code></pre>
  
  <p>This Returned 446 visits for Org 1741, my original query returns 449 visits.</p>
  
  <p>What could be causing the difference?</p>
  
  <p>Can I improve my original query somehow?</p>
  
  <p>Is there a better way to test this?</p>
  
  <p>Thanks</p>