Select 常量作为伪列并对伪列进行算术运算

Select constants as pseudo columns and do arithmetic on pseudo columns

我想做这样的事情:

select 999 as price, 0.1 as tax_rate, 
price*tax_rate as tax_amount, tax_amount+price as total
from Dual;

在 Oracle 数据库上。

您可以使用 CROSS APPLY(Oracle 12c):

select price, tax_rate, tax_amount, tax_amount+price as total
from Dual
CROSS APPLY (SELECT 999 AS price, 0.1 AS tax_rate FROM dual) s
CROSS APPLY (SELECT price*tax_rate AS tax_amount FROM dual) s2;

select price, tax_rate, tax_amount, tax_amount+price as total
from Dual
,LATERAL (SELECT 999 AS price, 0.1 AS tax_rate FROM dual) s
,LATERAL (SELECT price*tax_rate AS tax_amount FROM dual) s2;

db<>fiddle demo

您无法在同一级别引用已定义的列(SELECT),但使用 CROSS APPLY/LATERAL JOIN 您可以在不使用子查询的情况下创建一系列计算列。

类似的方法:PostgreSQL using a calculated column in the same query