MS Access:按计算字段排序(使用别名)

mal*_*lte 3 sql ms-access

在选择查询中,我使用嵌套选择来计算字段。我想按计算字段(排名)对结果进行排序,但是 Access 无法识别字段排名。(当我运行查询时,Access 要求提供rank 的参数值。)

SELECT
  *,
  (select count(*)
   from tbl as tbl2
   where tbl.customers > tbl2.customers and tbl.dept = tbl2.dept
  ) + 1 as rank
FROM tbl
ORDER BY rank
Run Code Online (Sandbox Code Playgroud)

[示例查询取自这篇文章]

Mad*_*nan 5

使用派生表

SELECT * FROM
(
SELECT
  *,
  (select count(*)
   from tbl as tbl2
   where tbl.customers > tbl2.customers and tbl.dept = tbl2.dept
  ) + 1 as rank
FROM tbl
) as newtbl
ORDER BY rank
Run Code Online (Sandbox Code Playgroud)