mysql query to join 3 queries in 1 table while averaging

拥有回忆 提交于 2020-01-16 19:37:06

问题


I have 3 very large tables, with values getting logged every minute, below is an extract of these tables.

I would like to get hourly averages for a period of 1 Day of these tables and join them with respect to time, please note there is a couple of seconds gap between log time for ph and temperature

Table PH (extract only, this table is very large with more than 130,000 values)

    ID      time                Ph

    72176   2013-04-06 03:29:34 7.58
    72177   2013-04-06 03:30:34 7.58
    72178   2013-04-06 03:31:34 7.54
    72179   2013-04-06 03:32:34 7.58
    72180   2013-04-06 03:33:34 7.58
    72181   2013-04-06 03:34:34 7.58
    72182   2013-04-06 03:35:34 7.54
    72183   2013-04-06 03:36:34 7.58
    72184   2013-04-06 03:37:34 7.54
    72185   2013-04-06 03:38:34 7.58
    72186   2013-04-06 03:39:34 7.58

Table temperature1 (extract only, this table is very large with more than 130,000 values)

    ID      time            temperature

133312  2013-04-06 03:29:36 25.37
133313  2013-04-06 03:30:36 25.37
133314  2013-04-06 03:31:36 25.37
133315  2013-04-06 03:32:36 25.31
133316  2013-04-06 03:33:36 25.31
133317  2013-04-06 03:34:36 25.31
133318  2013-04-06 03:35:36 25.37
133319  2013-04-06 03:36:36 25.31
133320  2013-04-06 03:37:36 25.31
133321  2013-04-06 03:38:36 25.31
133322  2013-04-06 03:39:36 25.37

Table solids (extract only, this table is very large with more than 130,000 values)

    ID      time            solids

123791  2013-04-06 03:29:49 140
123792  2013-04-06 03:30:49 140
123793  2013-04-06 03:31:49 143
123794  2013-04-06 03:32:49 140
123795  2013-04-06 03:33:49 140
123796  2013-04-06 03:34:49 140
123797  2013-04-06 03:35:49 140
123798  2013-04-06 03:36:49 143
123799  2013-04-06 03:37:49 140
123800  2013-04-06 03:38:49 140
123801  2013-04-06 03:39:49 140

I am currently getting hourly averages using the query below

SELECT DATE_FORMAT(x.time,'%Y-%m-%d %H:00:00')
     , avg(x.solids) avg_solids
  FROM solids x where time >= NOW() - INTERVAL 1 DAY
 GROUP 
    BY DATE_FORMAT(x.time,'%Y-%m-%d %H:00:00'); 

how can I efficiently join (with respect to time) the results of the query above for each sensor (x3) to be displayed in 1 table

===============================

this query below gets the hourly values, but not sure how to tweek it to get averages per hour

SELECT DATE_FORMAT(timeTable.minuteTime, '%Y-%m-%d %k:%i') time,
(oT2.temperature) temperature,
(T2.temperature) temp,
(S2.solids) solids,
(P2.Ph) Ph


FROM
(
    SELECT minuteTime.minuteTime minuteTime,
    ( SELECT MAX(time) FROM outside_temperature WHERE time <= minuteTime.minuteTime AND time >= NOW() - INTERVAL 1 DAY) otempTime, 
    ( SELECT MAX(time) FROM temperature1 WHERE time <= minuteTime.minuteTime AND time >= NOW() - INTERVAL 1 DAY) tempTime, 
    ( SELECT MAX(time) FROM Ph WHERE time <= minuteTime.minuteTime AND time >= NOW() - INTERVAL 1 DAY) phTime,  
    ( SELECT MAX(time) FROM solids WHERE time <= minuteTime.minuteTime AND time >= NOW() - INTERVAL 1 DAY) solidsTime

    FROM  
    (
        SELECT DATE(time) + INTERVAL (HOUR(time) DIV 1 *1 ) HOUR minuteTime
        FROM Ph
        WHERE time >= NOW() - INTERVAL 1 DAY AND time <= NOW()
        UNION SELECT DATE(time) + INTERVAL (HOUR(time) DIV 1 *1) HOUR
        FROM solids
        WHERE time >= NOW() - INTERVAL 1 DAY AND time <= NOW()
        UNION SELECT DATE(time) + INTERVAL (HOUR(time) DIV 1 *1) HOUR
        FROM outside_temperature
        WHERE time >= NOW() - INTERVAL 1 DAY AND time <= NOW()
        UNION SELECT DATE(time) + INTERVAL (HOUR(time) DIV 1 *1) HOUR
        FROM temperature1
        WHERE time >= NOW() - INTERVAL 1 DAY AND time <= NOW()
        GROUP BY 1
    ) minuteTime
) timeTable
LEFT JOIN outside_temperature oT2 ON oT2.time = timeTable.otempTime
LEFT JOIN temperature1 T2 ON T2.time = timeTable.tempTime
LEFT JOIN solids S2 ON S2.time = timeTable.solidsTime
LEFT JOIN Ph P2 ON P2.time = timeTable.phTime


GROUP BY DATE_FORMAT(timeTable.minuteTime, '%Y-%m-%d %k:%i') 
ORDER BY minuteTime ASC

回答1:


Hour() seems that it would be a useful function for this, since you are only looking at a single day. Perhaps something like this would work for you:

SELECT * FROM
 (SELECT HOUR(time) hour, avg(ph) AS avg_ph
  FROM ph 
  WHERE time >= NOW() - INTERVAL 1 DAY
  GROUP BY hour) p
JOIN 
 (SELECT HOUR(time) hour, avg(temperature) AS avg_temp
  FROM temperature1 
  WHERE time >= NOW() - INTERVAL 1 DAY
  GROUP BY hour) t ON t.hour = p.hour
JOIN 
 (SELECT HOUR(time) hour, avg(solids) AS avg_solids
  FROM solids 
  WHERE time >= NOW() - INTERVAL 1 DAY
  GROUP BY hour) s ON s.hour = p.hour;

Being that it is using inner joins, I'm making the assumption that there will always be at least one records in each table for the hour, but it seems like a reasonable assumption.



来源:https://stackoverflow.com/questions/15839408/mysql-query-to-join-3-queries-in-1-table-while-averaging

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