title: Calculate maximum number of concurrent user sessions
body: <p>I have a <code>UserSession</code> table in PostgreSQL 9.6 that stores user's login and logout time, I want to calculate the maximum number of concurrent sessions - which are only considered to be concurrent if they <strong>overlap for at least 30 minutes</strong>.</p>
  
  <p><strong>Example</strong></p>
  
  <pre><code>userid      |  starttime                |  endtime  
  ------------+---------------------------+--------------------------
  1           |  01-Oct-19 6:00:00 AM     |    01-Oct-19 11:10:00 AM  
  2           |  01-Oct-19 11:00:00 AM    |    01-Oct-19 4:00:00 PM 
  3           |  01-Oct-19 10:30:00 AM    |    01-Oct-19 4:00:00 PM 
  </code></pre>
  
  <p>Here, session 1 and 2 are not concurrent since the they only overlap for 10 mins and session 1 and 3 are concurrent since they overlap for more than 30 mins, So the <strong>result is 2 concurrent sessions</strong>.  </p>
  
  <p><strong>NOTE:</strong> Result will only be n if all n sessions overlap for at least 30 mins.</p>
  
  <p><strong>Table Definition</strong></p>
  
  <pre class="lang-sql prettyprint-override"><code>CREATE TABLE UserSessions (
      SessionID bigserial NOT NULL,
      UserID bigint NOT NULL,
      StartTime timestamp NOT NULL,
      EndTime timestamp NULL,
      OrganisationID bigint NOT NULL,
      CONSTRAINT PK_SessionsID PRIMARY KEY(SessionID),
      CONSTRAINT FK_UserID FOREIGN KEY(UserID) REFERENCES GlobalUsers(UserID),
      CONSTRAINT FK_OrganisationID FOREIGN KEY(OrganisationID) REFERENCES Organisations(OrganisationID)
  );
  </code></pre>
  
  <p><strong>Similar Questions</strong></p>
  
  <p>There is a similar question here: <a href="https://stackoverflow.com/questions/29081000/count-max-number-of-concurrent-user-sessions-per-day/29081490">Count max number of concurrent user session</a>, but there concurrent means at the same point in time and in my case I need to check if they overlap for at least 30 mins</p>