【发布时间】:2019-06-18 23:41:45
【问题描述】:
我在 Grafana 中有以下由 MySql 数据源支持的查询。
SELECT
$__timeGroupAlias(ts,$__interval),
sum(total) AS "total"
FROM hp
WHERE
$__timeFilter(ts)
AND customer_type IN ($CustomerType) AND age IN ($age) AND gender IN ($gender)
GROUP BY 1
ORDER BY $__timeGroup(ts,$__interval)
仪表板中有多个 singleStat/panel/graphs,它们使用不同的选择参数,但 WHERE 条件在所有这些中保持相同。
我想将条件保留为单独的常量变量,以便我可以在每个查询中仅添加该变量。
我尝试像这样构建我的查询。
SELECT
$__timeGroupAlias(ts,$__interval),
sum(total) AS "total"
FROM hp
$where_condition
GROUP BY 1
ORDER BY $__timeGroup(ts,$__interval)
并将where_condition 声明为WHERE $__timeFilter(ts) AND customer_type IN ($CustomerType) AND age IN ($age) AND gender IN ($gender)。
但是查询失败,因为内部变量($CustomerType,$age,$gender)没有被查询生成器解析,生成的查询看起来像这样。
SELECT
UNIX_TIMESTAMP(ts) DIV 900 * 900 AS "time",
sum(total) AS "total"
FROM hp
ts BETWEEN FROM_UNIXTIME(1548311714) AND FROM_UNIXTIME(1548398114)
AND customer_type IN ($CustomerType) AND age IN ($age) AND gender IN ($gender)
GROUP BY 1
ORDER BY UNIX_TIMESTAMP(ts) DIV 900 * 900
有没有办法解决包含在其他变量中的变量。或者是否有任何其他方法可以将包含变量的查询部分外部化?
【问题讨论】:
-
where_condition使用了哪种变量类型,您的 Grafana 版本是什么? -
Grafana 版本 = v5.4.3,
where_condition使用了常量类型。
标签: mysql grafana grafana-templating grafana-variable