Daily light integral from 20s light intensity MySQL records

quanglewangle :

I record light intensity at 20s intervals as a number of photons. I need to integrate the values over the day. Daily light integral assumes that light intensity is measured every second. Simply doing a SUM() over the day's records and multiplying the result by 20 would work if the records were exactly 20s apart, which unfortunately they are not.

I need to multiply each reading by the difference in time between the record and the previous one.

I would prefer to do this in SQL rather than pulling records one at a time programmatically.

Ideas welcome. [edit] Just looked at some real data and the approximation made by assuming the intervals are exactly 20s is less that assuming that sampling every 20s can stand in for sampling every second. So I withdraw the question

Bernd Buffen :

Here is a little sample with a table with 3 columns (id, timestamp, cnt)

then you can calculate it like this:

SELECT p.id, p.mytime, p.photons, l.photons
,TIMESTAMPDIFF(SECOND, l.mytime, p.mytime) diffsecs
,p.photons * COALESCE(TIMESTAMPDIFF(SECOND, l.mytime, p.mytime),1) as cnt
FROM photons p
LEFT JOIN photons l ON p.id -1 = l.id
ORDER BY p.id;

SELECT SUM( p.photons * COALESCE(TIMESTAMPDIFF(SECOND, l.mytime, p.mytime),1)) as cnt
FROM photons p
LEFT JOIN photons l ON p.id -1 = l.id
ORDER BY p.id;

sample

mysql> describe photons;
+---------+------------------+------+-----+---------+----------------+
| Field   | Type             | Null | Key | Default | Extra          |
+---------+------------------+------+-----+---------+----------------+
| id      | int(11) unsigned | NO   | PRI | NULL    | auto_increment |
| mytime  | timestamp        | YES  | MUL | NULL    |                |
| photons | int(11)          | YES  |     | NULL    |                |
+---------+------------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

mysql> select * from photons;
+----+---------------------+---------+
| id | mytime              | photons |
+----+---------------------+---------+
|  1 | 2020-02-26 12:00:00 |     100 |
|  2 | 2020-02-26 12:00:25 |      80 |
|  3 | 2020-02-26 12:01:00 |     100 |
|  4 | 2020-02-26 12:05:00 |     200 |
|  5 | 2020-02-26 12:05:03 |      22 |
+----+---------------------+---------+
5 rows in set (0.00 sec)

mysql> SELECT p.id, p.mytime, p.photons, l.photons
    -> ,TIMESTAMPDIFF(SECOND, l.mytime, p.mytime) diffsecs
    -> ,p.photons * COALESCE(TIMESTAMPDIFF(SECOND, l.mytime, p.mytime),1) as cnt
    -> FROM photons p
    -> LEFT JOIN photons l ON p.id -1 = l.id
    -> ORDER BY p.id;
+----+---------------------+---------+---------+----------+-------+
| id | mytime              | photons | photons | diffsecs | cnt   |
+----+---------------------+---------+---------+----------+-------+
|  1 | 2020-02-26 12:00:00 |     100 |    NULL |     NULL |   100 |
|  2 | 2020-02-26 12:00:25 |      80 |     100 |       25 |  2000 |
|  3 | 2020-02-26 12:01:00 |     100 |      80 |       35 |  3500 |
|  4 | 2020-02-26 12:05:00 |     200 |     100 |      240 | 48000 |
|  5 | 2020-02-26 12:05:03 |      22 |     200 |        3 |    66 |
+----+---------------------+---------+---------+----------+-------+
5 rows in set (0.00 sec)

mysql> SELECT SUM( p.photons * COALESCE(TIMESTAMPDIFF(SECOND, l.mytime, p.mytime),1)) as cnt
    -> FROM photons p
    -> LEFT JOIN photons l ON p.id -1 = l.id
    -> ORDER BY p.id;
+-------+
| cnt   |
+-------+
| 53666 |
+-------+
1 row in set (0.00 sec)

mysql> 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=14029&siteId=1