Grafana、postgresql:聚合函数调用不能包含window个函数调用
Grafana, postgresql: aggregate function calls cannot contain window function calls
在 Grafana 中,我们想要显示指示所选时间间隔内 15 分钟平均值最大值的条形图。我们的数据有规律的 1 分钟间隔。数据库是Postgresql。
为了显示 15 分钟的平均值,我们使用以下查询:
SELECT
timestamp AS time,
AVG(rawvalue) OVER(ORDER BY timestamp ROWS BETWEEN 7 PRECEDING AND 7 FOLLOWING) AS value,
'15-min Average' AS metric
FROM database.schema
WHERE $__timeFilter(timestamp) AND device = '$Device'
ORDER BY time
要显示指示所选时间间隔内原始值最大值的条柱,我们使用以下查询:
SELECT
$__timeGroup(timestamp,'$INTERVAL') AS time,
MAX(rawvalue) AS value,
'Interval Max' AS metric
FROM database.schema
WHERE $__timeFilter(timestamp) AND device = '$Device'
GROUP BY $__timeGroup(timestamp,'$INTERVAL')
ORDER BY time
两种解决方案的天真组合不起作用:
SELECT
$__timeGroup(timestamp,'$INTERVAL') AS time,
MAX(AVG(rawvalue) OVER(ORDER BY timestamp ROWS BETWEEN 7 PRECEDING AND 7 FOLLOWING)) AS value,
'Interval Max 15-min Average' AS metric
FROM database.schema
WHERE $__timeFilter(timestamp) AND device = '$Device'
GROUP BY $__timeGroup(timestamp,'$INTERVAL')
ORDER BY time
我们收到错误:“pq:聚合函数调用不能包含 window 函数调用”。
关于 SO 的建议是使用“with”(),但我不知道在我们的案例中是否适合使用它。
将第一个查询用作第二个查询的 CTE(或 with
)。不再需要 CTE 的 order by
子句和第二个查询的 where
子句以及 CTE 的 metric
列。或者,您可以将第一个查询用作第二个查询的 from
子句中的派生 table。
with t as
(
SELECT
timestamp AS time,
AVG(rawvalue) OVER(ORDER BY timestamp ROWS BETWEEN 7 PRECEDING AND 7 FOLLOWING) AS value
FROM database.schema
WHERE $__timeFilter(timestamp) AND device = '$Device'
)
SELECT
$__timeGroup(time,'$INTERVAL') AS time,
MAX(value) AS value,
'Interval Max 15-min Average' AS metric
FROM t
GROUP BY 1 ORDER BY 1;
不相关,但 $__timeFilter
和 $__timeGroup
是什么?它们的语义很清楚,但它们来自哪里?顺便说一句,您可能会发现 this 函数很有用。
在 Grafana 中,我们想要显示指示所选时间间隔内 15 分钟平均值最大值的条形图。我们的数据有规律的 1 分钟间隔。数据库是Postgresql。
为了显示 15 分钟的平均值,我们使用以下查询:
SELECT
timestamp AS time,
AVG(rawvalue) OVER(ORDER BY timestamp ROWS BETWEEN 7 PRECEDING AND 7 FOLLOWING) AS value,
'15-min Average' AS metric
FROM database.schema
WHERE $__timeFilter(timestamp) AND device = '$Device'
ORDER BY time
要显示指示所选时间间隔内原始值最大值的条柱,我们使用以下查询:
SELECT
$__timeGroup(timestamp,'$INTERVAL') AS time,
MAX(rawvalue) AS value,
'Interval Max' AS metric
FROM database.schema
WHERE $__timeFilter(timestamp) AND device = '$Device'
GROUP BY $__timeGroup(timestamp,'$INTERVAL')
ORDER BY time
两种解决方案的天真组合不起作用:
SELECT
$__timeGroup(timestamp,'$INTERVAL') AS time,
MAX(AVG(rawvalue) OVER(ORDER BY timestamp ROWS BETWEEN 7 PRECEDING AND 7 FOLLOWING)) AS value,
'Interval Max 15-min Average' AS metric
FROM database.schema
WHERE $__timeFilter(timestamp) AND device = '$Device'
GROUP BY $__timeGroup(timestamp,'$INTERVAL')
ORDER BY time
我们收到错误:“pq:聚合函数调用不能包含 window 函数调用”。
关于 SO 的建议是使用“with”(
将第一个查询用作第二个查询的 CTE(或 with
)。不再需要 CTE 的 order by
子句和第二个查询的 where
子句以及 CTE 的 metric
列。或者,您可以将第一个查询用作第二个查询的 from
子句中的派生 table。
with t as
(
SELECT
timestamp AS time,
AVG(rawvalue) OVER(ORDER BY timestamp ROWS BETWEEN 7 PRECEDING AND 7 FOLLOWING) AS value
FROM database.schema
WHERE $__timeFilter(timestamp) AND device = '$Device'
)
SELECT
$__timeGroup(time,'$INTERVAL') AS time,
MAX(value) AS value,
'Interval Max 15-min Average' AS metric
FROM t
GROUP BY 1 ORDER BY 1;
不相关,但 $__timeFilter
和 $__timeGroup
是什么?它们的语义很清楚,但它们来自哪里?顺便说一句,您可能会发现 this 函数很有用。