我制作了具有多列的表格,每列都有单独的查询。如何添加呢?
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是总行中的无效标识符。
我该如何工作?:(
您只能column1
在order-by子句中引用列别名(例如此处)。从文档中:
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,
(select count(x) from table3 a, table4 b where ~~) as column2,
(select count(x) from table5 a, table6 b where ~~) as column3
from dual
);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句