2014-07-19 58 views
0

尋找此查詢的一種方法,用於顯示當天在相鄰列的同一行中最高溫度發生的實際DATETIME 。相鄰列中同一行中的MIN MAX的每日DATETIME

我一直在尋找並嘗試一些事情,但我有點迷失了連接,我認爲你需要爲此使用它。

當前查詢和結果..

SELECT 
    YEAR(tstLocal) AS "YEAR", 
    MONTHNAME(tstLocal) AS "MONTH", 
    DAY(tstLocal) AS "DAY", 
    min(tempC) AS "DAILY MIN TEMP", 
    max(tempC) AS "DAILY MAX TEMP", 
    ROUND(AVG(tempC),1) AS "DAILY AVG TEMP" 
FROM 
    house.outside1 
WHERE 
    YEAR(tstLocal) = '2014' AND MONTH(tstLocal) = '7' 
GROUP BY 
    YEAR(tstLocal), 
    MONTH(tstLocal), 
    DAY(tstLocal); 

RESULTS

什麼我需要做的是,這些最小/最大溫度發生在DATETIME顯示呢?

+0

你爲什麼要分手了日期時間(到年,月,日),而不是用它直接在查詢? – serakfalcon

回答

2

因爲你已經使用了group by,該group_concat()/substring_index()伎倆將工作:

SELECT YEAR(tstLocal) AS "YEAR", MONTHNAME(tstLocal) AS "MONTH", DAY(tstLocal) AS "DAY", 
     min(tempC) AS "DAILY MIN TEMP", max(tempC) AS "DAILY MAX TEMP", 
     ROUND(AVG(tempC),1) AS "DAILY AVG TEMP", 
     substring_index(group_concat(tstlocal order by tempC asc), ',', 1) as min_datetime, 
     substring_index(group_concat(tstlocal order by tempC desc), ',', 1) as max_datetime 
FROM house.outside1 
WHERE YEAR(tstLocal) = '2014' AND MONTH(tstLocal) = '7' 
GROUP BY YEAR(tstLocal), MONTH(tstLocal), DAY(tstLocal); 
+0

非常好,+1。就像在舊時代沒有使用子選詞:-) – VMai

+0

美麗,非常好。 Thx。 –

相關問題