我有一个sql server数据集,如下所示
+-------+---------------------+----------+---------+--+
| RowID | SensorTime | SensorId | Reading | |
+=======+=====================+==========+=========+==+
| 1 | 6/22/2020 0:00:06 | S1 | 5000 | |
+-------+---------------------+----------+---------+--+
| 2 | 6/22/2020 0:00:12 | S1 | 5001 | |
+-------+---------------------+----------+---------+--+
| 3 | 6/22/2020 0:00:19 | S1 | 5001 | |
+-------+---------------------+----------+---------+--+
| 4 | 6/22/2020 0:00:25 | S1 | 5000 | |
+-------+---------------------+----------+---------+--+
| 5 | 6/22/2020 0:06:54 | S1 | 5401 | |
+-------+---------------------+----------+---------+--+
| 6 | 6/22/2020 0:07:00 | S1 | 5401 | |
+-------+---------------------+----------+---------+--+
| 7 | 6/22/2020 0:00:19 | S4 | 5001 | |
+-------+---------------------+----------+---------+--+
| 8 | 6/22/2020 0:00:25 | S4 | 5000 | |
+-------+---------------------+----------+---------+--+
| 9 | 6/22/2020 0:00:36 | S2 | 5842 | |
+-------+---------------------+----------+---------+--+
| 10 | 6/22/2020 0:00:42 | S2 | 5137 | |
+-------+---------------------+----------+---------+--+
| 11 | 6/22/2020 0:00:49 | S2 | 5402 | |
+-------+---------------------+----------+---------+--+
| 12 | 6/22/2020 0:17:32 | S2 | 5001 | |
+-------+---------------------+----------+---------+--+
| 13 | 6/22/2020 0:17:39 | S2 | 5000 | |
+-------+---------------------+----------+---------+--+
| 14 | 6/22/2020 0:17:45 | S2 | 5001 | |
+-------+---------------------+----------+---------+--+
| 15 | 6/22/2020 0:00:49 | S2 | 5402 | |
+-------+---------------------+----------+---------+--+
| 16 | 6/22/2020 0:00:06 | S3 | 5000 | |
+-------+---------------------+----------+---------+--+
| 17 | 6/22/2020 0:00:12 | S3 | 5000 | |
+-------+---------------------+----------+---------+--+
| 18 | 6/22/2020 0:00:18 | S3 | 5000 | |
+-------+---------------------+----------+---------+--+
| 19 | 6/22/2020 0:04:53 | S3 | 5347 | |
+-------+---------------------+----------+---------+--+
| 20 | 6/22/2020 0:04:59 | S3 | 5566 | |
+-------+---------------------+----------+---------+--+
这是一组传感器读数,每个读数都有一个时间戳。
基于阅读随时间的变化,我想创建事件的开始和结束时间的基础上阅读时间
所需输出:
+-------+----------+---------------------+---------------------+--+
| Event | SensorId | Start Event | End Event | |
+=======+==========+=====================+=====================+==+
| 1 | S1 | 6/22/2020 0:00:06 | 6/22/2020 0:00:25 | |
+-------+----------+---------------------+---------------------+--+
| 2 | S1 | 6/22/2020 0:06:54 | 6/22/2020 0:07:00 | |
+-------+----------+---------------------+---------------------+--+
| 3 | S4 | 6/22/2020 0:00:19 | 6/22/2020 0:00:25 | |
+-------+----------+---------------------+---------------------+--+
| 4 | S2 | 6/22/2020 0:00:36 | 6/22/2020 0:00:49 | |
+-------+----------+---------------------+---------------------+--+
| 5 | S2 | 6/22/2020 0:17:32 | 6/22/2020 0:17:45 | |
+-------+----------+---------------------+---------------------+--+
| 6 | S2 | 6/22/2020 0:00:49 | 6/22/2020 0:00:49 | |
+-------+----------+---------------------+---------------------+--+
| 7 | S3 | 6/22/2020 0:00:06 | 6/22/2020 0:00:18 | |
+-------+----------+---------------------+---------------------+--+
| 8 | S3 | 6/22/2020 0:04:53 | 6/22/2020 0:04:59 | |
+-------+----------+---------------------+---------------------+--+
到目前为止,我尝试使用滞后函数来确定以前的传感器读数:
Select
*,
LAG(EVENTType, 1) OVER (
ORDER BY
RowID
) PrevEvent
from
(
Select
RowID,
SensorTime,
SensorId,
Param,
Reading,
CASE when Reading BETWEEN 500
AND 501 THEN 1 ELSE 0 END AS EVENTType
from
SensorReadings
)
光标可以用于此吗?
3条答案
按热度按时间jq6vz3qz1#
下面的解决方案有点不寻常,但您可以尝试作为游标的替代方案(当然,您可以使用游标解决此问题)
此处演示
bxgwgixi2#
你似乎想定义“相邻”的读数,这些读数之间有一个间隙。差距究竟是什么还不清楚,但1分钟似乎对你的例子有用。
你可以使用
lag()
确定以前的时间戳并确定新分组的开始位置。然后,开始的累积和就是聚合所需的:如果您还需要活动编号:
toe950273#
为什么我们需要光标?我们不需要光标。
演示。