2014-09-29 154 views
1
SELECT 
    PlayerName, ROUND(SUM(A)/SUM(TO),2) AS ATORatio 
FROM 
    Players_t 
INNER JOIN 
    GameStats_t ON Players_t.JerseyNumber=GameStats_t.JerseyNumber 
WHERE 
    ATORatio < (SELECT SUM(A)/SUM(TO) FROM GameStats_t) 
GROUP BY 
    PlayerName; 

我需要返回所有比ATORatio小於平均值的玩家。爲什麼這個代碼不工作?查找小於平均值的計算比率(SQL)

+1

使用having子句 – 2014-09-29 04:33:19

+0

當我這樣做,MS Access就會要求我輸入ATORatio參數值(我簡直複製並粘貼組通過下面的WHERE行並將其變更爲 – 2014-09-29 04:35:01

回答

1

下面應該有所幫助:

SELECT 
    PlayerName, ROUND(SUM(A)/SUM(TO),2) AS ATORatio 
FROM 
    Players_t 
INNER JOIN 
    GameStats_t ON Players_t.JerseyNumber=GameStats_t.JerseyNumber 
GROUP BY 
    PlayerName; 
HAVING    ----> You can also use WHERE here 
    ROUND(SUM(A)/SUM(TO),2) < (SELECT SUM(A)/SUM(TO) FROM GameStats_t) 

的問題是,ATORatio會在執行WHERE或HAVING子句不最初稱爲。因此,您需要在where或having子句中使用字段ATORatio的值。

+0

非常感謝!!!! – 2014-09-29 04:36:59

+1

@WilliamSmolen:如果它對你有幫助,當你有足夠的聲望時,請接受作爲答覆和upvote!:) – 2014-09-29 04:38:32