title: SQL Server - Selecting Frequent Records Within Time Frame
body: <p>I am looking for a way to select all the records where one column has the same records and the datetime is within 5 minutes of each other. I manage a ticketing system, and I am trying to view all similar tickets created within a 5-minute time frame. See below for a sample dataset:</p>
  <pre><code>|ID |Subject    |CreatedDateTime    |
  -------------------------------------
  |1  |A          |2020-09-28 11:01:00|
  |2  |A          |2020-09-28 11:02:00|
  |3  |A          |2020-09-28 11:03:00|
  |4  |A          |2020-09-28 11:03:09|
  |5  |A          |2020-09-28 11:04:52|
  |6  |A          |2020-09-28 11:15:00|
  |7  |B          |2020-09-28 11:20:00|
  |8  |B          |2020-09-28 11:20:00|
  |9  |B          |2020-09-28 11:20:00|
  </code></pre>
  <p>My goal is to only select records 1-5, because 5 the same Subject and they were all created within 5 minutes of each other. 6-10 should not be selected because the amount of Subjects is not great enough, or it is outside the specified time frame.</p>
  <p>Below is a test query that I have so far, yet it does not take into account the 5-minute range (I am only looking back 1 week, hence that clause):</p>
  <pre><code>SELECT Subject,COUNT(*)
  FROM TableName
  WHERE CreatedDateTime &gt; DATEADD(day, -7, GETDATE())
  GROUP BY Subject
  HAVING COUNT(*) &gt; 5 
  ORDER BY COUNT(*) DESC;
  </code></pre>
  <p>Is there any way to only see similar records in a close time frame? Thank you all in advance!</p>