2013-05-06 100 views
-1

我有一個表格,其值在以下列中:低,中,高和月份。 我想得到以下結果每月的最大值。它會抓住每個月每列的最大值。每月分組最大值

這裏是我的發言

SELECT MAX(low) as Low,MAX(medium) as Medium,MAX(high) as High,month as mth FROM tablename WHERE month > 0; 

期望的結果將是

month 1: Low 2 Medium 3 High 4

month 2: Low 4 Medium 2 High 5

month 3: Low 6 Medium 3 High 6

+0

使用GROUP BY。我在這裏錯過了什麼嗎? – PinnyM 2013-05-06 15:03:37

+0

手冊:['GROUP BY'(Aggregate)Functions](http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html) – Kermit 2013-05-06 15:05:21

+0

另外,爲什麼你不存儲月作爲'日期'類型? – Kermit 2013-05-06 15:05:55

回答

1
SELECT MAX(low) as Low,MAX(medium) as Medium,MAX(high) as High,month as mth 
FROM tablename 
WHERE month > 0 
GROUP BY month 
ORDER BY month;