I’m stuck in trying to create a percentage of a single table, but with different values.
I have this telegraf plugin.exec which is uploading data to table patch_all. patch_all has one single field called value which match with the hostname and timestamp.
from(bucket: "telegraf")
|> range(start: -6h)
|> filter(fn: (r) => r["_measurement"] == "patch_all")
|> filter(fn: (r) => r["_field"] == "value")
|> filter(fn: (r) => r["host"] =~ /q/)
|> filter(fn: (r) => r._value >= 0)
|> aggregateWindow(every: 6h, fn: last, createEmpty: false)
|> set(key: "tag", value: "Total")
|> group(columns: ["tag"])
|> count()
from(bucket: "telegraf")
|> range(start: -6h)
|> filter(fn: (r) => r["_measurement"] == "patch_all")
|> filter(fn: (r) => r["_field"] == "value")
|> filter(fn: (r) => r["host"] =~ /q/)
|> filter(fn: (r) => r._value > 0)
|> aggregateWindow(every: 6h, fn: last, createEmpty: false)
|> set(key: "tag", value: "Bad")
|> group(columns: ["tag"])
|> count()```
So now I have two queries with return the **total** of hosts and another one with the total of hosts with value >0.
Now I do need to get the percentage of BAD out of TOTAL.
$BAD / $TOTAL * 100 =
How do I can achieve that with two queries into to the same table/bucket?
Thanks
Hello @jcorrea,
I’m assuming you want second query/ (first query) * 100?
Based off the set.
But I am confused because the values that the queries return are identical so you should get 100…because the queries are basically identical.
If you’re grouping by hosts do you expect multiple values from each total and bad for each host? Or just one?
@jcorrea There are a couple of different ways you can do this. Here’s an option that is pretty well optimized and should give you what you’re looking for: