title: MySQL extract average data from multiple group criteria
body: <p>Apologies for the wall of text, the example in the end explains my question. Any help is appreciated, thank you!</p>
  
  <p>I have a table which contains several columns of data from among other values voltages and currents. </p>
  
  <p>These instances are logged every second when there is current flowing. I want to calculate an approximated <code>kJ</code> and <code>kW</code> from these values. </p>
  
  <p>Basically I have one table, <strong><code>instances</code></strong>, that contains:</p>
  
  <ul>
  <li><code>instanceID</code>, </li>
  <li><code>location</code>, </li>
  <li><code>current</code>, </li>
  <li><code>voltage</code>, </li>
  <li><code>time</code>.</li>
  </ul>
  
  <p>And another one, <strong><code>sets</code></strong>, that contains:</p>
  
  <ul>
  <li><code>instanceID</code>, </li>
  <li><code>setID</code>.</li>
  </ul>
  
  <p>The <code>instanceID</code> is the same, the <code>instanceID</code> in instances is a FK pointing to sets. For every location in instances there are approximately 23 rows (varies). There are 30 locations. So I have 23 rows where the instance have location 1, another 23 for the same instance for location 2 and so on. Time is the logged time for when the measured data was taken (so if the difference is one second between all the 23 instances the difference between the first and the last time is 23 seconds). </p>
  
  <p>I need to calculate the average <code>kW</code> and the total <code>kJ</code> (approximated). </p>
  
  <p>What I've done is the following:</p>
  
  <pre><code>SELECT instances.instanceID, location, current, 
         voltage, current * voltage AS kW, 
         COUNT(IF(current &gt; 0 AND voltage &gt; 0, 
                  instances.instanceID, 
                  0)) AS InstancedTime
  FROM instances
  INNER JOIN sets ON instances.instanceID = sets.instanceID
  WHERE sets.setID = arbitrary_number;
  </code></pre>
  
  <p>The problem arises that I get the following table:</p>
  
  <p><code>instanceID, location, current, voltage, kW, InstancedTime</code></p>
  
  <p>The <code>kW</code> is a random number from one of the 23 sets, which is fine since it's an approximation anyway, but the <code>COUNT(IF())</code> is counting <em>ALL</em> the instances in the instances table, when I only want the query to count the instances for every location.</p>
  
  <p>I tried the <code>MAX(CAST(time AS SIGNED)) - MIN(CAST(time AS SIGNED))</code>, but that takes the max time from the <em>last</em> location minus the min time of the <em>first</em> location, I want to isolate it to one location at a time. </p>
  
  <p>What I want to do is get the total amount of <code>kJ</code> which would be the time it had power multiplied by the kW of that time. Since I know the time is always 1 second between the instances it should be enough to count the number of instances for individual locations and multiply that by the <code>kW</code>, however I want to do that for all the instances <em>within</em> one set. It is possible to replace the set by using a single query for all the individual instances but that would take eons.</p>
  
  <p>I'm trying to take a table that looks like</p>
  
  <pre><code>instanceID, location, current, voltage, kW, InstancedTime
  1, 1, 500V, 2A, 1kW, 1s
  1, 1, 500V, 2A, 1kw, 1s
  1, 2, 400V, 3A, 1.2kW, 1s
  1, 2, 400V, 3A, 1.2kW, 1s
  2, 1, 700V, 2A, 1.4kW, 1s
  2, 1, 700V, 2A, 1.4kw, 1s
  2, 2, 300V, 3A, 0.9kW, 1s
  2, 2, 300V, 3A, 0.9kW, 1s
  </code></pre>
  
  <p>And add the <code>kJ</code> which would be summarising the number of instances that ID 1 has been in location 1 and location 2, doing the same for ID 2 and presenting this all in one table that would look like:</p>
  
  <pre><code>instanceID, location, current, voltage, kW, SumInstancedTime, kJ
  1, 1, 500V, 2A, 1kW, 2s, 2kJ
  1, 2, 400V, 3A, 1.2kW, 2s, 2.4kJ
  2, 1, 700V, 2A, 1.4kW, 2s, 2.8kJ
  2, 2, 300V, 3A, 0.9kW, 2s, 1.8kJ
  </code></pre>
  
  <p>Thank you for your time, any provided help is appreciated!</p>