问题描述
select
(select count(x) from table1 a,table2 b where ~~;) as column1,(select count(x) from table3 a,table4 b where ~~;) as column2,(select count(x) from table5 a,table6 b where ~~;) as column3,(column1 + column2 + column3) as total
from dual;
如果每个列都来自同一表,则上面的查询有效,但是在这种情况下,Oracle sql告诉我column1、2、3是总行中的无效标识符。
我如何进行这项工作? :(
解决方法
您可以使用子查询:
select *,column1+column2+column3 as total from
(
select
(select count(x) from table1 a,table2 b where ...) as column1,(select count(x) from table3 a,table4 b where ...) as column2,(select count(x) from table5 a,table6 b where ...) as column3
from dual
) X
,
使用通用表表达式:
EventHandler
,
您只能在order-by子句中引用列别名,例如column1
。 From the documentation:
c_alias
为列表达式指定别名。 Oracle数据库将在结果集的列标题中使用此别名。
AS
关键字是可选的。别名在查询期间有效地重命名了选择列表项。别名可以在 order_by_clause 中使用,但不能在查询中的其他子句中使用。
您可以使用子查询来获取计算的列,然后使用别名在CTE或嵌入式视图中在该子查询之外执行进一步的工作:
select column1,column2,column3,(column1 + column2 + column3) as total
from (
select
(select count(x) from table1 a,table2 b where ~~) as column1,table4 b where ~~) as column2,table6 b where ~~) as column3
from dual
);