2

我正在尝试在 Microsoft Access 中执行以下查询

SELECT Weights.Group, Abs(Wt-(Select avg(Wt) from Weights w2 where Weights.Group = w2.Group)) AS Rank
FROM Weights
WHERE (((Weights.[Selected])=1))
ORDER BY Abs(Wt-(Select avg(Wt) from Weights w2 where Weights.Group = w2.Group));

对照下表

ID  Wt  Selected    Group
1   911 1   1
2   912 1   1
3   913 0   1
4   914 1   1
5   880 1   2
6   890 1   2
7   885 1   2
8   886 1   2

当我执行查询时,我得到一个无效的语法错误。如果我尝试仅引用 Order By Clause 中的第 2 列,它看起来好像是按文字 2 而不是第 2 列排序。有人对这个问题有任何经验吗?

4

1 回答 1

3

我想你必须把它包装起来:

SELECT * FROM (
    SELECT 
       Weights.Group, 
       Abs([Wt]-
         (Select avg(Wt) from Weights w2 where Weights.Group = w2.Group)) AS Rank
    FROM Weights
    WHERE Weights.Selected=1)
ORDER BY Rank
于 2013-03-20T17:32:32.860 回答