title: SQL minimum of average
body: <p>For a query I need to select the records that are 'minimum of average'. So in theory I should do something like </p>
  
  <pre><code>select min(avg(x)) from tableA;
  </code></pre>
  
  <p>Since nested aggregation functions are not supported I'm not sure how to achieve this. I tried subquery's as wel, but I never could make it work so anyone an idea? Pseudo code is enough, I don't need a full solution. That's why I used pseudocode myself and not giving the data I'm working on.</p>
  
  <p>Thanks in advance!</p>
  
  <p>EDIT:
  Since it's required to give example/sampledata:</p>
  
  <p>I have 3 tables where I need to get the part, employee and duration from.
  I need the query to be grouped by part &amp; employee and need to get the minimumaverage of duration. </p>
  
  <pre><code>SELECT p.part, e.Name, min(avg(s.duration)) as AvgDuration
  FROM store s
  JOIN parts p
  ON p.PartId = s.PartId
  JOIN employees e
  ON e.EmployeeId = s.EmployeeId
  GROUP BY p.PartNr, e.Name
  ORDER BY PartNr;
  </code></pre>
  
  <p>I know that this is a faulty query but that should make clear what I'm aiming for.</p>
  
  <p>Based on the solution of Gordon,</p>
  
  <p>I have following query:</p>
  
  <pre><code>SELECT m.PartNr, m.Name, min(avg_duration) as Duration
  FROM (
      SELECT p.PartNr, e.Name, avg(s.Duration) as avg_duration
      FROM Store s
      JOIN parts p
      ON p.PartId = s.PartId
      JOIN employees e
      ON e.EmployeeId = s.EmployeeId
      GROUP BY p.PartNr, e.Name
  ) m
  GROUP BY m.PartNr, m.Name;
  </code></pre>
  
  <p>But what this does is for each employee give the minimum avg, while only the employee with the minimum should be shown.</p>