sql - 微软 Access : Order by calculated field (using alias)

标签 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

[示例查询取自this发布]

最佳答案

使用派生表

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

关于sql - 微软 Access : Order by calculated field (using alias),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22782653/

相关文章:

sql - 在列中的任何行等于1的情况下选择行,如果不相等,则列等于2,但不能同时包含两者

sql - 寻找丢失的 key

ms-access - .FindFirst VBA MS Access 函数出现意外行为 : . NoMatch 始终返回 true

java - 如何使用 UCanAccess 更改表

sql - 如何选择一列中的列值重复但另一列中的列值不同的行?

mysql - 没有 GROUP BY 的 SQL 对表中的组进行排序

java - SQL/Java并发事务死锁

php - 按数字字母顺序输出列表 - MYSQL

从受密码保护的 Access 数据库进行 Excel VBA 查询

mysql - 从 MS Access 移植