Search code examples
promql

Group by measure values returned by aggregation


I am taking maximum value of a measure (stage) by group (instance) and then I want to show by maximum stage, what is the number of instances.

In SQL, this would be:

select max_stage, count(*)
from
  (select instance, max(stage) as max_stage 
   from table 
   group by instance
  ) x
group by 
  max_stage 

With PromQL, this does not work:

count by (stage) ((max by (instance) (stage)))

How do I reference max stage in the outer aggregation?


Solution

  • Use count_values instead of count:

    count_values ("some_name", max by (instance) (stage))
    

    https://prometheus.io/docs/prometheus/latest/querying/operators/#aggregation-operators