您如何找到属于不同时间戳的两个数据点的字段键的差异?

时间:2018-10-30 18:53:39

标签: influxdb

我有以下数据:

INSERT table1,adwords_id=123-456-7890 total_spending=0 1538377201000000000
INSERT table1,adwords_id=123-456-7890 total_spending=110 1538463601000000000
INSERT table1,adwords_id=123-456-7890 total_spending=120 1538550001000000000

我想编写一个查询来查找两个时间戳之间的total_spending差异。

例如,假设我要查找1538377201000000000 + 1h1538550001000000000 + 1h之间的total_spending差异

一行一行地做:

v1 = SELECT last(total_spending) FROM table1 WHERE "adwords_id" = '123-456-7890' AND time < 1538377201000000000 + 1h

v2 = SELECT last(total_spending) FROM table1 WHERE "adwords_id" = '123-456-7890' AND time < 1538550001000000000 + 1h

答案将为v2-v1

如何在一个查询中执行此操作? (所以我可以在许多adwords_id 中运行它)

2 个答案:

答案 0 :(得分:0)

以下内容应该适合您。

您需要具有@ t1,@ t2和@adwordsId的变量值。

SELECT
(
    SELECT TOP 1 total_spending
    FROM Table1
    WHERE adwords_id = @adwordsId
    AND time < @t2
    ORDER BY time DESC
) - 
(
    SELECT TOP 1 total_spending
    FROM Table1
    WHERE adwords_id = @adwordsId
    AND time < @t1
    ORDER BY time DESC
)

答案 1 :(得分:0)

假设每个adwords_id的total_spending是累积的,我会尝试这样做:

SELECT last("total_spending") - first("total_spending") FROM Table1 WHERE time >= t1 AND time <= t2 GROUP BY "adwords_id"