我正在尝试编写一个 sql 查询来计算 Postgresql 中的 RSI(相对强度指数)并在 grafana 中实现。
我在表中只有 3 列 -
time_stamp bigint,
metric_name text,
value.max int
RSI 是关于 value.max 用于在 grafana 中作为时间序列查询进行绘图
RSI 参考:https ://school.stockcharts.com/doku.php?id=technical_indicators:relative_strength_index_rsi
我使用的 RSI 查询如下:
SELECT
$__unixEpochGroup(timestamp/1000, '1m') AS "time",
value.max - lag(value.max) OVER (ORDER BY $__unixEpochGroup(timestamp/1000, '1m')) AS "diff",
case when diff > 0 then diff else 0 end as "Gain",
case when diff < 0 then diff else 0 end as "Loss",
avg(Gain) OVER (ORDER BY $__unixEpochGroup(timestamp/1000, '1m') ROWS 20 PRECEDING) as "avg_gain",
avg(Loss) OVER (ORDER BY $__unixEpochGroup(timestamp/1000, '1m') ROWS 20 PRECEDING) AS "avg_loss"
FROM spectrum_schema.my_table
WHERE
$__unixEpochFilter(time) and
metric_name = 'CPUUtilization'
ORDER BY 1
这给出了错误**
窗口函数调用不能嵌套
任何人都可以帮助构建 RSI sql 查询吗?
时间序列数据绘图的正常查询工作如下:
SELECT
$__unixEpochGroup(timestamp/1000, '1m') AS "time",
value.max AS "value.max"
FROM spectrum_schema.my_table
WHERE
$__unixEpochFilter(time) and
metric_name = 'NetworkThroughput' and
ORDER BY 1
上面的工作查询也可以调整吗?