如何对单独查询的多个列求和?
How do you make sum of multiple columns made with separate query?
我用多列制作了 table,每列都有单独的查询。我该如何添加?
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;
如果每列都来自相同的 table,则上述查询有效,但在这种情况下,Oracle SQL 告诉我第 1、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
使用常见的 table 表达式:
with cte as (
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
)
select c.*, column1 + column2 + column3 as total
from cte c;
您只能在 order-by 子句中引用列别名 - 如此处的 column1
。 From the documentation:
c_alias
Specify an alias for the column expression. Oracle Database will use this alias in the column heading of the result set. The AS
keyword is optional. The alias effectively renames the select list item for the duration of the query. The alias can be used in the order_by_clause but not other clauses in the query.
您可以使用子查询获取计算列,然后使用别名在 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
);
我用多列制作了 table,每列都有单独的查询。我该如何添加?
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;
如果每列都来自相同的 table,则上述查询有效,但在这种情况下,Oracle SQL 告诉我第 1、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
使用常见的 table 表达式:
with cte as (
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
)
select c.*, column1 + column2 + column3 as total
from cte c;
您只能在 order-by 子句中引用列别名 - 如此处的 column1
。 From the documentation:
c_alias
Specify an alias for the column expression. Oracle Database will use this alias in the column heading of the result set. The
AS
keyword is optional. The alias effectively renames the select list item for the duration of the query. The alias can be used in the order_by_clause but not other clauses in the query.
您可以使用子查询获取计算列,然后使用别名在 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
);