title: Group by measure values returned by aggregation
body: <p>I am taking maximum value of a measure (<code>stage</code>) by group (<code>instance</code>) and then I want to show by maximum stage, what is the number of instances.</p>
  <p>In SQL, this would be:</p>
  <pre><code>select max_stage, count(*)
  from
    (select instance, max(stage) as max_stage 
     from table 
     group by instance
    ) x
  group by 
    max_stage 
  </code></pre>
  <p>With PromQL, this does not work:</p>
  <pre><code>count by (stage) ((max by (instance) (stage)))
  </code></pre>
  <p>How do I reference max stage in the outer aggregation?</p>