ORDER无法识别别名

时间:2019-06-08 16:43:02

标签: sql ms-access sql-order-by

我有一个带有别名字段名称“ Status”的计算字段。该字段显示所有正确的数据。当我将此别名添加到ORDER BY子句时,别名无法识别。

SELECT ORG, PR, NOUN, Elapsed, Target_Days, Supportable_Date,
      (Target_Days - Elapsed) AS Status
FROM qry_Gate_Status
ORDER BY ORG, Supportable_Date, Status;

如何获取别名“状态”以与ORDER BY一起使用?

3 个答案:

答案 0 :(得分:3)

别名不能与ORDER BY一起使用。而是这样做:

SELECT ORG, PR, NOUN, Elapsed, Target_Days, Supportable_Date,
  (Target_Days - Elapsed) AS Status
FROM qry_Gate_Status
ORDER BY ORG, Supportable_Date, (Target_Days - Elapsed);

答案 1 :(得分:2)

如果您不想重复代码,一个简单的解决方案可以基于子查询

select * from (
  SELECT ORG, PR, NOUN, Elapsed, Target_Days, Supportable_Date,
  (Target_Days - Elapsed) AS Status
  FROM qry_Gate_Status
) t
ORDER BY ORG, Supportable_Date, Status;

答案 2 :(得分:2)

不幸的是,在Access SQL中无法识别别名。我使用了对我来说足够优雅的ORDER BY(列号)。

SELECT ORG, PR, NOUN, Elapsed, Target_Days, Supportable_Date,
      (Target_Days - Elapsed) AS Status
FROM qry_Gate_Status
ORDER BY ORG, Supportable_Date, 7;