所以我研究bigquery,这个查询是一个scheluded查询,所以它第一次创建一个新表,然后附加到表中。因此,每次验证条件时,此查询都会添加1。这个查询每小时执行一次,所以每小时我都保留上一整小时的数据。
所以你可能会问我想要什么,所以很简单,我想保留这个计数器的最后一个值,从这个值执行这个脚本,并为每一行附加到它。
这是我的实际代码:
SELECT
Date,
sum(
CASE
WHEN B > 1 THEN ---Vérification si le capteur est fiable
CASE
WHEN A>1 THEN ---Cas numéro 1
CASE
WHEN A < (B * (1 + 0.05)) AND A > (B * (1 - 0.05)) THEN 1
END
WHEN C>1 THEN ---Cas numéro 2
CASE
WHEN E>1 THEN ---Si la donnée est fiable
CASE
WHEN (C+E) < (B * (1 + 0.05)) AND (C+E) > (B * (1 - 0.05)) THEN 1
END
ELSE --- Si elle ne l'est pas
CASE
WHEN D >= 0 AND F>= 0 AND G>=0 THEN ---On vérifie que toutes ces données sont >= 0
CASE
WHEN (C+(D+F+G)) < (B * (1 + 0.05)) AND (C+(D+F+G)) > (B * (1 - 0.05)) THEN 1
END
ELSE ---Si elles ne le sont pas
CASE
WHEN (C+0) < (B * (1 + 0.05)) AND (C+0) > (B * (1 - 0.05)) THEN 1
END
END
END
WHEN E>1 THEN ---Cas numéro 3
CASE
WHEN D>=0 AND G >= 0 AND F>=0 THEN ---On vérifie que toutes ces données sont >= 0
CASE
WHEN (E + (G+D+F)) < (B * (1 + 0.05)) AND (E + (G+D+F)) > (B * (1 - 0.05)) THEN 1
END
ELSE
CASE
WHEN (E + 0) < (B * (1 + 0.05)) AND (E + 0) > (B * (1 - 0.05)) THEN 1
END
END
END
END) over(over by Date) AS Cpt,
FROM
my_table
---WHERE DATETIME_DIFF(CAST("2020-05-11 21:00:00" AS DATETIME),DATETIME(Date),HOUR)<=1 AND DATETIME_DIFF(CAST("2020-05-11 21:00:00" AS DATETIME),DATETIME(Date),SECOND) > 1
我想要最后一张这样的table
First Hour :
+---------+---------+
| columnA | counter |
+---------+---------+
| 1 | 1 | <= Match condition
+---------+---------+
| 2 | 2 | <= Match condition
+---------+---------+
| 3 | 2 | <= Don't Match condition
+---------+---------+
| 4 | 3 | <= Match condition
+---------+---------+
Second hour :
+---------+---------+
| columnA | counter |
+---------+---------+
| 1 | 1 | <= Match condition
+---------+---------+
| 2 | 2 | <= Match condition
+---------+---------+
| 3 | 2 | <= Don't Match condition
+---------+---------+
| 4 | 3 | <= Match condition
+---------+---------+
| 5 | 4 | <= Match condition, the second iteration begin here, want to keep the last value of the counter and add frome this one
+---------+---------+
| 6 | 5 | <= Match condition
+---------+---------+
| 7 | 6 | <= Match condition
+---------+---------+
| 8 | 6 | <= Don't Match condition
+---------+---------+
编辑:示例数据:
FIRST INPUT TABLE :
+-------------------------+--------+------+--------+---+-----+---+-----+
| Date | A | B | C | D | E | F | G |
+-------------------------+--------+------+--------+---+-----+---+-----+
| 2020-05-11 00:00:00 UTC | 37.696 | 38.0 | 35.358 | 0 | 1.5 | 0 | 1.5 |
+-------------------------+--------+------+--------+---+-----+---+-----+
| 2020-05-11 00:01:00 UTC | 38.257 | 38.0 | 35.935 | 0 | 1.5 | 0 | 1.5 |
+-------------------------+--------+------+--------+---+-----+---+-----+
| 2020-05-11 00:02:00 UTC | 38.571 | 38.0 | 36.242 | 0 | 1.5 | 0 | 1.5 |
+-------------------------+--------+------+--------+---+-----+---+-----+
| 2020-05-11 00:03:00 UTC | 38.01 | 38.0 | 35.687 | 0 | 1.5 | 0 | 1.5 |
+-------------------------+--------+------+--------+---+-----+---+-----+
(1 HOUR LATER) SECOND INPUT :
+-------------------------+--------+------+--------+---+-----+---+-----+
| Date | A | B | C | D | E | F | G |
+-------------------------+--------+------+--------+---+-----+---+-----+
| 2020-05-11 01:00:00 UTC | 40.297 | 38.0 | 37.97 | 0 | 1.5 | 0 | 1.5 |
+-------------------------+--------+------+--------+---+-----+---+-----+
| 2020-05-11 01:01:00 UTC | 40.297 | 38.0 | 37.97 | 0 | 1.5 | 0 | 1.5 |
+-------------------------+--------+------+--------+---+-----+---+-----+
| 2020-05-11 01:02:00 UTC | 40.297 | 38.0 | 37.97 | 0 | 1.5 | 0 | 1.5 |
+-------------------------+--------+------+--------+---+-----+---+-----+
| 2020-05-11 01:03:00 UTC | 40.297 | 38.0 | 36.881 | 0 | 1.5 | 0 | 1.5 |
+-------------------------+--------+------+--------+---+-----+---+-----+
第二次编辑:我找到了我的案例的解决方案,我刚刚把我所有的算法放入了一个,在我做了一个选择之后,我做了我以前计数器的最大值和实际值之和。
暂无答案!
目前还没有任何答案,快来回答吧!