title: Count previous row only if > 2 days later
body: <p>In SQL Server 2012 using Studio: I need results displayed count of distinct clientnumbers (CN) for re-entry, grouped by Type like this:  </p>
  
  <pre><code>Type    CountOfCN    
  5        1  
  10       3  
  </code></pre>
  
  <p>Only a RE-entry counts (ENTRY_NO 1 never counts)  and it has to be more than 2 days after the end of the previous entry for that clientnumber. So basically ENTRY_NO 1 doesn't count. ENTRY_NO 2 counts if it's startdate is more than 2 days after the enddate of ENTRY_NO 1, and so on with ENTRY_NO 3, 4, 5.</p>
  
  <p>I got ENTRY_NO by doing a ROW_NUMBER function when I created the table. I have no idea how to go about creating a datediff or dateadd function (?) to look at the previous row's enddate and calculate it with my startdate for each CN?</p>
  
  <p>Here is my table:</p>
  
  <pre><code>CN    STARTDATE   ENDDATE    TYPE  ENTRY_NO  
  1     1/1/2018    1/20/2018  10    1
  1     1/21/2018   1/30/2018   5    2
  1     2/3/2018    NULL       10    3
  2     1/1/2018    1/20/2018  10    1
  2     1/27/2018   1/30/2018  10    2
  3     1/1/2018    1/20/2018   5    1
  3     1/27/2018   1/30/2018  10    2
  3     2/10/2018   2/20/2018   5    3
  4     1/7/2018    1/30/2018   5    1
  5     1/27/2018   1/30/2018   5    1
  5     1/31/2018   NULL        5    2
  </code></pre>
  
  <p>So the rows that should be in the results are ENTRY_NO 2 for CN 1, ENTRY_NO 2 for CN 2, ENTRY_NO 2 &amp; 3 for CN 3. </p>
  
  <p>Only the last Entry may/may not have a NULL enddate</p>