title: SQL Query SUM DATEDIFF MAX
body: <p>I have a problem with a SQL-Query. I want to count the runtime of a used application. But in the database the date value is inserted more then one time. I only need the highest value of the pk_date column and no duplicated entries from the starttime column. </p>
  
  <p>Here is the SQL-Query:</p>
  
  <pre><code>SELECT DISTINCT Standortname,
  DATEPART(YEAR,PK_Date) AS Jahr,
  DATEPART(month,PK_Date) AS Monat,
  Lizenzname,
  COUNT(DISTINCT username) AS AnzahlUser,
  SUM(DISTINCT DATEDIFF(minute,starttime ,pk_date))  AS RuntimeMinute,
  endtime,
  pk_date
  
  FROM BenutzerLizenz,Benutzer,Abteilung,Lizenz,Standort
  WHERE 
  BenutzerLizenz.PK_ID_user=Benutzer.PK_ID_user
  AND BenutzerLizenz.PK_ID_lic=Lizenz.PK_ID_lic
  AND PK_ID_standort=FK_ID_standort
  AND DATEPART(month,PK_Date) = '04'
  AND DATEPART(YEAR,PK_Date) = '2013'
  AND Lizenzname = 'iman_1st'
  AND Standortname = 'Unterlüß'
  
  GROUP BY
   Standortname,
   DATEPART(YEAR,PK_Date),
   DATEPART(month,PK_Date),
   Lizenzname,
   starttime,
   endtime,
   pk_date
  </code></pre>
  
  <p>Here is the result:</p>
  
  <pre><code>... RuntimeMinute   starttime                   pk_date
      339         2013-04-11 11:05:00.0000000 2013-04-11 16:44:37.9650000
      346         2013-04-11 11:05:00.0000000 2013-04-11 16:51:25.4800000
      356         2013-04-11 11:05:00.0000000 2013-04-11 17:01:19.9670000
      475         2013-04-11 10:06:00.0000000 2013-04-11 18:01:15.6620000
  </code></pre>
  
  <p>The first three above runtimes are from the same user and session, the last one is from another user and session. I only want to count and sum the last runtimes from the same starttime and the maximum date inserted (pk_date) -> 356 + 475 is the value that I would like to have.</p>
  
  <p>In another similar query all values are accumulated (the columns starttime, endtime, pk_date are not included in it, so the query builds the sum of all runtime values for all users). I tried to use DISTINCT and MAX(pk_date) but it didn't work as expected. Do I have to use Sub-Queries?</p>