title: SQL find groups based on time ranges
body: <p>I have a table that has the following columns:</p>
  
  <ul>
  <li>Purchase_ID (varchar)</li>
  <li>User_ID (varchar)</li>
  <li>Purchase_time (bigint) - in UNIX</li>
  </ul>
  
  <p>with some other less relevant ones.</p>
  
  <p>There are over 60k unique purchase IDs.
  all distinct User_IDs appear at least 4 times (usually a lot more) - meaning these are users that made at least 4 purchases.</p>
  
  <p>What I want to do, is find groups of User_IDs, that have made purchases within similar time ranges.
  For example, take User A, find all other users that have made purchases within one hour before and after each one of user A's purchases, then take these two users, repeat the process, until I reach the highest possible number of users that have made purchases together.
  The amount of purchases connecting all members of the group should be no less than 3.</p>
  
  <p>I realize this is kind of complex and will possibly result in many different tables, but I have no idea how to start going about this so all help would be appreciated.</p>
  
  <p>Thanks to all helpers and have a pleasant day!</p>
  
  <p>edit:</p>
  
  <ul>
  <li><p>as far as a data base tag goes, I am using AWS Athena, I'm not sure which functions it supports as I'm completely new to it.</p></li>
  <li><p>Sample Data:</p></li>
  </ul>
  
  <blockquote>
  <pre><code>User ID   Time
  User_A    1521441850
  User_B    1521441930
  User_C    1521500000
  User_D    1521530023
  User_E    1521441900
  User_F    1521000000
  User_A    1521445850
  User_B    1521445980
  User_C    1521510000
  User_D    1521520055
  User_E    1521445100
  User_F    1521000010
  User_A    1521449850
  User_B    1521445030
  User_C    1521520000
  User_D    1521500033
  User_E    1521441910
  User_F    1521000020
  User_A    1521453850
  User_B    1521441920
  User_C    1521530000
  User_D    1521540000
  User_E    1521400000
  User_F    1521000030
  </code></pre>
  </blockquote>
  
  <ul>
  <li>Desired Result:
  I'm Flexible on this, but essentially could be something like this:</li>
  </ul>
  
  <p>Table 1</p>
  
  <blockquote>
  <pre><code>Group1    Group2
  User_A    User_C
  User_B    User_D
  User_E
  </code></pre>
  </blockquote>
  
  <p>Table 2</p>
  
  <blockquote>
  <pre><code>Group Name    Amount of Users Amount of Purchases Linked
  Group1        3               11
  Group2        2               8
  </code></pre>
  </blockquote>