从表格中选择最小值(以及最大值和平均值)以及事件的时间戳

时间:2019-04-24 10:49:15

标签: mysql mariadb grafana

我有几个这样的表:

const X

EE是电表的计数(以kWh为单位),PO是电表的功率(以kW为单位),每15分钟从电表读取一次数据并将其写入数据库。

我正在寻找带有标记“ PO”的所有值的最大值,这就是为什么要排除所有“ EE”标记的原因。我想要返回最高值的代码,它是根据带有TAG“ PO”的时间戳

我打算使用该代码扩展具有最大,最小和AVG值及其相应时间戳的grafana仪表板(已经存在)。

这是我到目前为止的代码,但是它似乎总是选择我在WHERE子句中输入的时间范围内的第一个值:

> "dsid"    "tag"   "timestamp" "value"  
> "989" "EE"    "1445162347"    "931.647"
> "990" "PO"    "1445162347"    "0.087" 
> "991" "EE"    "1445163247"    "931.669"
> "992" "PO"    "1445163247"    "0.089"
> "993" "EE"    "1445164147"    "931.692"
> "994" "PO"    "1445164147"    "0.085"
> "995" "EE"    "1445165047"    "931.715"
> "996" "PO"    "1445165047"    "0.090"

当前我得到以下输出:

'''SELECT
    FROM_UNIXTIME(timestamp) as time_sec,
    '1P2' AS Meter,
    tag as metric,
    MIN(value) AS MIN,
    MAX(VALUE) AS MAX,
    AVG(VALUE) AS AVG
  FROM `WSE_KA-KHW_1P2`
  WHERE TAG NOT IN ('EE')
'''

我要的是这样的

"time_sec"  "Meter" "metric"    "MIN"   "MAX" "AVG"
"2015-10-13 08:14:07"   "1P2"   "PO"    "0.000000"  "6.404" "3.2"

根据要求,下面是插入脚本代码的某些行:

"time_sec"  "Meter" "metric"    "MIN"   "MAX"  "AVG"
"2015-10-13 08:14:07"   "1P2"   "PO"    "0.1"   ""  ""
"2015-12-13 10:14:07"   "1P2"   "PO"    ""  "6.404"  ""
""  "1P2"   "PO"    ""  ""  "3.2"

值后的其他列不相关...

THX到@symcbean我现在已经很接近我需要实现的目标:

INSERT INTO `WSE_KA-KHW_1P2` (`dsid`, `tag`, `timestamp`, `value`, `startts`, `correctts`, `isfixed`) VALUES (909, 'EE', 1445126346, '930.647', 1445125500, 1445126400, 0);
INSERT INTO `WSE_KA-KHW_1P2` (`dsid`, `tag`, `timestamp`, `value`, `startts`, `correctts`, `isfixed`) VALUES (910, 'PO', 1445126346, '0.088', 1445125500, 1445126400, 0);
INSERT INTO `WSE_KA-KHW_1P2` (`dsid`, `tag`, `timestamp`, `value`, `startts`, `correctts`, `isfixed`) VALUES (911, 'EE', 1445127246, '930.669', 1445126400, 1445127300, 0);
INSERT INTO `WSE_KA-KHW_1P2` (`dsid`, `tag`, `timestamp`, `value`, `startts`, `correctts`, `isfixed`) VALUES (912, 'PO', 1445127246, '0.087', 1445126400, 1445127300, 0);
INSERT INTO `WSE_KA-KHW_1P2` (`dsid`, `tag`, `timestamp`, `value`, `startts`, `correctts`, `isfixed`) VALUES (913, 'EE', 1445128146, '930.691', 1445127300, 1445128200, 0);
INSERT INTO `WSE_KA-KHW_1P2` (`dsid`, `tag`, `timestamp`, `value`, `startts`, `correctts`, `isfixed`) VALUES (914, 'PO', 1445128146, '0.087', 1445127300, 1445128200, 0);
INSERT INTO `WSE_KA-KHW_1P2` (`dsid`, `tag`, `timestamp`, `value`, `startts`, `correctts`, `isfixed`) VALUES (915, 'EE', 1445129046, '930.718', 1445128200, 1445129100, 0);
INSERT INTO `WSE_KA-KHW_1P2` (`dsid`, `tag`, `timestamp`, `value`, `startts`, `correctts`, `isfixed`) VALUES (916, 'PO', 1445129046, '0.138', 1445128200, 1445129100, 0);
INSERT INTO `WSE_KA-KHW_1P2` (`dsid`, `tag`, `timestamp`, `value`, `startts`, `correctts`, `isfixed`) VALUES (917, 'EE', 1445129947, '930.752', 1445129100, 1445130000, 0);
INSERT INTO `WSE_KA-KHW_1P2` (`dsid`, `tag`, `timestamp`, `value`, `startts`, `correctts`, `isfixed`) VALUES (918, 'PO', 1445129947, '0.134', 1445129100, 1445130000, 0);
INSERT INTO `WSE_KA-KHW_1P2` (`dsid`, `tag`, `timestamp`, `value`, `startts`, `correctts`, `isfixed`) VALUES (919, 'EE', 1445130847, '930.782', 1445130000, 1445130900, 0);
INSERT INTO `WSE_KA-KHW_1P2` (`dsid`, `tag`, `timestamp`, `value`, `startts`, `correctts`, `isfixed`) VALUES (920, 'PO', 1445130847, '0.088', 1445130000, 1445130900, 0);
INSERT INTO `WSE_KA-KHW_1P2` (`dsid`, `tag`, `timestamp`, `value`, `startts`, `correctts`, `isfixed`) VALUES (921, 'EE', 1445131747, '930.804', 1445130900, 1445131800, 0);
INSERT INTO `WSE_KA-KHW_1P2` (`dsid`, `tag`, `timestamp`, `value`, `startts`, `correctts`, `isfixed`) VALUES (922, 'PO', 1445131747, '0.087', 1445130900, 1445131800, 0);
INSERT INTO `WSE_KA-KHW_1P2` (`dsid`, `tag`, `timestamp`, `value`, `startts`, `correctts`, `isfixed`) VALUES (923, 'EE', 1445132647, '930.826', 1445131800, 1445132700, 0);
INSERT INTO `WSE_KA-KHW_1P2` (`dsid`, `tag`, `timestamp`, `value`, `startts`, `correctts`, `isfixed`) VALUES (924, 'PO', 1445132647, '0.086', 1445131800, 1445132700, 0);
INSERT INTO `WSE_KA-KHW_1P2` (`dsid`, `tag`, `timestamp`, `value`, `startts`, `correctts`, `isfixed`) VALUES (925, 'EE', 1445133547, '930.848', 1445132700, 1445133600, 0);
INSERT INTO `WSE_KA-KHW_1P2` (`dsid`, `tag`, `timestamp`, `value`, `startts`, `correctts`, `isfixed`) VALUES (926, 'PO', 1445133547, '0.086', 1445132700, 1445133600, 0);
INSERT INTO `WSE_KA-KHW_1P2` (`dsid`, `tag`, `timestamp`, `value`, `startts`, `correctts`, `isfixed`) VALUES (927, 'EE', 1445134447, '930.870', 1445133600, 1445134500, 0);
INSERT INTO `WSE_KA-KHW_1P2` (`dsid`, `tag`, `timestamp`, `value`, `startts`, `correctts`, `isfixed`) VALUES (928, 'PO', 1445134447, '0.087', 1445133600, 1445134500, 0);

