title: Need to count certain rows based on date criteria in SQL Server
body: <p>I'm using SQL Server 2012 and have a table with these 2 columns.  I need to count an ORG_ID once ONLY IF the EndDate for every row or that ORG_ID falls within/before the timeframe of '1-1-2018' and '1-31-2018' (or before but NOT after) for ALL rows for that org.  An ORE with an EndDate of NULL would also NOT be in my results</p>
  
  <pre><code>ORG_ID              EndDate    
  99968042            1/31/2018    
  99968042            2/14/2018    
  99968042            2/14/2018    
  99900699            1/10/2018    
  99900699            1/10/2018    
  99900699            1/10/2018    
  99900699            1/10/2018    
  99899776            1/20/2018    
  99843366           12/17/2017    
  99843366             1/4/2018    
  99841000             2/1/2016    
  99651255             NULL
  99651255             1/15/2018
  </code></pre>
  
  <p>The rows that should output are:  </p>
  
  <pre><code>99900699    
  99899776    
  99843366    
  </code></pre>
  
  <p>I haven't tried anything, because I can't think how to approach it.</p>
  
  <p>So now I've tried this:  </p>
  
  <pre><code>select distinct ORG_ID
  from ##PLCMT p1
  where not exists (
      select *
      from ##PLCMT p2
      where p1.ORG_ID = p2.ORG_ID and
            (p1.EndDate &lt;= '2018-01-01' or p1.enddate &gt;= '2018-01-31' or p1.EndDate is NULL)
     )  
  </code></pre>
  
  <p>and it is still resulting back an org that has a NULL enddate, I can't figure out why.  ORG_ID 3098376 is in my results but if I look at all the rows for that ORG_ID, it looks like this:  </p>
  
  <pre><code>select *
  from ##PLCMT
  where org_id = '3098376'  
  </code></pre>
  
  <p>results:<br>
      ORG_ID     EndDate<br>
      3098376    2017-09-11<br>
      3098376    NULL<br>
      3098376    NULL  </p>