MySQL查询,用于计算某些数字的连续出现次数
还有一个问题,但我想以前没人问过 我有一张像这样的桌子MySQL查询,用于计算某些数字的连续出现次数,mysql,Mysql,还有一个问题,但我想以前没人问过 我有一张像这样的桌子 +----+------+---------------------+------+-----------+--------------+ | id | unit | datetime | idle | idlecount | boutduration | +----+------+---------------------+------+-----------+--------------+ | 1 | A
+----+------+---------------------+------+-----------+--------------+
| id | unit | datetime | idle | idlecount | boutduration |
+----+------+---------------------+------+-----------+--------------+
| 1 | A | 2009-12-04 08:10:05 | 139 | | |
| 2 | A | 2009-12-04 08:20:05 | 107 | | |
| 3 | A | 2009-12-04 08:30:05 | 0 | | |
| 4 | A | 2009-12-04 08:40:05 | 144 | | |
| 5 | A | 2009-12-04 08:50:05 | 0 | | |
| 6 | A | 2009-12-04 09:00:05 | 0 | | |
| 7 | A | 2009-12-04 09:10:05 | 58 | | |
| 8 | A | 2009-12-04 09:20:05 | 0 | | |
| 9 | A | 2009-12-04 09:30:05 | 0 | | |
| 10 | A | 2009-12-04 09:40:05 | 0 | | |
| 11 | A | 2009-12-04 09:50:05 | 0 | | |
| 12 | A | 2009-12-04 10:00:05 | 107 | | |
| 13 | A | 2009-12-04 10:10:05 | 0 | | |
| 14 | A | 2009-12-04 10:20:05 | 144 | | |
| etc...
+----+------+---------------------+------+-----------+--------------+
| id | unit | datetime | idle | idlecount | boutduration |
+----+------+---------------------+------+-----------+--------------+
| 1 | A | 2009-12-04 08:10:05 | 139 | | |
| 2 | A | 2009-12-04 08:20:05 | 107 | | |
| 3 | A | 2009-12-04 08:30:05 | 0 | | |
| 4 | A | 2009-12-04 08:40:05 | 144 | | |
| 5 | A | 2009-12-04 08:50:05 | 0 | 2 | 00:20:00 |
| 6 | A | 2009-12-04 09:00:05 | 0 | | |
| 7 | A | 2009-12-04 09:10:05 | 58 | | |
| 8 | A | 2009-12-04 09:20:05 | 0 | 4 | 00:40:00 |
| 9 | A | 2009-12-04 09:30:05 | 0 | | |
| 10 | A | 2009-12-04 09:40:05 | 0 | | |
| 11 | A | 2009-12-04 09:50:05 | 0 | | |
| 12 | A | 2009-12-04 10:00:05 | 107 | | |
| 13 | A | 2009-12-04 10:10:05 | 0 | | |
| 14 | A | 2009-12-04 10:20:05 | 144 | | |
| etc...
我需要计算列idle中连续出现的零的数量,并确定每个序列的持续时间。单一事件是不相关的。所以结果应该是这样的
+----+------+---------------------+------+-----------+--------------+
| id | unit | datetime | idle | idlecount | boutduration |
+----+------+---------------------+------+-----------+--------------+
| 1 | A | 2009-12-04 08:10:05 | 139 | | |
| 2 | A | 2009-12-04 08:20:05 | 107 | | |
| 3 | A | 2009-12-04 08:30:05 | 0 | | |
| 4 | A | 2009-12-04 08:40:05 | 144 | | |
| 5 | A | 2009-12-04 08:50:05 | 0 | | |
| 6 | A | 2009-12-04 09:00:05 | 0 | | |
| 7 | A | 2009-12-04 09:10:05 | 58 | | |
| 8 | A | 2009-12-04 09:20:05 | 0 | | |
| 9 | A | 2009-12-04 09:30:05 | 0 | | |
| 10 | A | 2009-12-04 09:40:05 | 0 | | |
| 11 | A | 2009-12-04 09:50:05 | 0 | | |
| 12 | A | 2009-12-04 10:00:05 | 107 | | |
| 13 | A | 2009-12-04 10:10:05 | 0 | | |
| 14 | A | 2009-12-04 10:20:05 | 144 | | |
| etc...
+----+------+---------------------+------+-----------+--------------+
| id | unit | datetime | idle | idlecount | boutduration |
+----+------+---------------------+------+-----------+--------------+
| 1 | A | 2009-12-04 08:10:05 | 139 | | |
| 2 | A | 2009-12-04 08:20:05 | 107 | | |
| 3 | A | 2009-12-04 08:30:05 | 0 | | |
| 4 | A | 2009-12-04 08:40:05 | 144 | | |
| 5 | A | 2009-12-04 08:50:05 | 0 | 2 | 00:20:00 |
| 6 | A | 2009-12-04 09:00:05 | 0 | | |
| 7 | A | 2009-12-04 09:10:05 | 58 | | |
| 8 | A | 2009-12-04 09:20:05 | 0 | 4 | 00:40:00 |
| 9 | A | 2009-12-04 09:30:05 | 0 | | |
| 10 | A | 2009-12-04 09:40:05 | 0 | | |
| 11 | A | 2009-12-04 09:50:05 | 0 | | |
| 12 | A | 2009-12-04 10:00:05 | 107 | | |
| 13 | A | 2009-12-04 10:10:05 | 0 | | |
| 14 | A | 2009-12-04 10:20:05 | 144 | | |
| etc...
我认为这需要使用,但我以前从未使用过,并且对MySQL文档感到有点害怕。尽管如此,我还是梦想着一个单一的查询解决方案
干杯,Tom你可以通过相关子查询来实现这一点。第一个获取每个之后的下一个非零值。第二个计算中间的值。以下是获取空闲计数的示例:
select t.*,
(select (case when count(*) > 1 then count(*) end)
from t t3
where t3.id >= t.id and
(t3.id < t.nextNonZeroIdle or nextNonZeroIdle is NULL) and
t3.idle = 0
) as IdleCOunt
from (select t.id, t.unit, t.datetime, t.idle,
(select id
from t t2
where t2.unit = t.unit and
t2.id > t.id and
t2.idle > 0
order by id
limit 1
) as nextNonZeroIdle
from t
) t
from t;
哇,我还在想你的答案。但我有一种预感,这个查询可能需要几个小时才能遍历我的表。它大约有170万行;