title: T-SQL filtering records based on dates and time difference with other records
body: <p>I have a table for which I have to perform a rather complex filter: first a filter by date is applied, but then records from the previous and next days should be included if their time difference does not exceed 8 hours compared to its prev or next record (depending if the date is less or greater than filter date).</p>
  <p>For those adjacent days the selection should stop at the first record that does not satisfy this condition.</p>
  <p>This is how my raw data looks like:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>Id</th>
  <th>Desc</th>
  <th>EntryDate</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>1</td>
  <td>Event type 1</td>
  <td>2021-03-12 21:55:00.000</td>
  </tr>
  <tr>
  <td>2</td>
  <td>Event type 1</td>
  <td>2021-03-12 01:10:00.000</td>
  </tr>
  <tr>
  <td>3</td>
  <td>Event type 1</td>
  <td>2021-03-11 20:17:00.000</td>
  </tr>
  <tr>
  <td>4</td>
  <td>Event type 1</td>
  <td>2021-03-11 05:04:00.000</td>
  </tr>
  <tr>
  <td>5</td>
  <td>Event type 1</td>
  <td>2021-03-10 23:58:00.000</td>
  </tr>
  <tr>
  <td>6</td>
  <td>Event type 1</td>
  <td>2021-03-10 11:01:00.000</td>
  </tr>
  <tr>
  <td>7</td>
  <td>Event type 1</td>
  <td>2021-03-10 10:00:00.000</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>In this example set, if my filter date is '2021-03-11', my expected result set should be all records from that day plus adjacent records from 03-12 and 03-10 that satisfy the 8 hours condition. Note how record with Id 7 is not be included because record with Id 6 does not comply:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>Id</th>
  <th>EntryDate</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>2</td>
  <td>2021-03-12 01:10:00.000</td>
  </tr>
  <tr>
  <td>3</td>
  <td>2021-03-11 20:17:00.000</td>
  </tr>
  <tr>
  <td>4</td>
  <td>2021-03-11 05:04:00.000</td>
  </tr>
  <tr>
  <td>5</td>
  <td>2021-03-10 23:58:00.000</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>Need advice how to write this complex query</p>