title: how to sum max values in application insights analytics
body: <p>we are going to collect usage metrics (meaning customMeasurements via customEvents) from our application via application insights. The data is collected if our (windows) service starts and then on a daily base via a timer. </p>
  
  <p>I Know that app insights is not meant to use for "on premises" software, but we do it anyway ;-)</p>
  
  <p>So we get the data from all the services that are installed at our clients. Each client has a unique id (GUID) which allows us to group by customer. (fyi we don't know which customer is behind this guid, it's just a value for "grouping" by customer)</p>
  
  <p>the event looks like this:</p>
  
  <p><a href="https://i.stack.imgur.com/q6Sgt.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/q6Sgt.png" alt="enter image description here"></a></p>
  
  <ol>
  <li>I now want to group by customerId, </li>
  <li>get the maximum value for a specific metricValue </li>
  <li>and create the sum for all customers. </li>
  </ol>
  
  <p>I got 1 and 2 already but have no clue how to sum the max values...</p>
  
  <p><a href="https://i.stack.imgur.com/SXcFq.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/SXcFq.png" alt="enter image description here"></a></p>
  
  <p>demonstrate what I'm trying to do I added a example in SQL:</p>
  
  <pre><code>CREATE TABLE [dbo].[metricData]
  (
      [RecId] [int] IDENTITY(1,1) NOT NULL,
      [customerId] [int], 
     [metricValue1] [int],
     [metricValue2] [int]
  )
  
  
  INSERT INTO [dbo].[metricData]     VALUES ( 1234, 1,1)
  INSERT INTO [dbo].[metricData]     VALUES ( 1234, 1,2)
  INSERT INTO [dbo].[metricData]     VALUES ( 1234, 1,1)
  INSERT INTO [dbo].[metricData]     VALUES ( 2345, 6,4)
  INSERT INTO [dbo].[metricData]     VALUES ( 2345, 8,7)
  INSERT INTO [dbo].[metricData]     VALUES ( 3456, 1,1)
  INSERT INTO [dbo].[metricData]     VALUES ( 3456, 1,2)
  INSERT INTO [dbo].[metricData]     VALUES ( 3456, 1,1)
  INSERT INTO [dbo].[metricData]     VALUES ( 4576, 20,30)
  
  
  select sum(maxVal1),sum(maxVal2) from
  (
    select max(metricValue1) as maxVal1, max(metricValue2) as maxVal2 from metricData
    group by customerId
  ) t
  </code></pre>
  
  <p>Basically the same as also asked here, but for app insights :-) 
  <a href="https://stackoverflow.com/questions/14657881/sql-sum-the-max-values-of-results-returned">SQL: SUM the MAX values of results returned</a></p>
  
  <p>Thanks for any hints</p>