了解 Oracle 别名 - 为什么除非包含在第二个查询中,否则查询中无法识别别名?

Understanding Oracle aliasing - why isn#39;t an alias not recognized in a query unless wrapped in a second query?(了解 Oracle 别名 - 为什么除非包含在第二个查询中,否则查询中无法识别别名?)
本文介绍了了解 Oracle 别名 - 为什么除非包含在第二个查询中,否则查询中无法识别别名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题


SELECT COUNT(*) AS "CNT",
       imei
FROM   devices  

执行得很好.我想用 WHERE 语句进一步限制查询.(人为)合乎逻辑的下一步是按以下方式修改查询:

which executes just fine. I want to further restrict the query with a WHERE statement. The (humanly) logical next step is to modify the query followingly:


SELECT COUNT(*) AS "CNT",
       imei
FROM   devices
WHERE  CNT > 1 

然而,这会导致错误消息ORA-00904:CNT":标识符无效.出于某种原因,将查询包装在另一个查询中会产生所需的结果:

However, this results in a error message ORA-00904: "CNT": invalid identifier. For some reason, wrapping the query in another query produces the desired result:


SELECT *
FROM   (SELECT COUNT(*) AS "CNT",
               imei
        FROM   devices
        GROUP  BY imei)
WHERE  CNT > 1  

为什么 Oracle 在第二个查询中无法识别别名CNT"?

Why does Oracle not recognize the alias "CNT" in the second query?

推荐答案

简单的答案是 AS 子句定义了结果中将调用的列,与查询本身.

The simple answer is that the AS clause defines what the column will be called in the result, which is a different scope than the query itself.

在您的示例中,使用 HAVING 子句效果最佳:

In your example, using the HAVING clause would work best:

SELECT COUNT(*) AS "CNT",
       imei
FROM   devices
GROUP  BY imei
HAVING COUNT(*) > 1

这篇关于了解 Oracle 别名 - 为什么除非包含在第二个查询中,否则查询中无法识别别名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)