MySQL: Get start & end timestamp for each day

后端 未结 3 659
无人共我
无人共我 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条回答
  • 2021-01-25 18:38

    Try this code:

      select *
        (select max(StatusTime) from tbl where Reading > 50) above_50,
        (select min(StatusTime) from tbl where Reading < 50) bellow_50;
    
    0 讨论(0)
  • 2021-01-25 18:52

    Something like this?

    SELECT DISTINCT MAX(StatusTime), MIN(StatusTime) 
    FROM table_name
    WHERE Reading < 50 OR Reading > 50;
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题