2

I have a single time-series table with some data, but the time-stamp is occasionally off. To check the timestamp, we have been manually defining sunrise and sunset to get an estimate of solar noon using the PAR (Photosynthetically Active Radiation) values - PAR tells how much light is coming in - to determine daybreak and nightfall, then extrapolate solar noon at the midpoint between the two. We decided to declare that the record where PAR rises above 1.9, usually about 7AM, is daybreak, and the record where PAR is equal to or less than 1.9 is considered nightfall.

I've selected the PAR values above 1.9, and then find the min and max time values for each day using a GROUP BY, and this works:

SELECT datePart, MIN(timePart) AS sunrise, MAX(timePart) as sunset, TIMEDIFF(MAX(timePart), MIN(timePart)) AS dayHours FROM Imnavait101119_110225 WHERE PAR_2029410_uE >1.9 GROUP BY datePart

I get this (yes, it's correct, it's an arctic site).

    datePart    sunrise sunset          dayHours    dayHoursHalf
    07/09/2010  0:07:00 23:52:00    23:45:00    NULL
    07/10/2010  0:07:00 23:52:00    23:45:00    NULL
    07/11/2010  0:07:00 23:22:00    23:15:00    NULL
    07/12/2010  1:37:00 23:52:00    22:15:00    NULL
    07/13/2010  0:07:00 23:52:00    23:45:00    NULL
    07/14/2010  0:07:00 23:52:00    23:45:00    NULL
    07/15/2010  0:07:00 23:52:00    23:45:00    NULL
    07/16/2010  0:07:00 23:52:00    23:45:00    NULL
    07/17/2010  0:07:00 23:52:00    23:45:00    NULL
    07/18/2010  0:07:00 23:52:00    23:45:00    NULL
    07/19/2010  0:22:00 23:52:00    23:30:00    NULL
    07/20/2010  0:07:00 23:52:00    23:45:00    NULL
    07/21/2010  0:07:00 23:52:00    23:45:00    NULL

Now that I have the TIMEDIFF value, I want to divide this value by 2 to add to sunrise to get solar noon. I created a column to hold the value, but can't seem to figure out how to get it. I've been able to divide by 2 or multiply by 0.50, but get very weird results, usually along these lines

SELECT  @dayHoursHalf := (dayHours * 0.50) as dayHoursHalf 

FROM Imnavait101119_110225_TimestampCheck

and I get this:

dayHoursHalf
117250
117250
115750
110750
117250
117250
117250
117250
117250
117250
116500
117250
117250
117250

I don't even know where to start - these numbers make no sense to me, and I can't find anything about how to code a query for a fractional time value.

Many thanks to all responders.

4

3 回答 3

2

编辑:我错误地计算了“中午”而不是一半dayHours,这个查询会做你真正想要的:)

UPDATE Imnavait101119_110225_TimestampCheck 
   SET dayHoursHalf = SEC_TO_TIME(TIME_TO_SEC(dayHours)/2);

一个用于测试的 SQLfiddle


原来的:

这个查询改为计算“中午”的时间,正好在日出和日落之间;

UPDATE Imnavait101119_110225_TimestampCheck 
  SET dayHoursHalf = SEC_TO_TIME(TIME_TO_SEC(`sunrise`)  + 
                                 TIME_TO_SEC(dayHours)/2);

一个用于测试的 SQLfiddle

(请注意,SQLfiddle 出于某种原因显示时间值的虚拟日期:))

于 2013-08-03T09:14:12.997 回答
1

如果理解正确,您可以这样做来选择您的值

SELECT q.*, 
       ADDTIME(sunrise, SEC_TO_TIME(TIME_TO_SEC(dayHours) / 2)) dayHoursHalf 
FROM
(
  SELECT datePart, 
         MIN(timePart) sunrise, 
         MAX(timePart) sunset, 
         TIMEDIFF(MAX(timePart), MIN(timePart)) dayHours 
    FROM Imnavait101119_110225 
   WHERE PAR_2029410_uE > 1.9 
   GROUP BY datePart
) q

样本输出:

+------------+----------+----------+---------+--- ------------+
| 日期部分 | 日出| 高分辨率照片| CLIPARTO 日落 | 天小时 | dayHoursHalf |
+------------+----------+----------+---------+--- ------------+
| 2010-07-09 | 00:07:00 | 23:52:00 | 23:45:00 | 11:59:30.0000 |
| 2010-07-10 | 00:07:00 | 23:52:00 | 23:45:00 | 11:59:30.0000 |
| 2010-07-11 | 00:07:00 | 23:22:00 | 23:15:00 | 11:44:30.0000 |
| 2010-07-12 | 01:37:00 | 23:52:00 | 22:15:00 | 12:44:30.0000 |
...

这是SQLFiddle演示


为了dayHoursHalf用各自的值更新列

UPDATE Table1
   SET dayHoursHalf = ADDTIME(sunrise, SEC_TO_TIME(TIME_TO_SEC(dayHours) / 2))

这是SQLFiddle演示

于 2013-08-03T09:10:54.093 回答
1

好吧,11.7250 x 2 = 23.45,这就是你的“白天时间”的值。如果我做对了,你需要将时间除以 2,所以首先我认为最好用 substr() 你的值得到 23 和 45。比你能得到正确的时间。

于 2013-08-03T09:04:53.003 回答