我已经使用了grafana中的三个表,它有一个漂亮的表,但是它总是选择数据库中的第一个时间值作为时间戳。但是我需要它从1P2_MAX输出时间戳。这样我就会知道MAX到底是什么时候出现的...

当前输出如下:

SELECT records.*, aggregated.*
 FROM
 (
 SELECT
 from_unixtime(TIMESTAMP),
 tag AS metric,
 MAX(VALUE) AS 1P2_MAX,
 AVG(VALUE) AS 1P2_AVG
 FROM `WSE_KA-KHW_1P2`
 WHERE TAG NOT IN ('EE')
 AND from_unixtime(TIMESTAMP BETWEEN FROM_UNIXTIME(1546297200) AND FROM_UNIXTIME(1577833199))
 ) records JOIN
 (
 SELECT
 tag AS metric,
 MAX(VALUE) AS 1P2_MAX,
 AVG(VALUE) AS 1P2_AVG
 FROM `WSE_KA-KHW_1P2`
 WHERE TAG NOT IN ('EE')
 AND from_unixtime(TIMESTAMP BETWEEN FROM_UNIXTIME(1546297200) AND FROM_UNIXTIME(1577833199))
 GROUP BY metric
 ) aggregated ON
 records.metric=aggregated.metric

1 个答案:

答案 0 :(得分:0)

您需要GROUP BY FROM_UNIXTIME(timestamp), tag

(取决于DBMS的配置方式,您可能需要在分组的属性中包括文字)。

这是一个相当琐碎的SQL问题-如此之多,以至于它可以在这里键入答案而不是搜索重复项。

尽管这回答了您提出的问题-看起来您在问错了问题。尽管示例数据中的多行具有相同的标记和时间戳,但这似乎有些奇怪。我怀疑如果您真的对汇总值感兴趣,您可能想四舍五入时间值以获得更大的bin。

根据以下评论进行更新

如果您希望聚合值不与特定时间匹配。...

SELECT records.*, aggregated.*
FROM
(
  SELECT *
  FROM `WSE_KA-KHW_1P2`
  WHERE TAG NOT IN ('EE')
  AND timestamp BETWEEN _____ AND _____
) records JOIN
(
  SELECT
  tag AS metric,
  MIN(value) AS minval,
  MAX(VALUE) AS maxval,
  AVG(VALUE) AS avgval
  FROM `WSE_KA-KHW_1P2`
  WHERE TAG NOT IN ('EE')
  AND timestamp BETWEEN _____ AND _____
  GROUP BY tag
) aggregated ON
records.tag=aggregated.tag

但这与您在随后的问题编辑中要求的有所不同:

  

我想要返回最大值的代码,并且该代码具有TAG“ PO”的时间戳

这是一个新要求。并且在样本数据中,PO为“ NOT IN(EE)”时,不一定要遵循此表达式是相符的形式。抛开这些,您似乎正在要求“逐组最大值”-您可以使用max concat tricksub-query

来解决