简体   繁体   中英

Finding consecutive days

I am trying to put together a PHP script that calculates various climatological parameters using a MySQL db with variables. I was able to get most of them done except this:

So what I want, is to find the longest "dry" period, ie consecutive days where rain was 0. And then, similar thing, the time of the year when there was for the first time at least 5 consecutive days when T was below 0.

The MySQL table looks like this:

It has records for each 5 minutes and for the rain, it logs daily cumulative sum, so to get the daily rain I would use simply max(R)

Table structure (simplified) columns: DateTime, T, R

And then the rows would be for example:

2015-05-01 15:00  13    0
2015-05-01 15:05  13.2  0

In the above example, the times are obvious and then the column represents the current temperature and current daily cumulative rain.

My goal now is to find a command that will find the most consecutive days where the daily max(R) is 0 (ie there was no rain) and then, the date, where for the first time, on five consecutive days, the T was at least once during the day below 0 (ie min(T)<0).

My only idea right now is to do this using some nested cycles in PHP, but loading it into arrays and then cycling would be quite time and resources demanding and so if it was possible to do with MySQL, would be great.

Any help is much appreciated, thanks.

Well I tried first to group the data by YEAR(DateTime),MONTH(DateTime),DAY(DateTime) and then put a condition WHERE max(R)>0. This basically gives me a list of days with some rain. But I do not know how to now select only the period, where the days are consecutive, if that is possible at all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM