我在SQLFiddle上放了一些数据样本,我试图弄清楚如何根据这些数据构建查询:
TimeStr
表的scl1_fill
字段的TIMEDIFF()和下一个sc1_weight
表TimeStr
字段
我认为我需要使用MySQL模拟ROW_NUMBER(),并且由于某种原因,我似乎总是使用变量和子查询来获取行号等。
我尝试过很多东西并且每次都做得很短,所以甚至不确定它是否值得展示我尝试过的东西所以我会提供我所拥有的和可视化/澄清的预期结果。
基本上当sc1_fill
获得1
值时,设备开始填充,当sc1_weight
获得任何值时,这意味着它符合权重设定值并返回一个值点。
获取两者之间的时差将显示从sc1_fill
开始到明确的下一个记录时间所花费的时间sc1_weight
获得任何值。
我想我可以将此值作为sc1_weight
记录的值TIMEDIFF()投放,并有一个新列,所有其他记录显示NULL
并且该工作正常(参见预期结果)。
(始终只获取某个特定日期的数据,例如WHERE TimeStr LIKE '2017-11-14%'
)
| test_tbl | TimeStr | Value |
----------------------------------------------
sc1_setpnt 2017-11-14 00:05:01 650
sc1_disch 2017-11-14 07:10:24 1
sc1_fill 2017-11-14 07:15:56 1
sc1_weight 2017-11-14 07:21:29 651
sc1_disch 2017-11-14 07:26:18 1
sc1_fill 2017-11-14 07:27:32 1
sc1_weight 2017-11-14 07:33:07 650
sc1_disch 2017-11-14 08:18:36 1
sc1_fill 2017-11-14 08:19:53 1
sc1_weight 2017-11-14 08:25:29 651
sc1_disch 2017-11-14 08:30:21 1
sc1_fill 2017-11-14 08:31:39 1
sc1_weight 2017-11-14 08:37:20 650
sc1_disch 2017-11-14 08:46:00 1
sc1_fill 2017-11-14 08:47:17 1
sc1_weight 2017-11-14 08:52:50 649
sc1_disch 2017-11-14 09:03:22 1
sc1_fill 2017-11-14 09:04:36 1
sc1_weight 2017-11-14 09:10:10 651
sc1_disch 2017-11-14 09:18:45 1
sc1_fill 2017-11-14 09:20:35 1
sc1_weight 2017-11-14 09:26:11 650
sc1_disch 2017-11-14 09:36:40 1
sc1_fill 2017-11-14 09:37:59 1
sc1_weight 2017-11-14 09:43:39 650
sc1_disch 2017-11-14 09:46:55 1
sc1_fill 2017-11-14 09:53:49 1
sc1_weight 2017-11-14 09:59:42 650
sc1_disch 2017-11-14 10:36:06 1
sc1_fill 2017-11-14 10:37:27 1
sc1_weight 2017-11-14 10:43:14 650
sc1_disch 2017-11-14 10:50:44 1
sc1_fill 2017-11-14 10:52:01 1
sc1_weight 2017-11-14 10:57:47 650
sc1_disch 2017-11-14 11:05:46 1
sc1_fill 2017-11-14 11:07:19 1
sc1_weight 2017-11-14 11:12:48 650
sc1_disch 2017-11-14 11:25:25 1
sc1_fill 2017-11-14 11:36:18 1
sc1_fill 2017-11-14 11:39:12 1
sc1_fill 2017-11-14 11:45:26 1
sc1_weight 2017-11-14 11:50:39 651
sc1_disch 2017-11-14 12:00:45 1
sc1_fill 2017-11-14 12:04:44 1
sc1_weight 2017-11-14 12:10:16 651
sc1_disch 2017-11-14 12:58:48 1
sc1_fill 2017-11-14 13:00:01 1
sc1_weight 2017-11-14 13:05:23 651
sc1_disch 2017-11-14 13:06:19 1
sc1_fill 2017-11-14 13:10:52 1
sc1_weight 2017-11-14 13:16:19 650
sc1_disch 2017-11-14 13:18:27 1
sc1_fill 2017-11-14 13:24:35 1
sc1_weight 2017-11-14 13:30:01 651
sc1_disch 2017-11-14 13:31:35 1
sc1_fill 2017-11-14 13:35:40 1
sc1_weight 2017-11-14 13:41:05 650
sc1_disch 2017-11-14 13:47:04 1
sc1_fill 2017-11-14 13:50:19 1
sc1_weight 2017-11-14 13:55:43 649
sc1_disch 2017-11-14 14:06:57 1
sc1_fill 2017-11-14 14:08:11 1
sc1_weight 2017-11-14 14:13:32 651
sc1_disch 2017-11-14 14:26:10 1
sc1_fill 2017-11-14 14:27:24 1
sc1_weight 2017-11-14 14:32:49 650
sc1_disch 2017-11-14 14:42:00 1
sc1_fill 2017-11-14 14:43:11 1
sc1_weight 2017-11-14 14:48:25 650
| test_tbl | TimeStr | Value | FillTime |
-------------------------------------------------------------
sc1_setpnt 2017-11-14 00:05:01 650 NULL
sc1_disch 2017-11-14 07:10:24 1 NULL
sc1_fill 2017-11-14 07:15:56 1 NULL
sc1_weight 2017-11-14 07:21:29 651 00:05:33
sc1_disch 2017-11-14 07:26:18 1 NULL
sc1_fill 2017-11-14 07:27:32 1 NULL
sc1_weight 2017-11-14 07:33:07 650 00:05:35
sc1_disch 2017-11-14 08:18:36 1 NULL
sc1_fill 2017-11-14 08:19:53 1 NULL
sc1_weight 2017-11-14 08:25:29 651 00:05:36
sc1_disch 2017-11-14 08:30:21 1 NULL
sc1_fill 2017-11-14 08:31:39 1 NULL
sc1_weight 2017-11-14 08:37:20 650 00:05:41
sc1_disch 2017-11-14 08:46:00 1 NULL
好的,更糟糕的是,有时在sc1_fill
后续值1
之前有多个sc1_weight
值,但我只需要计算最近或最新{集合的{1}}来获取填充时间(例如TimeStr
)
在这种情况下的预期结果
SELECT TIMEDIFF('2017-11-14 11:50:39','2017-11-14 11:45:26')
答案 0 :(得分:1)
您可以使用相关子查询:
select t.*,
(select t2.TimeStr
from Detail t2
where t2.TimeStr < t.TimeStr and
t2.tbl = 'sc1_fill' and t2.value = 1
order by t2.TimeStr desc
limit 1
) as prev_timestamp
from Detail t
where t.tbl = 'sc1_weight';
然后,您可以获得timestamp
和prev_timestamp
的时差。
Here是SQL小提琴。
答案 1 :(得分:1)
虽然您没有请求此摘要输出,但我找到了一种可能有用的方法,结果如下所示
+----+---------+--------+---------------------+---------------------+----------+--------------+
| | filllno | weight | start_fill | end_fill | duration | num_of_fills |
+----+---------+--------+---------------------+---------------------+----------+--------------+
| 1 | 0 | 651 | 14.11.2017 07:15:56 | 14.11.2017 07:21:29 | 00:05:33 | 1 |
| 2 | 1 | 650 | 14.11.2017 07:27:32 | 14.11.2017 07:33:07 | 00:05:35 | 1 |
| 3 | 2 | 651 | 14.11.2017 08:19:53 | 14.11.2017 08:25:29 | 00:05:36 | 1 |
| 4 | 3 | 650 | 14.11.2017 08:31:39 | 14.11.2017 08:37:20 | 00:05:41 | 1 |
| 5 | 4 | 649 | 14.11.2017 08:47:17 | 14.11.2017 08:52:50 | 00:05:33 | 1 |
| 6 | 5 | 651 | 14.11.2017 09:04:36 | 14.11.2017 09:10:10 | 00:05:34 | 1 |
| 7 | 6 | 650 | 14.11.2017 09:20:35 | 14.11.2017 09:26:11 | 00:05:36 | 1 |
| 8 | 7 | 650 | 14.11.2017 09:37:59 | 14.11.2017 09:43:39 | 00:05:40 | 1 |
| 9 | 8 | 650 | 14.11.2017 09:53:49 | 14.11.2017 09:59:42 | 00:05:53 | 1 |
| 10 | 9 | 650 | 14.11.2017 10:37:27 | 14.11.2017 10:43:14 | 00:05:47 | 1 |
| 11 | 10 | 650 | 14.11.2017 10:52:01 | 14.11.2017 10:57:47 | 00:05:46 | 1 |
| 12 | 11 | 650 | 14.11.2017 11:07:19 | 14.11.2017 11:12:48 | 00:05:29 | 1 |
| 13 | 12 | 651 | 14.11.2017 11:45:26 | 14.11.2017 11:50:39 | 00:05:13 | 3 |
| 14 | 13 | 651 | 14.11.2017 12:04:44 | 14.11.2017 12:10:16 | 00:05:32 | 1 |
| 15 | 14 | 651 | 14.11.2017 13:00:01 | 14.11.2017 13:05:23 | 00:05:22 | 1 |
| 16 | 15 | 650 | 14.11.2017 13:10:52 | 14.11.2017 13:16:19 | 00:05:27 | 1 |
| 17 | 16 | 651 | 14.11.2017 13:24:35 | 14.11.2017 13:30:01 | 00:05:26 | 1 |
| 18 | 17 | 650 | 14.11.2017 13:35:40 | 14.11.2017 13:41:05 | 00:05:25 | 1 |
| 19 | 18 | 649 | 14.11.2017 13:50:19 | 14.11.2017 13:55:43 | 00:05:24 | 1 |
| 20 | 19 | 651 | 14.11.2017 14:08:11 | 14.11.2017 14:13:32 | 00:05:21 | 1 |
| 21 | 20 | 650 | 14.11.2017 14:27:24 | 14.11.2017 14:32:49 | 00:05:25 | 1 |
| 22 | 21 | 650 | 14.11.2017 14:43:11 | 14.11.2017 14:48:25 | 00:05:14 | 1 |
+----+---------+--------+---------------------+---------------------+----------+--------------+
select
case when value = 1 then fillno else fillno-2 end filllno
, max(value) weight
, max(case when value = 1 then TimeStr end) start_fill
, max(TimeStr) end_fill
, timediff(max(TimeStr),max(case when value = 1 then TimeStr end)) duration
, count(*) - 1 num_of_fills
from (
SELECT *
, IF(value <> 1, @prev := value, @prev) pweight
, IF(value <> 1, @counter := @counter+1, @counter - 1) fillno
FROM test_tbl
cross join (select @counter :=0, @prev:=0) vars
where tbl in ('sc1_fill','sc1_weight','sc1_setpnt')
order by TimeStr ASC
) d
where tbl in ('sc1_fill','sc1_weight')
group by
case when value = 1 then fillno else fillno-2 end
order by start_fill ASC
;
内部子查询准备行,其中包含将填充与权重对齐的信息(通过&#34; fillno&#34;),因此允许您在上面看到的聚合视图。这些行看起来像这样:
+----+------------+---------------------+-------+--------------+----------+---------+--------+
| | tbl | TimeStr | Value | @counter :=0 | @prev:=0 | pweight | fillno |
+----+------------+---------------------+-------+--------------+----------+---------+--------+
| 1 | sc1_setpnt | 14.11.2017 00:05:01 | 650 | 0 | 0 | 650 | 1 |
| 2 | sc1_fill | 14.11.2017 07:15:56 | 1 | 0 | 0 | 650 | 0 |
| 3 | sc1_weight | 14.11.2017 07:21:29 | 651 | 0 | 0 | 651 | 2 |
| 4 | sc1_fill | 14.11.2017 07:27:32 | 1 | 0 | 0 | 651 | 1 |
| 5 | sc1_weight | 14.11.2017 07:33:07 | 650 | 0 | 0 | 650 | 3 |
| 6 | sc1_fill | 14.11.2017 08:19:53 | 1 | 0 | 0 | 650 | 2 |
| 7 | sc1_weight | 14.11.2017 08:25:29 | 651 | 0 | 0 | 651 | 4 |
| 8 | sc1_fill | 14.11.2017 08:31:39 | 1 | 0 | 0 | 651 | 3 |
| 9 | sc1_weight | 14.11.2017 08:37:20 | 650 | 0 | 0 | 650 | 5 |
| 10 | sc1_fill | 14.11.2017 08:47:17 | 1 | 0 | 0 | 650 | 4 |
| 11 | sc1_weight | 14.11.2017 08:52:50 | 649 | 0 | 0 | 649 | 6 |
| 12 | sc1_fill | 14.11.2017 09:04:36 | 1 | 0 | 0 | 649 | 5 |
+----+------------+---------------------+-------+--------------+----------+---------+--------+
逻辑确实按照要求计算了2个或更多个填充事件,但也对这些事件进行了计数,以便您可以看到任何已发生的事件。