根据时间戳计算并更新当前行和最后一行之间的功耗

时间:2019-06-28 19:12:37

标签: mysql

不幸的是,我的SQL技能非常基础。我希望有一个人可以帮助我。我想根据同一设备两行之间的时间差来计算Wh(Watt / h)的功耗(计数器读数)。

我有一个表(mysql),其中具有deviceID,Date,CounterReading(Wh)和当前为空,ActivePower(Watt)和其他一些列。在表“ device_data”中,大约设备每分钟会在当前行中插入新行(字段activePower)。时间戳记为现场日期。我希望每小时运行一个脚本,该脚本计算计数器读数(Wh)并更新每一行。需要将计算出的Wh添加到表中保存的最后一个值中。

我知道公式,但是我没有技巧来编写适当的sql查询来计算计数器读数并更新表。

  

公式:Wh ='以秒为单位的timediff'* activePower / 3600   例如:55s * 9.1601 / 3600 = 0.139945972 Wh(。之后的四位数应足够)。

计算得出的Wh必须添加到counterReading的最后一个条目中。目前,CounterReading字段为“ NULL”,我想从0 Wh开始并将其汇总到今天。

有时ActivePower也可以为0(=电源插头后面的设备无电源使用)。在这种情况下,CounterReading值必须与最后一个条目相同。

下面是我的带有两个设备的表“ device_data”的示例。

+--------------+---------------------+---------+--------+
|   DeviceId   |        DATE         | Counter | Active |
|              |                     | Reading | POWER  |
+--------------+---------------------+---------+--------+
| B4E62DE92CE9 | 2019-06-28 17:34:02 | NULL    | 9.1150 |
| B4E62DE92CE9 | 2019-06-28 17:35:01 | NULL    | 9.2129 |
| B4E62DE92CE9 | 2019-06-28 17:36:01 | NULL    | 9.1962 |
| B4E62DE92CE9 | 2019-06-28 17:37:01 | NULL    | 9.1741 |
| B4E62DE92CE9 | 2019-06-28 17:38:02 | NULL    | 9.1123 |
| B4E62DE92CE9 | 2019-06-28 17:39:02 | NULL    | 9.1256 |
| B4E62DE92CE9 | 2019-06-28 17:40:01 | NULL    | 9.2432 |
| B4E62DE92CE9 | 2019-06-28 17:41:02 | NULL    | 9.1924 |
| B4E62DE92CE9 | 2019-06-28 17:42:02 | NULL    | 9.2486 |
| B4E62DE92CE9 | 2019-06-28 17:43:01 | NULL    | 9.2040 |
| B4E62DE92CE9 | 2019-06-28 17:44:01 | NULL    | 9.0565 |
| B4E62DE92CE9 | 2019-06-28 17:45:01 | NULL    | 9.1879 |
| B4E62DE92CE9 | 2019-06-28 17:46:01 | NULL    | 9.2045 |
| B4E62DE92CE9 | 2019-06-28 17:47:02 | NULL    | 9.1986 |
| B4E62DE92CE9 | 2019-06-28 17:48:04 | NULL    | 9.1601 |
+--------------+---------------------+---------+--------+

以下是我希望获得的最终结果的示例:

