MySQL: Get start & end timestamp for each day

后端 未结 3 660
无人共我
无人共我 2021-01-25 18:05

Similar to the question at MYSQL: Find Start and End Timestamp of a consecutive count, I have a table with similar data:

StatusTime          | Reading   
2014-01-01 0         


        
3条回答
  •  -上瘾入骨i
    2021-01-25 18:59

    If it only goes below/above again once per day, you can make the query quite simple; just find the min and max time where it's below, grouping by date.

    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
    

    An SQLfiddle to test with.

提交回复
热议问题