好日子所有,多個MAX()在同一個查詢
我: -
SELECT MAX(register_number) AS "Next-6-months-register-number"
FROM gypsum WHERE test_frequency ='6'
我想在同一個query.Is這可能test_frequency ='3'
和'12'
返回值是多少? 謝謝GWS。
好日子所有,多個MAX()在同一個查詢
我: -
SELECT MAX(register_number) AS "Next-6-months-register-number"
FROM gypsum WHERE test_frequency ='6'
我想在同一個query.Is這可能test_frequency ='3'
和'12'
返回值是多少? 謝謝GWS。
是的,這是可能的。使用GROUP BY
以獲得最大的價值爲每個組:
SELECT test_frequency, MAX(register_number) AS "Max-register-number"
FROM gypsum
WHERE test_frequency IN ('3', '6', '12')
GROUP BY test_frequency
SELECT
MAX(CASE WHEN test_frequency = '3' THEN register_number END)
AS "Next-3-months-register-number",
MAX(CASE WHEN test_frequency = '12' THEN register_number END)
AS "Next-12-months-register-number",
MAX(CASE WHEN test_frequency = '6' THEN register_number END)
AS "Next-6-months-register-number",
MAX(register_number) AS "Next-6-months-register-number"
FROM gypsum
謝謝Mamoud這個工作,但返回4列與12重複 – user1731603
@ user1731603刪除'test_frequency'和'GROUP BY test_frequency'。看到我的編輯 –
感謝馬哈茂德,做到了。 – user1731603
試試這個
SELECT MAX(CASE
WHEN test_frequency ='6' THEN register_number
ELSE NULL END) AS "Next-6-months-register-number" ,
MAX(CASE
WHEN test_frequency ='3' THEN register_number
ELSE NULL END) AS "Next-3-months-register-number" ,
MAX(CASE
WHEN test_frequency ='12' THEN register_number
ELSE NULL END) AS "Next-12-months-register-number"
FROM gypsum
謝謝rs我無法得到這個工作有一個錯誤:錯誤代碼:1064 你有你的SQL語法錯誤;檢查對應於您的MySQL服務器版本的手冊,以便在')AS「Next-6-months-register-number」附近使用正確的語法, MAX(CASE WHEN te'at line 3 – user1731603
Thanks但我仍然得到錯誤代碼:1064 您的SQL語法錯誤;請查看與您的MySQL服務器版本相對應的手冊,以查看'MAX(CASE WHEN test_frequency ='3'THEN register_number 'at第4行 – user1731603
現在檢查,查詢時在列之間缺少一個逗號 –
爲了獲得最大價值的所有test_frequency
SELECT test_frequency ,MAX(register_number) AS "Next-6-months-register-number"
FROM gypsum
group by test_frequency
OR
SELECT MAX(register_number) AS "Next-6-months-register-number"
FROM gypsum WHERE test_frequency in('6','3','12')
group by test_frequency
感謝您的回答阿南德 – user1731603
您應該可以只添加或關鍵字的額外參數。
SELECT MAX(register_number) AS "Next-6-months-register-number" FROM gypsum
WHERE test_frequency ='6'
or
test_frequency ='3'
or
test_frequency ='12'
感謝您的回答Stephen – user1731603
在這裏,你從查詢添加您的測試頻率。( '3','12)..或選擇,如:
SELECT test_frequency, MAX(register_number) AS "Max-register-number"
FROM gypsum
WHERE test_frequency IN ('3','12')
GROUP BY test_frequency
SELECT test_frequency, MAX(register_number) AS "Max-register-number"
FROM gypsum
WHERE test_frequency IN (select frequency_colname from tablename where colname=value)
GROUP BY test_frequency
謝謝你標記這個工作的對待 – user1731603