title: How to compare the difference for the last row in SQL
body: <p>I have a view and this is the result of it:</p>
  
  <pre><code>ID  Type  Time                                RowNum
  18  2     2015-12-14 12:15:00.9330000 +00:00  214
  18  6     2015-12-14 12:21:22.4330000 +00:00  215
  18  2     2015-12-15 09:04:41.2870000 +00:00  216
  18  6     2015-12-15 09:06:22.9400000 +00:00  217
  18  5     2015-12-15 09:07:28.0130000 +00:00  218
  </code></pre>
  
  <p>I am trying to compare the time difference between rows and group by the type by using the query below. My issue is for the last row I want to get the difference wth the current time and calculate the sum of difference. This is the query am using. How to compare only for the last row with the current time and calculate the sum:</p>
  
  <pre><code>WITH rows AS (SELECT * FROM [dbo].[View])
  SELECT  p.ID ,
          p.Type ,
          SUM(DATEDIFF(SECOND, p.Time, q.Time))
  FROM    rows p
          JOIN rows q ON p.RowNum = q.RowNum - 1
  GROUP BY p.ID ,
           p.Type
  </code></pre>