Procstat heat map

Hi,

I’m trying to build heat map visualization with processes gathered with telegraf’s procstat plugin.
I have an influx query:

SELECT last("cpu_usage") FROM "procstat" WHERE ("host" =~ /^$Host$/ AND "process_name" =~ /^$Process$/) AND $timeFilter GROUP BY time($interval), "process_name"

but I want to have processes sorted according to last variable “Sort”. See the screenshot for better understanding. Is that possible at all?

Secondly, I don’t know how to limit number of processes to top 10, “limit” clausule seems limiting only number of all results and does not take “where” into account.

Thanks for help.

Small tunning, two queries:

SELECT last("cpu_usage") FROM "procstat" WHERE ("host" =~ /^$Host$/ AND "process_name" =~ /^$Process$/ AND "user" =~ /^$User$/) AND $timeFilter GROUP BY time($interval), "process_name", "user"
SELECT last("memory_rss") FROM "procstat" WHERE ("host" =~ /^$Host$/ AND "process_name" =~ /^$Process$/ AND "user" =~ /^$User$/) AND $timeFilter GROUP BY time($interval), "process_name", "user"