+--------------+-------------------+----------------+-------------+
|   DeviceId   |       DATE        | CounterReading | ActivePower |
+--------------+-------------------+----------------+-------------+
| B4E62DE92CE9 | 28.06.19 17:48:04 |         0.0000 |      9.1601 |
| B4E62DE92CE9 | 28.06.19 17:47:02 |         0.1559 |      9.1986 |
| B4E62DE92CE9 | 28.06.19 17:46:01 |         0.3093 |      9.2045 |
| B4E62DE92CE9 | 28.06.19 17:45:01 |         0.4599 |      9.1879 |
| B4E62DE92CE9 | 28.06.19 17:44:01 |         0.6083 |      9.0565 |
| B4E62DE92CE9 | 28.06.19 17:43:01 |         0.7591 |       9.204 |
| B4E62DE92CE9 | 28.06.19 17:42:02 |         0.9081 |      9.2486 |
| B4E62DE92CE9 | 28.06.19 17:41:02 |         1.0588 |      9.1924 |
| B4E62DE92CE9 | 28.06.19 17:40:01 |         1.2128 |      9.2432 |
| B4E62DE92CE9 | 28.06.19 17:39:02 |         1.3599 |      9.1256 |
| B4E62DE92CE9 | 28.06.19 17:38:02 |         1.5092 |      9.1123 |
| B4E62DE92CE9 | 28.06.19 17:37:01 |         1.6621 |      9.1741 |
| B4E62DE92CE9 | 28.06.19 17:36:01 |         1.8128 |      9.1962 |
| B4E62DE92CE9 | 28.06.19 17:35:01 |         1.9638 |      9.2129 |
| B4E62DE92CE9 | 28.06.19 17:34:02 |         2.1107 |       9.115 |
+--------------+-------------------+----------------+-------------+

如果有人可以帮助我,那将很棒。

亲切的问候 帕特里克

2 个答案:

答案 0 :(得分:0)

您可以使用LAG窗口函数来检索上一行。

以下SQL计算两行之间的时间差:

SELECT date - LAG(date) OVER (ORDER BY date) FROM device_data;

有了这个,您应该能够创建一个执行您所需的SQL语句。

编辑:

要汇总这些值,可以使用CTE:

WITH t AS (
     SELECT date, (UNIX_TIMESTAMP(date) - LAG(UNIX_TIMESTAMP(date)) OVER (ORDER BY date ASC)) * activepower / 3600 AS Wh FROM device_data
) SELECT IFNULL(SUM(t.Wh) OVER (ORDER BY t.date), 0) FROM t;

答案 1 :(得分:0)

我现在能够计算瓦特/小时,因为我必须先将日期转换为UNIX_TIMESTAMP。我现在正在SQL中努力加总计数器读数始终为:last value of counterRading + Wh

这是我到目前为止所拥有的SQL:

SELECT date,
       deviceID,
       activePower,
       counterReading,
       (UNIX_TIMESTAMP(date) - LAG(UNIX_TIMESTAMP(date)) OVER (ORDER BY date ASC)) * activePower / 3600 AS Wh
    FROM device_data
    WHERE deviceID = 'B4E62DE92E05';

我尝试过类似的

SELECT ...
    counterReading + (UNIX_TIMESTAMP(date) - LAG(UNIX_TIMESTAMP(date)) OVER (ORDER BY date ASC)) * activePower / 3600 AS Wh
    FROM ...

但是那根本没有用。这就是我得到的结果。

+---------------------+--------------+-------------+----------------+------------+
| date                | deviceID     | activePower | counterReading | Wh         |
+---------------------+--------------+-------------+----------------+------------+
| 2019-06-28 15:00:02 | B4E62DE92E05 |     80.2636 |           NULL |       NULL |
| 2019-06-28 15:01:01 | B4E62DE92E05 |     79.6895 |           NULL | 1.30602236 |
| 2019-06-28 15:02:01 | B4E62DE92E05 |     78.8532 |           NULL | 1.31422000 |
| 2019-06-28 15:03:01 | B4E62DE92E05 |     78.1088 |           NULL | 1.30181333 |
| 2019-06-28 15:04:01 | B4E62DE92E05 |     77.5467 |           NULL | 1.29244500 |
| 2019-06-28 15:05:01 | B4E62DE92E05 |     76.2649 |           NULL | 1.27108167 |
| 2019-06-28 15:06:01 | B4E62DE92E05 |     76.3540 |           NULL | 1.27256667 |
| 2019-06-28 15:07:01 | B4E62DE92E05 |     74.6706 |           NULL | 1.24451000 |
+---------------------+--------------+-------------+----------------+------------+