2014-01-12 87 views
0

我有了一個數學計算和聚合函數如下查詢:如何在同一查詢中添加多個聚合函數的結果?

SELECT u.username, u.id, COUNT(t.tahmin) AS tahmins_no, 
     SUM(t.result = 1) AS winnings, 
     SUM(t.result = 2) AS loses, 
     sum(case when t.tahmin = 1 and t.result = 1 then 1 else 0 end) * 1 as ms1, 
     sum(case when t.tahmin = 2 and t.result = 1 then 1 else 0 end) * 3 as ms0, 
     sum(case when t.tahmin = 3 and t.result = 1 then 1 else 0 end) * 1 as ms2, 
     sum(case when t.tahmin = 4 and t.result = 1 then 1 else 0 end) * 2 as alt, 
     sum(case when t.tahmin = 5 and t.result = 1 then 1 else 0 end) * 2 as ust, 
     sum(case when t.tahmin = 6 and t.result = 1 then 1 else 0 end) * 3 as tg_0_1, 
     sum(case when t.tahmin = 7 and t.result = 1 then 1 else 0 end) * 2 as tg_2_3, 
     sum(case when t.tahmin = 8 and t.result = 1 then 1 else 0 end) * 4 as tg_4_6, 
     sum(case when t.tahmin = 9 and t.result = 1 then 1 else 0 end) * 20 as tg_7, 
     sum(case when t.tahmin = 10 and t.result = 1 then 1 else 0 end) * 1 as kg_var, 
     sum(case when t.tahmin = 11 and t.result = 1 then 1 else 0 end) * 1 as kg_yok 
     sum(ms1 + ms0 + ms2 + alt + ust + tg_0_1 + tg_2_3 + tg_4_6 + tg_7 + kg_var + kg_yok) as total  
FROM users u 
LEFT JOIN tahminler t ON u.id = t.user_id 
LEFT JOIN matches_of_comments mc ON t.match_id = mc.match_id 
WHERE MONTH(STR_TO_DATE(mc.match_date, '%d.%m.%Y')) = 01 AND 
     YEAR(STR_TO_DATE(mc.match_date, '%d.%m.%Y')) = 2014 AND flag=1 
GROUP BY u.id 
HAVING tahmins_no > 0 
ORDER BY total DESC 

查詢工作非常好,我得到預期的結果,唯一的問題是,當我下面的行添加到查詢:

總和(MS1 + MS 0 + MS2 + ALT +烏斯+ tg_0_1 + tg_2_3 + tg_4_6 + tg_7 + kg_var + kg_yok)作爲總

我想total我想知道要我的專欄是線正確與否?它的語法正確與否?

+0

你缺少在上面的'total'行的最後一個逗號。 – dasblinkenlight

回答

1

您試圖獲得SUM的SUM(在這種情況下需要noz),並且您希望重新使用別名,兩者都不能在標準SQL(不包括派生表)中使用。

如果它是隻用於選別,你只需要

ORDER BY ms1 + ms0 + ms2 + alt + ust + tg_0_1 + tg_2_3 + tg_4_6 + tg_7 + kg_var + kg_yok DESC 

如果你想在你的SELECT列表顯示總需要重複計算:

sum(case when t.tahmin = 1 and t.result = 1 then 1 else 0 end) * 1 + 
    sum(case when t.tahmin = 2 and t.result = 1 then 1 else 0 end) * 3 + 
    .... 
    sum(case when t.tahmin = 11 and t.result = 1 then 1 else 0 end) * 1 as TOTAL 

或更好的使用派生表:

SELECT username, id, tahmins_no, 
     winnings, 
     loses, 
     ms1, 
     ms0, 
     ... 
     kg_yok, 
     ms1 + ms0 + ms2 + alt + ust + tg_0_1 + tg_2_3 + tg_4_6 + tg_7 + kg_var + kg_yok as TOTAL 
FROM 
    (
    your existing query (without ORDER BY) 
) as dt 
ORDER BY TOTAL DESC 
0

你的特殊金額實際上不是一個總和,而是一個簡單的加法,它應該是b e使用包裝器查詢進行。

查詢的總和可以通過使用mysql中的true爲1和false爲0(因爲您已經在爲winningsloses執行操作)而簡化。

你也有你的group by第一個缺陷:你必須列出所有,聚合列(即usernameid),否則你會得到不規範的行爲(即錯誤的結果)。請注意,這也可以更簡單地表達。

試試這個:

SELECT *, ms1 + ms0 + ms2 + alt + ust + tg_0_1 + tg_2_3 + tg_4_6 + tg_7 + kg_var + kg_yok as total FROM (
    SELECT 
    u.username, 
    u.id, 
    COUNT(t.tahmin) AS tahmins_no, 
    SUM(t.result = 1) AS winnings, 
    SUM(t.result = 2) AS loses, 
    sum(t.tahmin = 1 and t.result = 1) * 1 as ms1, 
    sum(t.tahmin = 2 and t.result = 1 then 1 else 0 end) * 3 as ms0, 
    sum(t.tahmin = 3 and t.result = 1) * 1 as ms2, 
    sum(t.tahmin = 4 and t.result = 1) * 2 as alt, 
    sum(t.tahmin = 5 and t.result = 1) * 2 as ust, 
    sum(t.tahmin = 6 and t.result = 1) * 3 as tg_0_1, 
    sum(t.tahmin = 7 and t.result = 1) * 2 as tg_2_3, 
    sum(t.tahmin = 8 and t.result = 1) * 4 as tg_4_6, 
    sum(t.tahmin = 9 and t.result = 1) * 20 as tg_7, 
    sum(t.tahmin = 10 and t.result = 1) * 1 as kg_var, 
    sum(t.tahmin = 11 and t.result = 1) * 1 as kg_yok 
    FROM users u 
    LEFT JOIN tahminler t ON u.id = t.user_id 
    LEFT JOIN matches_of_comments mc ON t.match_id = mc.match_id 
    WHERE MONTH(STR_TO_DATE(mc.match_date, '%d.%m.%Y')) = 01 
    AND YEAR(STR_TO_DATE(mc.match_date, '%d.%m.%Y')) = 2014 
    AND flag=1 
    GROUP BY 1, 2 
    HAVING tahmins_no > 0) x 
ORDER BY total DESC 
相關問題