Oracle SELECT - 一列的别名作为另一列的输入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15389338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Oracle SELECT - alias of one column as an input to another
提问by Senthu Sivasambu
Folks I found similar but not exact questions on this forum - pardon me if i have not done enough searching for them. This is my question..in Oracle
我在这个论坛上发现了类似但不准确的问题 - 如果我没有对它们进行足够的搜索,请原谅我。这是我的问题……在 Oracle 中
select ( t.value*2) as inst2, (inst2 * 3) as inst3
from table t;
the thinking behind is if f() = t.value*2
is an expensive call, then we do not need to make that twice..or is there an alternative query structure i could use (I am trying to achieve this in CTAS)
背后的想法是,如果f() = t.value*2
是一个昂贵的调用,那么我们不需要进行两次......或者是否有我可以使用的替代查询结构(我试图在 CTAS 中实现这一点)
thanks in advance.
提前致谢。
回答by Thomas Tschernich
Another option:
另外一个选项:
with cte as (
select t.value*2 as inst2
)
select
cte.inst2,
(cte.inst2*3) as inst3
from cte
This is actually the same as in bluefeet's reply, but I would consider it easier to understand with the with
-syntax.
这实际上与 bluefeet 的回复相同,但我认为使用with
-syntax更容易理解。
回答by Taryn
If you want to use an alias in the second calculation, then you will want to use a subquery:
如果要在第二个计算中使用别名,则需要使用子查询:
select inst2,
(inst2 * 3) as inst3
from
(
select t.value*2 as inst2
from table t
)