检查值是否超过5次并标记它们

时间:2017-12-21 08:23:13

标签: mysql sql

今天早上我真的受阻了......

如何检查值是否连续重复连续超过5次并标记它们?
我的意思是,如果它们是否定的则很容易标记它们,但是如何检查它们是否出现超过5次?

更新:对不起,我不清楚......

DATA:

+-------+-----------------+
| time  | V_preisereadDOT | 
+-------+-----------------+
| 00:00 |               5 |
| 01:00 |               1 |
| 02:00 |               2 |
| 03:00 |              -3 |
| 04:00 |              -8 |
| 05:00 |              -9 |
| 06:00 |               5 |
| 07:00 |               4 |
| 08:00 |              -9 |
| 09:00 |              -9 |
| 10:00 |              -5 |
| 11:00 |              -5 |
| 12:00 |              -4 |
| 13:00 |              -4 |
| 14:00 |               6 |
| 15:00 |               5 |
| 16:00 |               2 |
| 17:00 |               4 |
| 18:00 |              -4 |
| 19:00 |              -8 |
| 20:00 |              -9 |
| 21:00 |              -7 |
| 22:00 |               2 |
| 23:00 |               8 |
+-------+-----------------+

结果得到:

+-------+-----------------+----------+
| time  | V_preisereadDOT | 6negativ |
+-------+-----------------+----------+
| 00:00 |               5 |        0 |
| 01:00 |               1 |        0 |
| 02:00 |               2 |        0 |
| 03:00 |              -3 |        0 |
| 04:00 |              -8 |        0 |
| 05:00 |              -9 |        0 |
| 06:00 |               5 |        0 |
| 07:00 |               4 |        0 |
| 08:00 |              -9 |        1 |
| 09:00 |              -9 |        1 |
| 10:00 |              -5 |        1 |
| 11:00 |              -5 |        1 |
| 12:00 |              -4 |        1 |
| 13:00 |              -4 |        1 |
| 14:00 |               6 |        0 |
| 15:00 |               5 |        0 |
| 16:00 |               2 |        0 |
| 17:00 |               4 |        0 |
| 18:00 |              -4 |        0 |
| 19:00 |              -8 |        0 |
| 20:00 |              -9 |        0 |
| 21:00 |              -7 |        0 |
| 22:00 |               2 |        0 |
| 23:00 |               8 |        0 |
+-------+-----------------+----------+

提前谢谢!

1 个答案:

答案 0 :(得分:1)

如下:

SELECT
  reading,
  COUNT(reading)
FROM data
WHERE negative = 1
GROUP BY reading
HAVING COUNT(reading) > 5