title: How can I get the length of a session in SQL given multiple timestamps?
body: <p>I have a table in MS SQL Server in which I run the following query:</p>
  
  <pre><code>  SELECT Time, SessionId
    FROM dbName
    WHERE SessionId IS NOT NULL
    ORDER BY Time, SessionId
  </code></pre>
  
  <p>This returns a set of timestamps associated with a SessionId, and of course, there are multiple SessionIds. My question is, how can I further refine this query to return the average length of a session? i.e. get earliest timestamp and latest timestamp, diff them, and return value in seconds or minutes, and do this per unique SessionId.</p>
  
  <p>From this, I think I can get the longest session, the shortest session and the average length of a session.</p>
  
  <p>The timestamp looks like this: 2014-08-19 18:20:28.4359918</p>