SQL SELECT:在“as"之前的下一个选定参数中使用“as"之后的分配值 [英] SQL SELECT: Using the assigned value after 'as' in the next selected parameter before 'as'
问题描述
我需要在下一条语句中使用 sql 中两个函数的计算值(在 'as' 关键字之后).我想使用它是因为在这种情况下计算时间减少了 x2.
I need to use the computed values (after 'as' keyword) of two functions in sql in the next statement. I want to use it because the time of computations decreases x2 in this case.
我有以下声明:
SELECT f1() as f_1, f2() as f_2, f_1 - f_2 as f1_minus_f2 FROM mytable
其中 f1(), f2() - 一些函数
where f1(), f2() - some functions
推荐答案
1) 您可以使用表达式本身(如果函数是确定性的):
1) You can use expression itself (if functions are deterministic):
SELECT f1() as f_1, f2() as f_2, f1() - f2() as f1_minus_f2
FROM mytable
2) 使用子查询:
SELECT sub.f_1, sub.f_2, sub.f_1 - sub.f_2 as f1_minus_f2
FROM (
SELECT f1() as f_1, f2() as f_2
FROM mytable
) sub
您不能随意使用它的原因是 all-at-once
规则:
The reason you cannot use it as you want is all-at-once
rule:
All-at-Once Operations"意味着所有表达式都在同一个逻辑查询过程阶段同时进行逻辑评估.
"All-at-Once Operations" means that all expressions in the same logical query process phase are evaluated logically at the same time.
这篇关于SQL SELECT:在“as"之前的下一个选定参数中使用“as"之后的分配值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!