与MYSQL: Find Start and End Timestamp of a consecutive count的问题类似,我有一张包含类似数据的表格:
StatusTime | Reading 2014-01-01 00:00 | 255 2014-01-01 01:00 | 255 2014-01-01 02:00 | 255 2014-01-01 03:00 | 255 2014-01-01 04:00 | 255 2014-01-01 05:00 | 241 2014-01-01 06:00 | 189 2014-01-01 07:00 | 100 2014-01-01 08:00 | 20 2014-01-01 09:00 | 0 2014-01-01 10:00 | 1 2014-01-01 11:00 | 1 2014-01-01 12:00 | 0 2014-01-01 13:00 | 21 2014-01-01 14:00 | 1 2014-01-01 15:00 | 0 2014-01-01 16:00 | 12 2014-01-01 17:00 | 63 2014-01-01 18:00 | 102 2014-01-01 19:00 | 198 2014-01-01 20:00 | 255 2014-01-01 21:00 | 255 2014-01-01 22:00 | 255 2014-01-01 23:00 | 255 2014-01-02 00:00 | 255 2014-01-02 01:00 | 255 2014-01-02 02:00 | 255 2014-01-02 03:00 | 255 2014-01-02 04:00 | 255 2014-01-02 05:00 | 208 2014-01-02 06:00 | 100 2014-01-02 07:00 | 48 2014-01-02 08:00 | 0 2014-01-02 09:00 | 0
我想提取开始和结束时间戳,其中读数高于50 且低于50 , 分别。我只是无法理解其他答案中提出的SQL!
提前感谢您的帮助。
答案 0 :(得分:4)
如果它每天只再次低于/高于此值,则可以使查询变得非常简单;只需找到它所在的最小和最大时间,按日期分组。
SELECT
DATE(statustime) statusdate,
MIN(CASE WHEN reading<50 THEN statustime ELSE NULL END) start_time,
MAX(CASE WHEN reading<50 THEN statustime ELSE NULL END) end_time
FROM myTable
GROUP BY statusdate
答案 1 :(得分:0)
这样的东西?
SELECT DISTINCT MAX(StatusTime), MIN(StatusTime)
FROM table_name
WHERE Reading < 50 OR Reading > 50;
答案 2 :(得分:0)
试用此代码:
select *
(select max(StatusTime) from tbl where Reading > 50) above_50,
(select min(StatusTime) from tbl where Reading < 50) bellow_50;