使用Microsoft Access中的子查询按ABS排序

时间:2013-03-20 17:24:49

标签: ms-access subquery

我正在尝试在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列排序。任何人都有这个问题的经验吗?

1 个答案:

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