首页 > 解决方案 > MySQL 将一个查询组合成另一个以用于 Grafana

问题描述

我正在运行一个 Openhab2 实例并收集数据以及数据收集中重要更改的时间戳。在我的表 item45 中,我存储 2 列时间和价值女巫看起来像这样。

表 (item45) 设计 截图 1 截图 2

Time (datetime,primary) Value (datetime)
...                     ...
2018-10-17 03:08:30     2018-10-17 03:08:30
2018-10-19 00:13:13     2018-10-19 00:13:13
2018-10-19 00:27:58     2018-10-19 00:27:57

它是 Openhab 如何存储数据的一种设计,所以我对此无能为力。我现在尝试在 Grafana 中使用这些值,就像这样:

成功使用示例

我的问题在于 Grafana 用于绘制查询。如果下一个较旧的点在视口之外,它会断开图形的连接。我试图通过虚拟填补查询中数据的空白来解决这个问题。像这样:

当前使用的查询

SELECT 
1 as value,
'Net Reset' as metric,
UNIX_TIMESTAMP(v.gen_date) AS time_sec
from 
  (select DATE_SUB( FROM_UNIXTIME(1539707286), INTERVAL t3*1000 + t2*100 + t1*10 + t0  HOUR) gen_date from
              (select 0 t0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0
   CROSS JOIN (select 0 t1 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1
   CROSS JOIN (select 0 t2 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2
   CROSS JOIN (select 0 t3 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3
   ) v
LEFT JOIN item45
    ON DATE_FORMAT( Time, '%Y-%m-%dT%H:00:00') = v.gen_date
where $__timeFilter(v.gen_date)
ORDER BY v.gen_date DESC;

正如你可以看到我在这里使用给定的“黑客”之一来生成每小时的填充数据,只是设置为一个。

我需要帮助来实现这个查询:

查询替换 FROM_UNIXTIME(...)

SELECT * FROM item45 ORDER BY Time DESC LIMIT 1

进入查询 1 where FROM_UNIXTIME(1539707286) 设置填充未来数据的实际停止。

我正在运行 MySQL57-server,目前无法升级到版本 8 以使用 with 子句。

期望的输出

Time                    Value
2018-10-18 21:00:00     1 <- Inserted
2018-10-18 22:00:00     1 <- Inserted
2018-10-18 23:00:00     1 <- Inserted
2018-10-19 00:00:00     1 <- Inserted
2018-10-19 00:13:13     1
2018-10-19 00:27:58     1

标签: mysqlgrafanaopenhab

解决方案


推荐阅读