我有了一個數學計算和聚合函數如下查詢:如何在同一查詢中添加多個聚合函數的結果?
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
我想知道要我的專欄是線正確與否?它的語法正確與否?
你缺少在上面的'total'行的最後一個逗號。 – dasblinkenlight