Skip to content
Advertisement

Calculate mode() on an array column without skewing averages in PostgreSQL

I’ve designed a table to keep track of running processes:

Where pauses is the number of times the process has haulted and power_levels is an array of integers from 0 to 4, with repetitions allowed, representing power levels that that process has been consuming.

Using a single query I would like to select all processes executed within a span of a certain week and display:

  1. A breakdown of the average duration, per day of week
  2. The maximum number of pauses within a single process, throughout the week
  3. The most commonly used power level used throughout the week

So given example data:

I would like a result similar to:

So far I have:

But there are two issues with this query:

  1. Unnesting the power levels in the sub-query skew the calculation of the averages
  2. The final result groups the most used power level, and number of pauses per day of week, rather than the whole duration

Apart from splitting the query in 2, I’m not sure how to proceed from here. Is there a way to achieve this in a single query?

Advertisement

Answer

If you want the most used power level independent of the day of the week, then calculate it separately:

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement