2013-04-05 53 views
0

我有3個非常大的表,每分鐘都有值被記錄下來, 下面是這些表的摘錄mysql query在1個表中加入3個查詢時的平均值

我想獲得每小時平均爲期這些表的第1天並且相對於投身其中時,請注意,以pH值和溫度

日誌時間之間的幾秒鐘的差距表PH(僅提取,該表是非常大的超過13萬倍的值)

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 

表溫度1(僅提取,該表是非常大的超過13萬倍的值)

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 

表固體(僅提取物,該表是非常大的超過13萬倍的值)

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 

我目前使用下面的查詢得到小時平均值

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'); 

我怎樣纔能有效地加入(相對於時間)上面的查詢結果爲每個傳感器(x3)顯示在1表

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

低於此查詢獲取的小時值,但不知道如何tweek它得到每小時平均

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 
+0

沒有必要做這個date_format東西的分組。 'GROUP BY DATE(x.time)'將會完成同樣的事情,而不會強制mysql做一堆反覆的字符串格式化,而這些字符串格式化將會被扔掉。 – 2013-04-05 16:57:23

+0

您是否需要每小時或每天(或兩者)的平均值?或者您是否需要每小時的平均值,然後每小時平均值的平均值? – Barranka 2013-04-05 16:59:04

+0

我只需要每小時的平均值,但只限於顯示1天的平均值 – Ossama 2013-04-05 17:01:16

回答

0

Hour()看來,這將是一個有用的功能,因爲你只能看一天。或許,這樣的事情會爲你工作:

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; 

是它使用內部連接,我正在做的假設,總是會有每個表的時間至少一個記錄,但它似乎像一個合理的假設。

+0

謝謝你,你的表工作,但由於我將推動結果表到圖表功能,時間需要以日期時間格式顯示,此外,查詢表上的時間從0開始,並在23結束,如果當前時間例如上午5點,我希望查詢能夠在昨天早上5點到凌晨5點,而不是凌晨5點到23點。如果你明白我的意思。 – Ossama 2013-04-05 18:30:17

+0

我這樣做,它應該在昨天早上6點 - 今天凌晨5點相當有用。昨天凌晨5點和今天凌晨5點重疊將是一個問題。如果是我,我會調整WHERE子句來處理整個小時。如果我正在分析數據,並提供了23組小時數據,預訂了兩個半小時,我肯定會丟掉半小時。至於格式化的時間,只需從任何子查詢中抽取MIN(時間)或某物,然後您就可以參加比賽了。 – femtoRgon 2013-04-05 18:46:34

+0

是一個noob,任何人都可以提供幫助,我特別感興趣的是時間和它的順序 – Ossama 2013-04-05 18:51:38