Dear all,
for Grafana Docker image tag 10.0.1:
I need to compose an SQL query for a plugin (Databricks or Trino) with aggregated values:
SELECT …,
- AVG(column1) AS column1Aggregated,*
- AVG(column3) AS column3Aggregated,*
- …*
- AVG(column7) AS column7Aggregated,*
…
I have created a custom variable “function” for the aggregation function with values AVG, MAX, MIN etc. with single value selection.
For selection of the columns, I want to fetch these from the database (“SHOW COLUMNS”) as multi-value selection query variable.
Assume the user has selected $function=AVG and $columns=column1,column3,column5,column6: Is there any way to compose the string “AVG(column1) AS column1Aggregated,AVG(column3) AS column3Aggregated,AVG(column5) AS column5Aggregated,AVG(column6) AS column6Aggregated,” from the two variables? E.g., via a third variable which contains the “AVG(columnX) AS columnXAggregated” parts, then expanding in the final SQL query via ${someVariable:csv}?
Currently, I have made it half the way with a query variable using the Infinity plugin with all signals hard-coded as inline CSV with the lines “$function(column1) AS column1Aggregated” etc. and the regex (?.AS (?.).)*. The selection labels show “column1”, “column2” etc. and the variable values contain the strings to be used in the query, then expanded with ${columns:csv}. However, with this, the column names are all hard-coded in the inline CSV and not fetched from the database itself.
As far as I could see, the “chained variables” section in the documentation does not apply to this. Does anybody have an idea how to solve this? E.g. some “helper plugin” for text editing in variables, or a data source plugin which allows adding a constant part to each value (like $function + “(” + value + ") AS " + value + “Aggregated”)?
Thanks & Regards
Jochen