查找小于平均值的计算比率(SQL)

时间:2014-09-29 04:26:38

标签: sql ms-access

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低于平均值的所有玩家。为什么这段代码不起作用?

1 个答案:

答案 0 :(得分: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)

问题是,在执行where或having子句时,最初不会知道ATORatio。因此,您需要在where或having子句中使用字段ATORatio的值本身。