Mysql query to get trend of temperature

一曲冷凌霜 提交于 2019-12-08 13:22:23

问题


I have a very large table where I have temperatures getting logged every 1 mins, what I would like to query is a trend; something like a percentage increase or percentage decrease per selected period ( hour or 15mins; depending on the query)

my table looks (example) like the following

ID      time                temp
119950  2013-03-27 07:56:05 27.25
119951  2013-03-27 07:57:05 27.50
119952  2013-03-27 07:58:05 27.60
119953  2013-03-27 07:59:05 27.80
119954  2013-03-27 08:00:05 27.70
119955  2013-03-27 08:01:05 27.50
119956  2013-03-27 08:02:05 27.25
119957  2013-03-27 08:03:05 27.10
119958  2013-03-27 08:04:05 26.9
119959  2013-03-27 08:05:05 27.1
119960  2013-03-27 08:06:05 27.25
119961  2013-03-27 08:07:05 27.6

I believe a trend can be calculated as follow (as per link), but correct me if you have a better way; take the difference between each row then add then up and divide by count. so for the table above we get

Diff
+0.25
+0.10
+0.20
-0.10
-0.20
-0.25
-0.15
-0.20
+0.20
+0.15
+0.35

The trend per minute for last 11 minutes is sum of diff/11. which gives 0.063C per minute for last 11minutes.

Can someone please help me get percentage trend per hour for last 3 hours. and trend per minute for 1 hour?


回答1:


CREATE TABLE temperature_log
(ID      INT NOT NULL,dt DATETIME NOT NULL, temperature DECIMAL(5,2) NOT NULL);

INSERT INTO temperature_log VALUES
(119950  ,'2013-03-27 07:56:05',27.25),
(119951  ,'2013-03-27 07:57:05', 27.50),
(119952  ,'2013-03-27 07:58:05', 27.60),
(119953  ,'2013-03-27 07:59:05', 27.80),
(119954  ,'2013-03-27 08:00:05', 27.70),
(119955  ,'2013-03-27 08:01:05', 27.50),
(119956  ,'2013-03-27 08:02:05', 27.25),
(119957  ,'2013-03-27 08:03:05', 27.10),
(119958  ,'2013-03-27 08:04:05', 26.9),
(119959  ,'2013-03-27 08:05:05', 27.1),
(119960  ,'2013-03-27 08:06:05', 27.25),
(119961  ,'2013-03-27 08:07:05', 27.6);

SELECT x.*
     , x.temperature - y.temperature diff
     , COUNT(*) cnt
     ,(x.temperature-y.temperature)/COUNT(*) trend 
  FROM temperature_log x 
  JOIN temperature_log y 
    ON y.id < x.id 
 GROUP 
    BY x.id;
+--------+---------------------+-------------+-------+-----+-----------+
| ID     | dt                  | temperature | diff  | cnt | trend     |
+--------+---------------------+-------------+-------+-----+-----------+
| 119951 | 2013-03-27 07:57:05 |       27.50 |  0.25 |   1 |  0.250000 |
| 119952 | 2013-03-27 07:58:05 |       27.60 |  0.35 |   2 |  0.175000 |
| 119953 | 2013-03-27 07:59:05 |       27.80 |  0.55 |   3 |  0.183333 |
| 119954 | 2013-03-27 08:00:05 |       27.70 |  0.45 |   4 |  0.112500 |
| 119955 | 2013-03-27 08:01:05 |       27.50 |  0.25 |   5 |  0.050000 |
| 119956 | 2013-03-27 08:02:05 |       27.25 |  0.00 |   6 |  0.000000 |
| 119957 | 2013-03-27 08:03:05 |       27.10 | -0.15 |   7 | -0.021429 |
| 119958 | 2013-03-27 08:04:05 |       26.90 | -0.35 |   8 | -0.043750 |
| 119959 | 2013-03-27 08:05:05 |       27.10 | -0.15 |   9 | -0.016667 |
| 119960 | 2013-03-27 08:06:05 |       27.25 |  0.00 |  10 |  0.000000 |
| 119961 | 2013-03-27 08:07:05 |       27.60 |  0.35 |  11 |  0.031818 |
+--------+---------------------+-------------+-------+-----+-----------+

Incidentally, if you're interested in getting average results per hour, you could do something like this...

SELECT DATE_FORMAT(x.dt,'%Y-%m-%d %h:00:00')
     , AVG(x.temperature) avg_temp
  FROM temperature_log x 
 GROUP 
    BY DATE_FORMAT(x.dt,'%Y-%m-%d %h:00:00');


来源:https://stackoverflow.com/questions/15762585/mysql-query-to-get-trend-of-temperature

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!