本文介绍了涌入:对不同组中的相同字段进行数学运算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用以下模式"设置了InfluxDB测量:

I have InfluxDB measurement currently set up with following "schema":

+----+-------------+-----------+
| ts | cost(field) | type(tag) |
+----+-------------+-----------+
|  1 | 10          | 'a'       |
|  1 | 20          | 'b'       |
|  2 | 12          | 'a'       |
|  2 | 18          | 'b'       |
|  2 | 22          | 'c'       |
+------------------+-----------+

我正在尝试编写一个查询,该查询将按时间戳对表进行分组,并获得两个不同标签的字段值之间的差值.如果我想获得标签"a"和标签"b"之间的差异,它将得到以下结果(请不要忽略标签"c"):

I am trying to write a query that will group my table by timestamp and get a delta between field values of two different tags. If I want to get delta between tag 'a' and tag 'b', it will give me following result (please not that I ignore tag 'c'):

+----+-----------+------------+
| ts | type(tag) | delta_cost |
+----+-----------+------------+
|  1 | 'a'       | 10         |
|  2 | 'b'       |  6         |
+----+-----------+------------+

是Influx可以做的事情,还是我使用了错误的工具?

Is it something Influx can do or am I using the wrong tool?

推荐答案

只是设法回答了我自己的问题.尽管显而易见的方法之一是执行自联接,但Influx不再支持联接.但是,我们可以使用以下格式的嵌套选择:

Just managed to answer my own question. While one of the obvious ways would be performing self-join, Influx does not support joins anymore. We can, however, use nested selects in a following format:

SELECT MEAN(cost_a) - MEAN(cost_b) as delta_cost
FROM
    (SELECT cost as cost_a, tag, tablename where tag='a'),
    (SELECT cost as cost_b, tag, tablename where tag='b')
GROUP BY time(60s)

由于我还是每60秒获取一次数据,并且我保证每60秒每个标签仅获得1分,因此我可以使用GROUP BY并轻松获取MEAN

Since I am getting my data every 60 seconds anyway, and I have a guarantee of just one point per tag per 60 seconds, I can use GROUP BY and take MEAN without any problems

这篇关于涌入:对不同组中的相同字段进行数学运算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 23:45