title: Django - Aggregate sum of difference between datetime column
body: <p>I have UserSession models that record first_action and last_action datetime columns,
  I want to know how long a user spent in the app - that is the sum of the difference between the two (ignoring those with Null last_action).</p>
  
  <p>Unfortunately django ORM aggregate functions only work on a single column, so for the sake of efficiency (there can be thousands of sessions per user) I wrote it in raw SQL, but because I got some high numbers I compared to a python sum.  I don't know why, but the two calculations do not show the same result, can you help me figure it out?</p>
  
  <p>Python:</p>
  
  <pre><code>sum(((us.last_action - us.first_action) for us in UserSession.objects.filter(user_id=1234) if us.last_action), datetime.timedelta(0))
  </code></pre>
  
  <p>SQL:</p>
  
  <pre><code>select sum(last_action - first_action) as total_time   from usersession where user_id = 1234 and last_action is not null;
  </code></pre>