title: Retrieve two records within the same table and get datediff in one query
body: <p>I have this table</p>
  
  <pre><code>sessionid | serviceid | requesttimestamp | etc..
  </code></pre>
  
  <p>My aim is to get a serviceid for logging in and logging out which for arguments sake we will say is 2 and 3. So when the two records have a log in and log out then I want to make sure the sessionid is the same and then datediff the timestamp in order to find out how long the user have been logged in. I also want a condition that if the serviceid for logging in exists for one and not exists for logging out then to skip that record in the results. I wondered if there was a magic way to combine this in one query, so far i have </p>
  
  <pre><code>Declare @sessionStart DATETIME , @sessionStart2 as DATETIME
  set @sessionStart = (select  requesttimestamp from logentry where serviceid=151 and sessionid = '1234')
  set @sessionStart2 = (select requesttimestamp from logentry where serviceid=202 and sessionid = '1234')
  
  
  select datediff(mi, @sessionStart, @sessionStart2 );
  </code></pre>
  
  <p>This brings me back the required result without the null checking, I have been looking into simple case statements but I am attempting to make this into one query if possible. Any pointers in the right direction would be great</p>