title: SQL Server 2008: DateDiff() of Min()/ Max() not working
body: <p>Could you help me out with the following query?</p>
  
  <p><strong>My goal:</strong></p>
  
  <p>For each day and for each employee select minimum datetime and the maximum datetime and get the time difference in minutes</p>
  
  <p><strong>My query :</strong></p>
  
  <pre><code>SELECT 
     PersonalId, Name, LastName, 
     Min(TmsPrick) as MinDatetime, 
     Max(TmsPrick) as MaxDatetime, 
     DATEDIFF(mi, MIN(TmsPrick), MAX(TmsPrick)) AS Expr1
  FROM 
     prickTable
  GROUP BY 
     DATEPART(dy, TmsPrick), PersonalId, name, lastname, TmsPrick
  ORDER BY 
     PersonalId, TmsPrick
  </code></pre>
  
  <p><strong>Note:</strong>
  DATEPART(dy, TmsPrick) = Day of year</p>
  
  <p><strong>My expected output</strong></p>
  
  <pre><code>  PersonalId | Name      | LastName   | MinDatetime       | MaxDatetime        | Expr1
    ------------------------------------------------------------------------------------
     000001    | A         | AC         |2014-01-05 07:12:00|2014-01-05 16:00:00 | 528
     000001    | A         | AC         |2014-01-06 07:08:00|2014-01-05 16:00:00 | 532
     000001    | A         | AC         |2014-01-07 07:11:00|2014-01-07 16:00:00 | 529
     000002    | B         | BD         |2014-01-05 07:11:00|2014-01-05 16:00:00 | 529
  </code></pre>
  
  <p><strong>The output I'm getting:</strong></p>
  
  <pre><code>  PersonalId | Name      | LastName   | MinDatetime       | MaxDatetime        | Expr1
    ------------------------------------------------------------------------------------
     000001    | A         | AC         |2014-01-05 07:12:00|2014-01-05 16:00:00 | 1053168
     000001    | A         | AC         |2014-01-06 07:08:00|2014-01-05 16:00:00 | 532
     000001    | A         | AC         |2014-01-07 07:11:00|2014-01-07 16:00:00 | 529
     000002    | B         | BD         |2014-01-05 07:11:00|2014-01-05 16:00:00 | 1053169
  </code></pre>
  
  <p>Any idea why my output shows such differences?</p>
  
  <p>Thanks</p>
  
  <p><strong>Note:</strong> The values in Tmsprick may vary from several a day to 2 in a week. The table contains values from over 3 years.</p>